Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / binutils / bfd / elf64-mips.c
1 /* MIPS-specific support for 64-bit ELF
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Ian Lance Taylor, Cygnus Support
5    Linker support added by Mark Mitchell, CodeSourcery, LLC.
6    <mark@codesourcery.com>
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
23
24 /* This file supports the 64-bit MIPS ELF ABI.
25
26    The MIPS 64-bit ELF ABI uses an unusual reloc format.  This file
27    overrides the usual ELF reloc handling, and handles reading and
28    writing the relocations here.  */
29
30 /* TODO: Many things are unsupported, even if there is some code for it
31  .       (which was mostly stolen from elf32-mips.c and slightly adapted).
32  .
33  .   - Relocation handling for REL relocs is wrong in many cases and
34  .     generally untested.
35  .   - Relocation handling for RELA relocs related to GOT support are
36  .     also likely to be wrong.
37  .   - Support for MIPS16 is only partially implemented.
38  .   - Embedded PIC  is only partially implemented (is it needed?).
39  .   - Combined relocs with RSS_* entries are unsupported.
40  .   - The whole GOT handling for NewABI is missing, some parts of
41  .     the OldABI version is still lying around and shold be removed.
42  */
43
44 #include "bfd.h"
45 #include "sysdep.h"
46 #include "libbfd.h"
47 #include "aout/ar.h"
48 #include "bfdlink.h"
49 #include "genlink.h"
50 #include "elf-bfd.h"
51 #include "elf/mips.h"
52
53 /* Get the ECOFF swapping routines.  The 64-bit ABI is not supposed to
54    use ECOFF.  However, we support it anyhow for an easier changeover.  */
55 #include "coff/sym.h"
56 #include "coff/symconst.h"
57 #include "coff/internal.h"
58 #include "coff/ecoff.h"
59 /* The 64 bit versions of the mdebug data structures are in alpha.h.  */
60 #include "coff/alpha.h"
61 #define ECOFF_SIGNED_64
62 #include "ecoffswap.h"
63
64 struct mips_elf64_link_hash_entry;
65
66 static void mips_elf64_swap_reloc_in
67   PARAMS ((bfd *, const Elf64_Mips_External_Rel *,
68            Elf64_Mips_Internal_Rel *));
69 static void mips_elf64_swap_reloca_in
70   PARAMS ((bfd *, const Elf64_Mips_External_Rela *,
71            Elf64_Mips_Internal_Rela *));
72 static void mips_elf64_swap_reloc_out
73   PARAMS ((bfd *, const Elf64_Mips_Internal_Rel *,
74            Elf64_Mips_External_Rel *));
75 static void mips_elf64_swap_reloca_out
76   PARAMS ((bfd *, const Elf64_Mips_Internal_Rela *,
77            Elf64_Mips_External_Rela *));
78 static void mips_elf64_be_swap_reloc_in
79   PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rel *));
80 static void mips_elf64_be_swap_reloc_out
81   PARAMS ((bfd *, const Elf_Internal_Rel *, bfd_byte *));
82 static void mips_elf64_be_swap_reloca_in
83   PARAMS ((bfd *, const bfd_byte *, Elf_Internal_Rela *));
84 static void mips_elf64_be_swap_reloca_out
85   PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
86 static bfd_vma mips_elf64_high PARAMS ((bfd_vma));
87 static bfd_vma mips_elf64_higher PARAMS ((bfd_vma));
88 static bfd_vma mips_elf64_highest PARAMS ((bfd_vma));
89 static reloc_howto_type *mips_elf64_reloc_type_lookup
90   PARAMS ((bfd *, bfd_reloc_code_real_type));
91 static void mips_elf64_info_to_howto_rel
92   PARAMS ((bfd *, arelent *, Elf64_Internal_Rel *));
93 static void mips_elf64_info_to_howto_rela
94   PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
95 static long mips_elf64_get_reloc_upper_bound PARAMS ((bfd *, asection *));
96 static boolean mips_elf64_slurp_one_reloc_table
97   PARAMS ((bfd *, asection *, asymbol **, const Elf_Internal_Shdr *));
98 static boolean mips_elf64_slurp_reloc_table
99   PARAMS ((bfd *, asection *, asymbol **, boolean));
100 static void mips_elf64_write_relocs PARAMS ((bfd *, asection *, PTR));
101 static void mips_elf64_write_rel
102   PARAMS((bfd *, asection *, Elf_Internal_Shdr *, int *, PTR));
103 static void mips_elf64_write_rela
104   PARAMS((bfd *, asection *, Elf_Internal_Shdr *, int *, PTR));
105 static struct bfd_hash_entry *mips_elf64_link_hash_newfunc
106   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
107 static bfd_reloc_status_type mips_elf64_hi16_reloc
108   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
109 static bfd_reloc_status_type mips_elf64_higher_reloc
110   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
111 static bfd_reloc_status_type mips_elf64_highest_reloc
112   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
113 static bfd_reloc_status_type mips_elf64_gprel16_reloc
114   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
115 static bfd_reloc_status_type mips_elf64_gprel16_reloca
116   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
117 static bfd_reloc_status_type mips_elf64_literal_reloc
118   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
119 static bfd_reloc_status_type mips_elf64_gprel32_reloc
120   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
121 static bfd_reloc_status_type mips_elf64_shift6_reloc
122   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
123 static bfd_reloc_status_type mips_elf64_got16_reloc
124   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
125 static boolean mips_elf64_assign_gp PARAMS ((bfd *, bfd_vma *));
126 static bfd_reloc_status_type mips_elf64_final_gp
127   PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
128 static bfd_reloc_status_type gprel16_with_gp
129   PARAMS ((bfd *, asymbol *, arelent *, asection *, boolean, PTR, bfd_vma));
130 static int mips_elf64_additional_program_headers PARAMS ((bfd *));
131 static struct bfd_link_hash_table *mips_elf64_link_hash_table_create
132   PARAMS((bfd *));
133 static bfd_vma mips_elf64_got_offset_from_index
134   PARAMS ((bfd *, bfd *, bfd_vma));
135 static struct mips_elf64_got_info *_mips_elf64_got_info
136   PARAMS ((bfd *, asection **));
137 static bfd_vma mips_elf64_sign_extend PARAMS ((bfd_vma, int));
138 static boolean mips_elf64_overflow_p PARAMS ((bfd_vma, int));
139 static bfd_vma mips_elf64_global_got_index
140   PARAMS ((bfd *, struct elf_link_hash_entry *));
141 static boolean mips_elf64_sort_hash_table_f
142   PARAMS ((struct mips_elf64_link_hash_entry *, PTR));
143 static boolean mips_elf64_sort_hash_table
144   PARAMS ((struct bfd_link_info *, unsigned long));
145 static void mips_elf64_swap_msym_out
146   PARAMS ((bfd *, const Elf32_Internal_Msym *, Elf32_External_Msym *));
147 static bfd_vma mips_elf64_create_local_got_entry
148   PARAMS ((bfd *abfd, struct mips_elf64_got_info *, asection *,
149            bfd_vma value));
150 static bfd_vma mips_elf64_local_got_index
151   PARAMS ((bfd *, struct bfd_link_info *, bfd_vma));
152 static bfd_vma mips_elf64_got_page
153   PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, bfd_vma *));
154 static bfd_vma mips_elf64_got16_entry
155   PARAMS ((bfd *, struct bfd_link_info *, bfd_vma, boolean));
156 static boolean mips_elf64_local_relocation_p
157   PARAMS ((bfd *, const Elf_Internal_Rela *, asection **, boolean));
158 static const Elf_Internal_Rela *mips_elf64_next_relocation
159   PARAMS ((unsigned int, const Elf_Internal_Rela *,
160            const Elf_Internal_Rela *));
161 static boolean mips_elf64_create_dynamic_relocation
162   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
163            struct mips_elf64_link_hash_entry *, asection *, bfd_vma,
164            bfd_vma *, asection *));
165 static bfd_reloc_status_type mips_elf64_calculate_relocation
166   PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
167            const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
168            Elf_Internal_Sym *, asection **, bfd_vma *, const char **,
169            boolean *));
170 static bfd_vma mips_elf64_obtain_contents
171   PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
172 static boolean mips_elf64_perform_relocation
173   PARAMS ((struct bfd_link_info *, reloc_howto_type *,
174            const Elf_Internal_Rela *, bfd_vma,
175            bfd *, asection *, bfd_byte *, boolean));
176 static boolean mips_elf64_relocate_section
177   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
178            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
179 boolean mips_elf64_create_dynamic_sections
180   PARAMS ((bfd *, struct bfd_link_info *));
181 boolean mips_elf64_adjust_dynamic_symbol
182   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *h));
183 boolean mips_elf64_always_size_sections
184   PARAMS ((bfd *, struct bfd_link_info *));
185 static boolean mips_elf64_check_mips16_stubs
186   PARAMS ((struct mips_elf64_link_hash_entry *, PTR));
187 boolean mips_elf64_size_dynamic_sections
188   PARAMS ((bfd *, struct bfd_link_info *));
189 boolean mips_elf64_finish_dynamic_symbol
190   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
191            Elf_Internal_Sym *));
192 boolean mips_elf64_finish_dynamic_sections
193   PARAMS ((bfd *, struct bfd_link_info *info));
194 asection *mips_elf64_gc_mark_hook
195   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
196            struct elf_link_hash_entry *, Elf_Internal_Sym *));
197 boolean mips_elf64_gc_sweep_hook
198   PARAMS ((bfd *, struct bfd_link_info *, asection *,
199            const Elf_Internal_Rela *));
200 static boolean mips_elf64_create_got_section
201   PARAMS ((bfd *, struct bfd_link_info *));
202 static boolean mips_elf64_record_global_got_symbol
203   PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *,
204            struct mips_elf64_got_info *));
205 static asection *mips_elf64_create_msym_section PARAMS((bfd *));
206 static void mips_elf64_allocate_dynamic_relocations
207   PARAMS ((bfd *, unsigned int));
208 static boolean mips_elf64_stub_section_p PARAMS ((bfd *, asection *));
209 boolean mips_elf64_check_relocs
210   PARAMS ((bfd *, struct bfd_link_info *, asection *,
211            const Elf_Internal_Rela *));
212 static boolean mips_elf64_output_extsym
213         PARAMS ((struct mips_elf64_link_hash_entry *, PTR));
214 static void mips_elf64_swap_gptab_in
215   PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
216 static void mips_elf64_swap_gptab_out
217   PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
218 static int gptab_compare PARAMS ((const PTR, const PTR));
219 boolean mips_elf64_final_link PARAMS ((bfd *, struct bfd_link_info *));
220
221 extern const bfd_target bfd_elf64_bigmips_vec;
222 extern const bfd_target bfd_elf64_littlemips_vec;
223
224 static bfd_vma prev_reloc_addend = 0;
225 static bfd_size_type prev_reloc_address = 0;
226
227 /* Whether we are trying to be compatible with IRIX6 (or little endianers
228    which are otherwise IRIX-ABI compliant).  */
229 #define SGI_COMPAT(abfd) \
230   ((abfd->xvec == &bfd_elf64_bigmips_vec) \
231    || (abfd->xvec == &bfd_elf64_littlemips_vec) ? true : false)
232
233 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
234    from smaller values.  Start with zero, widen, *then* decrement.  */
235 #define MINUS_ONE       (((bfd_vma)0) - 1)
236
237 /* The number of local .got entries we reserve.  */
238 #define MIPS_RESERVED_GOTNO (2)
239
240 /* Instructions which appear in a stub.  */
241 #define ELF_MIPS_GP_OFFSET(abfd) 0x7ff0
242 #define STUB_LW    0xdf998010   /* ld t9,0x8010(gp) */
243 #define STUB_MOVE  0x03e07825   /* move t7,ra */
244 #define STUB_JALR  0x0320f809   /* jal t9 */
245 #define STUB_LI16  0x34180000   /* ori t8,zero,0 */
246 #define MIPS_FUNCTION_STUB_SIZE (16)
247
248 /* The relocation table used for SHT_REL sections.  */
249
250 #define UNUSED_RELOC(num) { num, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
251
252 static reloc_howto_type mips_elf64_howto_table_rel[] =
253 {
254   /* No relocation.  */
255   HOWTO (R_MIPS_NONE,           /* type */
256          0,                     /* rightshift */
257          0,                     /* size (0 = byte, 1 = short, 2 = long) */
258          0,                     /* bitsize */
259          false,                 /* pc_relative */
260          0,                     /* bitpos */
261          complain_overflow_dont, /* complain_on_overflow */
262          bfd_elf_generic_reloc, /* special_function */
263          "R_MIPS_NONE",         /* name */
264          false,                 /* partial_inplace */
265          0,                     /* src_mask */
266          0,                     /* dst_mask */
267          false),                /* pcrel_offset */
268
269   /* 16 bit relocation.  */
270   HOWTO (R_MIPS_16,             /* type */
271          0,                     /* rightshift */
272          2,                     /* size (0 = byte, 1 = short, 2 = long) */
273          16,                    /* bitsize */
274          false,                 /* pc_relative */
275          0,                     /* bitpos */
276          complain_overflow_signed, /* complain_on_overflow */
277          bfd_elf_generic_reloc, /* special_function */
278          "R_MIPS_16",           /* name */
279          true,                  /* partial_inplace */
280          0x0000ffff,            /* src_mask */
281          0x0000ffff,            /* dst_mask */
282          false),                /* pcrel_offset */
283
284   /* 32 bit relocation.  */
285   HOWTO (R_MIPS_32,             /* type */
286          0,                     /* rightshift */
287          2,                     /* size (0 = byte, 1 = short, 2 = long) */
288          32,                    /* bitsize */
289          false,                 /* pc_relative */
290          0,                     /* bitpos */
291          complain_overflow_dont, /* complain_on_overflow */
292          bfd_elf_generic_reloc, /* special_function */
293          "R_MIPS_32",           /* name */
294          true,                  /* partial_inplace */
295          0xffffffff,            /* src_mask */
296          0xffffffff,            /* dst_mask */
297          false),                /* pcrel_offset */
298
299   /* 32 bit symbol relative relocation.  */
300   HOWTO (R_MIPS_REL32,          /* type */
301          0,                     /* rightshift */
302          2,                     /* size (0 = byte, 1 = short, 2 = long) */
303          32,                    /* bitsize */
304          false,                 /* pc_relative */
305          0,                     /* bitpos */
306          complain_overflow_dont, /* complain_on_overflow */
307          bfd_elf_generic_reloc, /* special_function */
308          "R_MIPS_REL32",        /* name */
309          true,                  /* partial_inplace */
310          0xffffffff,            /* src_mask */
311          0xffffffff,            /* dst_mask */
312          false),                /* pcrel_offset */
313
314   /* 26 bit jump address.  */
315   HOWTO (R_MIPS_26,             /* type */
316          2,                     /* rightshift */
317          2,                     /* size (0 = byte, 1 = short, 2 = long) */
318          26,                    /* bitsize */
319          false,                 /* pc_relative */
320          0,                     /* bitpos */
321          complain_overflow_dont, /* complain_on_overflow */
322                                 /* This needs complex overflow
323                                    detection, because the upper 36
324                                    bits must match the PC + 4.  */
325          bfd_elf_generic_reloc, /* special_function */
326          "R_MIPS_26",           /* name */
327          true,                  /* partial_inplace */
328          0x03ffffff,            /* src_mask */
329          0x03ffffff,            /* dst_mask */
330          false),                /* pcrel_offset */
331
332   /* High 16 bits of symbol value.  */
333   HOWTO (R_MIPS_HI16,           /* type */
334          0,                     /* rightshift */
335          2,                     /* size (0 = byte, 1 = short, 2 = long) */
336          16,                    /* bitsize */
337          false,                 /* pc_relative */
338          0,                     /* bitpos */
339          complain_overflow_dont, /* complain_on_overflow */
340          bfd_elf_generic_reloc, /* special_function */
341          "R_MIPS_HI16",         /* name */
342          true,                  /* partial_inplace */
343          0x0000ffff,            /* src_mask */
344          0x0000ffff,            /* dst_mask */
345          false),                /* pcrel_offset */
346
347   /* Low 16 bits of symbol value.  */
348   HOWTO (R_MIPS_LO16,           /* type */
349          0,                     /* rightshift */
350          2,                     /* size (0 = byte, 1 = short, 2 = long) */
351          16,                    /* bitsize */
352          false,                 /* pc_relative */
353          0,                     /* bitpos */
354          complain_overflow_dont, /* complain_on_overflow */
355          bfd_elf_generic_reloc, /* special_function */
356          "R_MIPS_LO16",         /* name */
357          true,                  /* partial_inplace */
358          0x0000ffff,            /* src_mask */
359          0x0000ffff,            /* dst_mask */
360          false),                /* pcrel_offset */
361
362   /* GP relative reference.  */
363   HOWTO (R_MIPS_GPREL16,        /* type */
364          0,                     /* rightshift */
365          2,                     /* size (0 = byte, 1 = short, 2 = long) */
366          16,                    /* bitsize */
367          false,                 /* pc_relative */
368          0,                     /* bitpos */
369          complain_overflow_signed, /* complain_on_overflow */
370          mips_elf64_gprel16_reloc, /* special_function */
371          "R_MIPS_GPREL16",      /* name */
372          true,                  /* partial_inplace */
373          0x0000ffff,            /* src_mask */
374          0x0000ffff,            /* dst_mask */
375          false),                /* pcrel_offset */
376
377   /* Reference to literal section.  */
378   HOWTO (R_MIPS_LITERAL,        /* type */
379          0,                     /* rightshift */
380          2,                     /* size (0 = byte, 1 = short, 2 = long) */
381          16,                    /* bitsize */
382          false,                 /* pc_relative */
383          0,                     /* bitpos */
384          complain_overflow_signed, /* complain_on_overflow */
385          mips_elf64_literal_reloc, /* special_function */
386          "R_MIPS_LITERAL",      /* name */
387          true,                  /* partial_inplace */
388          0x0000ffff,            /* src_mask */
389          0x0000ffff,            /* dst_mask */
390          false),                /* pcrel_offset */
391
392   /* Reference to global offset table.  */
393   HOWTO (R_MIPS_GOT16,          /* type */
394          0,                     /* rightshift */
395          2,                     /* size (0 = byte, 1 = short, 2 = long) */
396          16,                    /* bitsize */
397          false,                 /* pc_relative */
398          0,                     /* bitpos */
399          complain_overflow_signed, /* complain_on_overflow */
400          mips_elf64_got16_reloc, /* special_function */
401          "R_MIPS_GOT16",        /* name */
402          true,                  /* partial_inplace */
403          0x0000ffff,            /* src_mask */
404          0x0000ffff,            /* dst_mask */
405          false),                /* pcrel_offset */
406
407   /* 16 bit PC relative reference.  */
408   HOWTO (R_MIPS_PC16,           /* type */
409          0,                     /* rightshift */
410          2,                     /* size (0 = byte, 1 = short, 2 = long) */
411          16,                    /* bitsize */
412          true,                  /* pc_relative */
413          0,                     /* bitpos */
414          complain_overflow_signed, /* complain_on_overflow */
415          bfd_elf_generic_reloc, /* special_function */
416          "R_MIPS_PC16",         /* name */
417          true,                  /* partial_inplace */
418          0x0000ffff,            /* src_mask */
419          0x0000ffff,            /* dst_mask */
420          true),                 /* pcrel_offset */
421
422   /* 16 bit call through global offset table.  */
423   /* FIXME: This is not handled correctly.  */
424   HOWTO (R_MIPS_CALL16,         /* type */
425          0,                     /* rightshift */
426          2,                     /* size (0 = byte, 1 = short, 2 = long) */
427          16,                    /* bitsize */
428          false,                 /* pc_relative */
429          0,                     /* bitpos */
430          complain_overflow_signed, /* complain_on_overflow */
431          bfd_elf_generic_reloc, /* special_function */
432          "R_MIPS_CALL16",       /* name */
433          true,                  /* partial_inplace */
434          0x0000ffff,            /* src_mask */
435          0x0000ffff,            /* dst_mask */
436          false),                /* pcrel_offset */
437
438   /* 32 bit GP relative reference.  */
439   HOWTO (R_MIPS_GPREL32,        /* type */
440          0,                     /* rightshift */
441          2,                     /* size (0 = byte, 1 = short, 2 = long) */
442          32,                    /* bitsize */
443          false,                 /* pc_relative */
444          0,                     /* bitpos */
445          complain_overflow_dont, /* complain_on_overflow */
446          mips_elf64_gprel32_reloc, /* special_function */
447          "R_MIPS_GPREL32",      /* name */
448          true,                  /* partial_inplace */
449          0xffffffff,            /* src_mask */
450          0xffffffff,            /* dst_mask */
451          false),                /* pcrel_offset */
452
453   UNUSED_RELOC (13),
454   UNUSED_RELOC (14),
455   UNUSED_RELOC (15),
456
457   /* A 5 bit shift field.  */
458   HOWTO (R_MIPS_SHIFT5,         /* type */
459          0,                     /* rightshift */
460          2,                     /* size (0 = byte, 1 = short, 2 = long) */
461          5,                     /* bitsize */
462          false,                 /* pc_relative */
463          6,                     /* bitpos */
464          complain_overflow_bitfield, /* complain_on_overflow */
465          bfd_elf_generic_reloc, /* special_function */
466          "R_MIPS_SHIFT5",       /* name */
467          true,                  /* partial_inplace */
468          0x000007c0,            /* src_mask */
469          0x000007c0,            /* dst_mask */
470          false),                /* pcrel_offset */
471
472   /* A 6 bit shift field.  */
473   HOWTO (R_MIPS_SHIFT6,         /* type */
474          0,                     /* rightshift */
475          2,                     /* size (0 = byte, 1 = short, 2 = long) */
476          6,                     /* bitsize */
477          false,                 /* pc_relative */
478          6,                     /* bitpos */
479          complain_overflow_bitfield, /* complain_on_overflow */
480          mips_elf64_shift6_reloc, /* special_function */
481          "R_MIPS_SHIFT6",       /* name */
482          true,                  /* partial_inplace */
483          0x000007c4,            /* src_mask */
484          0x000007c4,            /* dst_mask */
485          false),                /* pcrel_offset */
486
487   /* 64 bit relocation.  */
488   HOWTO (R_MIPS_64,             /* type */
489          0,                     /* rightshift */
490          4,                     /* size (0 = byte, 1 = short, 2 = long) */
491          64,                    /* bitsize */
492          false,                 /* pc_relative */
493          0,                     /* bitpos */
494          complain_overflow_dont, /* complain_on_overflow */
495          bfd_elf_generic_reloc, /* special_function */
496          "R_MIPS_64",           /* name */
497          true,                  /* partial_inplace */
498          MINUS_ONE,             /* src_mask */
499          MINUS_ONE,             /* dst_mask */
500          false),                /* pcrel_offset */
501
502   /* Displacement in the global offset table.  */
503   /* FIXME: Not handled correctly.  */
504   HOWTO (R_MIPS_GOT_DISP,       /* type */
505          0,                     /* rightshift */
506          2,                     /* size (0 = byte, 1 = short, 2 = long) */
507          16,                    /* bitsize */
508          false,                 /* pc_relative */
509          0,                     /* bitpos */
510          complain_overflow_signed, /* complain_on_overflow */
511          bfd_elf_generic_reloc, /* special_function */
512          "R_MIPS_GOT_DISP",     /* name */
513          true,                  /* partial_inplace */
514          0x0000ffff,            /* src_mask */
515          0x0000ffff,            /* dst_mask */
516          false),                /* pcrel_offset */
517
518   /* Displacement to page pointer in the global offset table.  */
519   /* FIXME: Not handled correctly.  */
520   HOWTO (R_MIPS_GOT_PAGE,       /* type */
521          0,                     /* rightshift */
522          2,                     /* size (0 = byte, 1 = short, 2 = long) */
523          16,                    /* bitsize */
524          false,                 /* pc_relative */
525          0,                     /* bitpos */
526          complain_overflow_signed, /* complain_on_overflow */
527          bfd_elf_generic_reloc, /* special_function */
528          "R_MIPS_GOT_PAGE",     /* name */
529          true,                  /* partial_inplace */
530          0x0000ffff,            /* src_mask */
531          0x0000ffff,            /* dst_mask */
532          false),                /* pcrel_offset */
533
534   /* Offset from page pointer in the global offset table.  */
535   /* FIXME: Not handled correctly.  */
536   HOWTO (R_MIPS_GOT_OFST,       /* type */
537          0,                     /* rightshift */
538          2,                     /* size (0 = byte, 1 = short, 2 = long) */
539          16,                    /* bitsize */
540          false,                 /* pc_relative */
541          0,                     /* bitpos */
542          complain_overflow_signed, /* complain_on_overflow */
543          bfd_elf_generic_reloc, /* special_function */
544          "R_MIPS_GOT_OFST",     /* name */
545          true,                  /* partial_inplace */
546          0x0000ffff,            /* src_mask */
547          0x0000ffff,            /* dst_mask */
548          false),                /* pcrel_offset */
549
550   /* High 16 bits of displacement in global offset table.  */
551   /* FIXME: Not handled correctly.  */
552   HOWTO (R_MIPS_GOT_HI16,       /* type */
553          0,                     /* rightshift */
554          2,                     /* size (0 = byte, 1 = short, 2 = long) */
555          16,                    /* bitsize */
556          false,                 /* pc_relative */
557          0,                     /* bitpos */
558          complain_overflow_dont, /* complain_on_overflow */
559          bfd_elf_generic_reloc, /* special_function */
560          "R_MIPS_GOT_HI16",     /* name */
561          true,                  /* partial_inplace */
562          0x0000ffff,            /* src_mask */
563          0x0000ffff,            /* dst_mask */
564          false),                /* pcrel_offset */
565
566   /* Low 16 bits of displacement in global offset table.  */
567   /* FIXME: Not handled correctly.  */
568   HOWTO (R_MIPS_GOT_LO16,       /* type */
569          0,                     /* rightshift */
570          2,                     /* size (0 = byte, 1 = short, 2 = long) */
571          16,                    /* bitsize */
572          false,                 /* pc_relative */
573          0,                     /* bitpos */
574          complain_overflow_dont, /* complain_on_overflow */
575          bfd_elf_generic_reloc, /* special_function */
576          "R_MIPS_GOT_LO16",     /* name */
577          true,                  /* partial_inplace */
578          0x0000ffff,            /* src_mask */
579          0x0000ffff,            /* dst_mask */
580          false),                /* pcrel_offset */
581
582   /* 64 bit substraction.  */
583   /* FIXME: Not handled correctly.  */
584   HOWTO (R_MIPS_SUB,            /* type */
585          0,                     /* rightshift */
586          4,                     /* size (0 = byte, 1 = short, 2 = long) */
587          64,                    /* bitsize */
588          false,                 /* pc_relative */
589          0,                     /* bitpos */
590          complain_overflow_dont, /* complain_on_overflow */
591          bfd_elf_generic_reloc, /* special_function */
592          "R_MIPS_SUB",          /* name */
593          true,                  /* partial_inplace */
594          MINUS_ONE,             /* src_mask */
595          MINUS_ONE,             /* dst_mask */
596          false),                /* pcrel_offset */
597
598   /* Insert the addend as an instruction.  */
599   /* FIXME: Not handled correctly.  */
600   HOWTO (R_MIPS_INSERT_A,       /* type */
601          0,                     /* rightshift */
602          2,                     /* size (0 = byte, 1 = short, 2 = long) */
603          32,                    /* bitsize */
604          false,                 /* pc_relative */
605          0,                     /* bitpos */
606          complain_overflow_dont, /* complain_on_overflow */
607          bfd_elf_generic_reloc, /* special_function */
608          "R_MIPS_INSERT_A",     /* name */
609          true,                  /* partial_inplace */
610          0xffffffff,            /* src_mask */
611          0xffffffff,            /* dst_mask */
612          false),                /* pcrel_offset */
613
614   /* Insert the addend as an instruction, and change all relocations
615      to refer to the old instruction at the address.  */
616   /* FIXME: Not handled correctly.  */
617   HOWTO (R_MIPS_INSERT_B,       /* type */
618          0,                     /* rightshift */
619          2,                     /* size (0 = byte, 1 = short, 2 = long) */
620          32,                    /* bitsize */
621          false,                 /* pc_relative */
622          0,                     /* bitpos */
623          complain_overflow_dont, /* complain_on_overflow */
624          bfd_elf_generic_reloc, /* special_function */
625          "R_MIPS_INSERT_B",     /* name */
626          true,                  /* partial_inplace */
627          0xffffffff,            /* src_mask */
628          0xffffffff,            /* dst_mask */
629          false),                /* pcrel_offset */
630
631   /* Delete a 32 bit instruction.  */
632   /* FIXME: Not handled correctly.  */
633   HOWTO (R_MIPS_DELETE,         /* type */
634          0,                     /* rightshift */
635          2,                     /* size (0 = byte, 1 = short, 2 = long) */
636          32,                    /* bitsize */
637          false,                 /* pc_relative */
638          0,                     /* bitpos */
639          complain_overflow_dont, /* complain_on_overflow */
640          bfd_elf_generic_reloc, /* special_function */
641          "R_MIPS_DELETE",       /* name */
642          true,                  /* partial_inplace */
643          0xffffffff,            /* src_mask */
644          0xffffffff,            /* dst_mask */
645          false),                /* pcrel_offset */
646
647   /* Get the higher value of a 64 bit addend.  */
648   HOWTO (R_MIPS_HIGHER,         /* type */
649          0,                     /* rightshift */
650          2,                     /* size (0 = byte, 1 = short, 2 = long) */
651          16,                    /* bitsize */
652          false,                 /* pc_relative */
653          0,                     /* bitpos */
654          complain_overflow_dont, /* complain_on_overflow */
655          mips_elf64_higher_reloc, /* special_function */
656          "R_MIPS_HIGHER",       /* name */
657          true,                  /* partial_inplace */
658          0x0000ffff,            /* src_mask */
659          0x0000ffff,            /* dst_mask */
660          false),                /* pcrel_offset */
661
662   /* Get the highest value of a 64 bit addend.  */
663   HOWTO (R_MIPS_HIGHEST,        /* type */
664          0,                     /* rightshift */
665          2,                     /* size (0 = byte, 1 = short, 2 = long) */
666          16,                    /* bitsize */
667          false,                 /* pc_relative */
668          0,                     /* bitpos */
669          complain_overflow_dont, /* complain_on_overflow */
670          mips_elf64_highest_reloc, /* special_function */
671          "R_MIPS_HIGHEST",      /* name */
672          true,                  /* partial_inplace */
673          0x0000ffff,            /* src_mask */
674          0x0000ffff,            /* dst_mask */
675          false),                /* pcrel_offset */
676
677   /* High 16 bits of displacement in global offset table.  */
678   /* FIXME: Not handled correctly.  */
679   HOWTO (R_MIPS_CALL_HI16,      /* type */
680          0,                     /* rightshift */
681          2,                     /* size (0 = byte, 1 = short, 2 = long) */
682          16,                    /* bitsize */
683          false,                 /* pc_relative */
684          0,                     /* bitpos */
685          complain_overflow_dont, /* complain_on_overflow */
686          bfd_elf_generic_reloc, /* special_function */
687          "R_MIPS_CALL_HI16",    /* name */
688          true,                  /* partial_inplace */
689          0x0000ffff,            /* src_mask */
690          0x0000ffff,            /* dst_mask */
691          false),                /* pcrel_offset */
692
693   /* Low 16 bits of displacement in global offset table.  */
694   /* FIXME: Not handled correctly.  */
695   HOWTO (R_MIPS_CALL_LO16,      /* type */
696          0,                     /* rightshift */
697          2,                     /* size (0 = byte, 1 = short, 2 = long) */
698          16,                    /* bitsize */
699          false,                 /* pc_relative */
700          0,                     /* bitpos */
701          complain_overflow_dont, /* complain_on_overflow */
702          bfd_elf_generic_reloc, /* special_function */
703          "R_MIPS_CALL_LO16",    /* name */
704          true,                  /* partial_inplace */
705          0x0000ffff,            /* src_mask */
706          0x0000ffff,            /* dst_mask */
707          false),                /* pcrel_offset */
708
709   /* Section displacement, used by an associated event location section.  */
710   /* FIXME: Not handled correctly.  */
711   HOWTO (R_MIPS_SCN_DISP,       /* type */
712          0,                     /* rightshift */
713          2,                     /* size (0 = byte, 1 = short, 2 = long) */
714          32,                    /* bitsize */
715          false,                 /* pc_relative */
716          0,                     /* bitpos */
717          complain_overflow_dont, /* complain_on_overflow */
718          bfd_elf_generic_reloc, /* special_function */
719          "R_MIPS_SCN_DISP",     /* name */
720          true,                  /* partial_inplace */
721          0xffffffff,            /* src_mask */
722          0xffffffff,            /* dst_mask */
723          false),                /* pcrel_offset */
724
725   HOWTO (R_MIPS_REL16,          /* type */
726          0,                     /* rightshift */
727          1,                     /* size (0 = byte, 1 = short, 2 = long) */
728          16,                    /* bitsize */
729          false,                 /* pc_relative */
730          0,                     /* bitpos */
731          complain_overflow_signed, /* complain_on_overflow */
732          bfd_elf_generic_reloc, /* special_function */
733          "R_MIPS_REL16",        /* name */
734          true,                  /* partial_inplace */
735          0xffff,                /* src_mask */
736          0xffff,                /* dst_mask */
737          false),                /* pcrel_offset */
738
739   /* These two are obsolete.  */
740   EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
741   EMPTY_HOWTO (R_MIPS_PJUMP),
742
743   /* Similiar to R_MIPS_REL32, but used for relocations in a GOT section.
744      It must be used for multigot GOT's (and only there).  */
745   HOWTO (R_MIPS_RELGOT,         /* type */
746          0,                     /* rightshift */
747          2,                     /* size (0 = byte, 1 = short, 2 = long) */
748          32,                    /* bitsize */
749          false,                 /* pc_relative */
750          0,                     /* bitpos */
751          complain_overflow_dont, /* complain_on_overflow */
752          bfd_elf_generic_reloc, /* special_function */
753          "R_MIPS_RELGOT",       /* name */
754          true,                  /* partial_inplace */
755          0xffffffff,            /* src_mask */
756          0xffffffff,            /* dst_mask */
757          false),                /* pcrel_offset */
758
759   /* Protected jump conversion.  This is an optimization hint.  No
760      relocation is required for correctness.  */
761   HOWTO (R_MIPS_JALR,           /* type */
762          0,                     /* rightshift */
763          2,                     /* size (0 = byte, 1 = short, 2 = long) */
764          32,                    /* bitsize */
765          false,                 /* pc_relative */
766          0,                     /* bitpos */
767          complain_overflow_dont, /* complain_on_overflow */
768          bfd_elf_generic_reloc, /* special_function */
769          "R_MIPS_JALR",         /* name */
770          false,                 /* partial_inplace */
771          0,                     /* src_mask */
772          0x00000000,            /* dst_mask */
773          false),                /* pcrel_offset */
774 };
775
776 /* The relocation table used for SHT_RELA sections.  */
777
778 static reloc_howto_type mips_elf64_howto_table_rela[] =
779 {
780   /* No relocation.  */
781   HOWTO (R_MIPS_NONE,           /* type */
782          0,                     /* rightshift */
783          0,                     /* size (0 = byte, 1 = short, 2 = long) */
784          0,                     /* bitsize */
785          false,                 /* pc_relative */
786          0,                     /* bitpos */
787          complain_overflow_dont, /* complain_on_overflow */
788          bfd_elf_generic_reloc, /* special_function */
789          "R_MIPS_NONE",         /* name */
790          false,                 /* partial_inplace */
791          0,                     /* src_mask */
792          0,                     /* dst_mask */
793          false),                /* pcrel_offset */
794
795   /* 16 bit relocation.  */
796   HOWTO (R_MIPS_16,             /* type */
797          0,                     /* rightshift */
798          2,                     /* size (0 = byte, 1 = short, 2 = long) */
799          16,                    /* bitsize */
800          false,                 /* pc_relative */
801          0,                     /* bitpos */
802          complain_overflow_signed, /* complain_on_overflow */
803          bfd_elf_generic_reloc, /* special_function */
804          "R_MIPS_16",           /* name */
805          false,                 /* partial_inplace */
806          0,                     /* src_mask */
807          0x0000ffff,            /* dst_mask */
808          false),                /* pcrel_offset */
809
810   /* 32 bit relocation.  */
811   HOWTO (R_MIPS_32,             /* type */
812          0,                     /* rightshift */
813          2,                     /* size (0 = byte, 1 = short, 2 = long) */
814          32,                    /* bitsize */
815          false,                 /* pc_relative */
816          0,                     /* bitpos */
817          complain_overflow_dont, /* complain_on_overflow */
818          bfd_elf_generic_reloc, /* special_function */
819          "R_MIPS_32",           /* name */
820          false,                 /* partial_inplace */
821          0,                     /* src_mask */
822          0xffffffff,            /* dst_mask */
823          false),                /* pcrel_offset */
824
825   /* 32 bit symbol relative relocation.  */
826   HOWTO (R_MIPS_REL32,          /* type */
827          0,                     /* rightshift */
828          2,                     /* size (0 = byte, 1 = short, 2 = long) */
829          32,                    /* bitsize */
830          false,                 /* pc_relative */
831          0,                     /* bitpos */
832          complain_overflow_dont, /* complain_on_overflow */
833          bfd_elf_generic_reloc, /* special_function */
834          "R_MIPS_REL32",        /* name */
835          false,                 /* partial_inplace */
836          0,                     /* src_mask */
837          0xffffffff,            /* dst_mask */
838          false),                /* pcrel_offset */
839
840   /* 26 bit jump address.  */
841   HOWTO (R_MIPS_26,             /* type */
842          2,                     /* rightshift */
843          2,                     /* size (0 = byte, 1 = short, 2 = long) */
844          26,                    /* bitsize */
845          false,                 /* pc_relative */
846          0,                     /* bitpos */
847          complain_overflow_dont, /* complain_on_overflow */
848                                 /* This needs complex overflow
849                                    detection, because the upper 36
850                                    bits must match the PC + 4.  */
851          bfd_elf_generic_reloc, /* special_function */
852          "R_MIPS_26",           /* name */
853          false,                 /* partial_inplace */
854          0,                     /* src_mask */
855          0x03ffffff,            /* dst_mask */
856          false),                /* pcrel_offset */
857
858   /* R_MIPS_HI16 and R_MIPS_LO16 are unsupported for 64 bit REL.  */
859   /* High 16 bits of symbol value.  */
860   HOWTO (R_MIPS_HI16,           /* type */
861          0,                     /* rightshift */
862          2,                     /* size (0 = byte, 1 = short, 2 = long) */
863          16,                    /* bitsize */
864          false,                 /* pc_relative */
865          0,                     /* bitpos */
866          complain_overflow_dont, /* complain_on_overflow */
867          bfd_elf_generic_reloc, /* special_function */
868          "R_MIPS_HI16",         /* name */
869          false,                 /* partial_inplace */
870          0,                     /* src_mask */
871          0x0000ffff,            /* dst_mask */
872          false),                /* pcrel_offset */
873
874   /* Low 16 bits of symbol value.  */
875   HOWTO (R_MIPS_LO16,           /* type */
876          0,                     /* rightshift */
877          2,                     /* size (0 = byte, 1 = short, 2 = long) */
878          16,                    /* bitsize */
879          false,                 /* pc_relative */
880          0,                     /* bitpos */
881          complain_overflow_dont, /* complain_on_overflow */
882          bfd_elf_generic_reloc, /* special_function */
883          "R_MIPS_LO16",         /* name */
884          false,                 /* partial_inplace */
885          0,                     /* src_mask */
886          0x0000ffff,            /* dst_mask */
887          false),                /* pcrel_offset */
888
889   /* GP relative reference.  */
890   HOWTO (R_MIPS_GPREL16,        /* type */
891          0,                     /* rightshift */
892          2,                     /* size (0 = byte, 1 = short, 2 = long) */
893          16,                    /* bitsize */
894          false,                 /* pc_relative */
895          0,                     /* bitpos */
896          complain_overflow_signed, /* complain_on_overflow */
897          mips_elf64_gprel16_reloca, /* special_function */
898          "R_MIPS_GPREL16",      /* name */
899          false,                 /* partial_inplace */
900          0,                     /* src_mask */
901          0x0000ffff,            /* dst_mask */
902          false),                /* pcrel_offset */
903
904   /* Reference to literal section.  */
905   HOWTO (R_MIPS_LITERAL,        /* type */
906          0,                     /* rightshift */
907          2,                     /* size (0 = byte, 1 = short, 2 = long) */
908          16,                    /* bitsize */
909          false,                 /* pc_relative */
910          0,                     /* bitpos */
911          complain_overflow_signed, /* complain_on_overflow */
912          mips_elf64_literal_reloc, /* special_function */
913          "R_MIPS_LITERAL",      /* name */
914          false,                 /* partial_inplace */
915          0,                     /* src_mask */
916          0x0000ffff,            /* dst_mask */
917          false),                /* pcrel_offset */
918
919   /* Reference to global offset table.  */
920   /* FIXME: This is not handled correctly.  */
921   HOWTO (R_MIPS_GOT16,          /* type */
922          0,                     /* rightshift */
923          2,                     /* size (0 = byte, 1 = short, 2 = long) */
924          16,                    /* bitsize */
925          false,                 /* pc_relative */
926          0,                     /* bitpos */
927          complain_overflow_signed, /* complain_on_overflow */
928          bfd_elf_generic_reloc, /* special_function */
929          "R_MIPS_GOT16",        /* name */
930          false,                 /* partial_inplace */
931          0,                     /* src_mask */
932          0x0000ffff,            /* dst_mask */
933          false),                /* pcrel_offset */
934
935   /* 16 bit PC relative reference.  */
936   HOWTO (R_MIPS_PC16,           /* type */
937          0,                     /* rightshift */
938          2,                     /* size (0 = byte, 1 = short, 2 = long) */
939          16,                    /* bitsize */
940          true,                  /* pc_relative */
941          0,                     /* bitpos */
942          complain_overflow_signed, /* complain_on_overflow */
943          bfd_elf_generic_reloc, /* special_function */
944          "R_MIPS_PC16",         /* name */
945          false,                 /* partial_inplace */
946          0,                     /* src_mask */
947          0x0000ffff,            /* dst_mask */
948          true),                 /* pcrel_offset */
949
950   /* 16 bit call through global offset table.  */
951   /* FIXME: This is not handled correctly.  */
952   HOWTO (R_MIPS_CALL16,         /* type */
953          0,                     /* rightshift */
954          2,                     /* size (0 = byte, 1 = short, 2 = long) */
955          16,                    /* bitsize */
956          false,                 /* pc_relative */
957          0,                     /* bitpos */
958          complain_overflow_signed, /* complain_on_overflow */
959          bfd_elf_generic_reloc, /* special_function */
960          "R_MIPS_CALL16",       /* name */
961          false,                 /* partial_inplace */
962          0,                     /* src_mask */
963          0x0000ffff,            /* dst_mask */
964          false),                /* pcrel_offset */
965
966   /* 32 bit GP relative reference.  */
967   HOWTO (R_MIPS_GPREL32,        /* type */
968          0,                     /* rightshift */
969          2,                     /* size (0 = byte, 1 = short, 2 = long) */
970          32,                    /* bitsize */
971          false,                 /* pc_relative */
972          0,                     /* bitpos */
973          complain_overflow_dont, /* complain_on_overflow */
974          mips_elf64_gprel32_reloc, /* special_function */
975          "R_MIPS_GPREL32",      /* name */
976          false,                 /* partial_inplace */
977          0,                     /* src_mask */
978          0xffffffff,            /* dst_mask */
979          false),                /* pcrel_offset */
980
981   UNUSED_RELOC (13),
982   UNUSED_RELOC (14),
983   UNUSED_RELOC (15),
984
985   /* A 5 bit shift field.  */
986   HOWTO (R_MIPS_SHIFT5,         /* type */
987          0,                     /* rightshift */
988          2,                     /* size (0 = byte, 1 = short, 2 = long) */
989          5,                     /* bitsize */
990          false,                 /* pc_relative */
991          6,                     /* bitpos */
992          complain_overflow_bitfield, /* complain_on_overflow */
993          bfd_elf_generic_reloc, /* special_function */
994          "R_MIPS_SHIFT5",       /* name */
995          false,                 /* partial_inplace */
996          0,                     /* src_mask */
997          0x000007c0,            /* dst_mask */
998          false),                /* pcrel_offset */
999
1000   /* A 6 bit shift field.  */
1001   HOWTO (R_MIPS_SHIFT6,         /* type */
1002          0,                     /* rightshift */
1003          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1004          6,                     /* bitsize */
1005          false,                 /* pc_relative */
1006          6,                     /* bitpos */
1007          complain_overflow_bitfield, /* complain_on_overflow */
1008          mips_elf64_shift6_reloc, /* special_function */
1009          "R_MIPS_SHIFT6",       /* name */
1010          false,                 /* partial_inplace */
1011          0,                     /* src_mask */
1012          0x000007c4,            /* dst_mask */
1013          false),                /* pcrel_offset */
1014
1015   /* 64 bit relocation.  */
1016   HOWTO (R_MIPS_64,             /* type */
1017          0,                     /* rightshift */
1018          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1019          64,                    /* bitsize */
1020          false,                 /* pc_relative */
1021          0,                     /* bitpos */
1022          complain_overflow_dont, /* complain_on_overflow */
1023          bfd_elf_generic_reloc, /* special_function */
1024          "R_MIPS_64",           /* name */
1025          false,                 /* partial_inplace */
1026          0,                     /* src_mask */
1027          MINUS_ONE,             /* dst_mask */
1028          false),                /* pcrel_offset */
1029
1030   /* Displacement in the global offset table.  */
1031   /* FIXME: Not handled correctly.  */
1032   HOWTO (R_MIPS_GOT_DISP,       /* type */
1033          0,                     /* rightshift */
1034          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1035          16,                    /* bitsize */
1036          false,                 /* pc_relative */
1037          0,                     /* bitpos */
1038          complain_overflow_signed, /* complain_on_overflow */
1039          bfd_elf_generic_reloc, /* special_function */
1040          "R_MIPS_GOT_DISP",     /* name */
1041          false,                 /* partial_inplace */
1042          0,                     /* src_mask */
1043          0x0000ffff,            /* dst_mask */
1044          false),                /* pcrel_offset */
1045
1046   /* Displacement to page pointer in the global offset table.  */
1047   /* FIXME: Not handled correctly.  */
1048   HOWTO (R_MIPS_GOT_PAGE,       /* type */
1049          0,                     /* rightshift */
1050          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1051          16,                    /* bitsize */
1052          false,                 /* pc_relative */
1053          0,                     /* bitpos */
1054          complain_overflow_signed, /* complain_on_overflow */
1055          bfd_elf_generic_reloc, /* special_function */
1056          "R_MIPS_GOT_PAGE",     /* name */
1057          false,                 /* partial_inplace */
1058          0,                     /* src_mask */
1059          0x0000ffff,            /* dst_mask */
1060          false),                /* pcrel_offset */
1061
1062   /* Offset from page pointer in the global offset table.  */
1063   /* FIXME: Not handled correctly.  */
1064   HOWTO (R_MIPS_GOT_OFST,       /* type */
1065          0,                     /* rightshift */
1066          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1067          16,                    /* bitsize */
1068          false,                 /* pc_relative */
1069          0,                     /* bitpos */
1070          complain_overflow_signed, /* complain_on_overflow */
1071          bfd_elf_generic_reloc, /* special_function */
1072          "R_MIPS_GOT_OFST",     /* name */
1073          false,                 /* partial_inplace */
1074          0,                     /* src_mask */
1075          0x0000ffff,            /* dst_mask */
1076          false),                /* pcrel_offset */
1077
1078   /* High 16 bits of displacement in global offset table.  */
1079   /* FIXME: Not handled correctly.  */
1080   HOWTO (R_MIPS_GOT_HI16,       /* type */
1081          0,                     /* rightshift */
1082          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1083          16,                    /* bitsize */
1084          false,                 /* pc_relative */
1085          0,                     /* bitpos */
1086          complain_overflow_dont, /* complain_on_overflow */
1087          bfd_elf_generic_reloc, /* special_function */
1088          "R_MIPS_GOT_HI16",     /* name */
1089          false,                 /* partial_inplace */
1090          0,                     /* src_mask */
1091          0x0000ffff,            /* dst_mask */
1092          false),                /* pcrel_offset */
1093
1094   /* Low 16 bits of displacement in global offset table.  */
1095   /* FIXME: Not handled correctly.  */
1096   HOWTO (R_MIPS_GOT_LO16,       /* type */
1097          0,                     /* rightshift */
1098          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1099          16,                    /* bitsize */
1100          false,                 /* pc_relative */
1101          0,                     /* bitpos */
1102          complain_overflow_dont, /* complain_on_overflow */
1103          bfd_elf_generic_reloc, /* special_function */
1104          "R_MIPS_GOT_LO16",     /* name */
1105          false,                 /* partial_inplace */
1106          0,                     /* src_mask */
1107          0x0000ffff,            /* dst_mask */
1108          false),                /* pcrel_offset */
1109
1110   /* 64 bit substraction.  */
1111   /* FIXME: Not handled correctly.  */
1112   HOWTO (R_MIPS_SUB,            /* type */
1113          0,                     /* rightshift */
1114          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1115          64,                    /* bitsize */
1116          false,                 /* pc_relative */
1117          0,                     /* bitpos */
1118          complain_overflow_dont, /* complain_on_overflow */
1119          bfd_elf_generic_reloc, /* special_function */
1120          "R_MIPS_SUB",          /* name */
1121          false,                 /* partial_inplace */
1122          0,                     /* src_mask */
1123          MINUS_ONE,             /* dst_mask */
1124          false),                /* pcrel_offset */
1125
1126   /* Insert the addend as an instruction.  */
1127   /* FIXME: Not handled correctly.  */
1128   HOWTO (R_MIPS_INSERT_A,       /* type */
1129          0,                     /* rightshift */
1130          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1131          32,                    /* bitsize */
1132          false,                 /* pc_relative */
1133          0,                     /* bitpos */
1134          complain_overflow_dont, /* complain_on_overflow */
1135          bfd_elf_generic_reloc, /* special_function */
1136          "R_MIPS_INSERT_A",     /* name */
1137          false,                 /* partial_inplace */
1138          0,                     /* src_mask */
1139          0xffffffff,            /* dst_mask */
1140          false),                /* pcrel_offset */
1141
1142   /* Insert the addend as an instruction, and change all relocations
1143      to refer to the old instruction at the address.  */
1144   /* FIXME: Not handled correctly.  */
1145   HOWTO (R_MIPS_INSERT_B,       /* type */
1146          0,                     /* rightshift */
1147          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1148          32,                    /* bitsize */
1149          false,                 /* pc_relative */
1150          0,                     /* bitpos */
1151          complain_overflow_dont, /* complain_on_overflow */
1152          bfd_elf_generic_reloc, /* special_function */
1153          "R_MIPS_INSERT_B",     /* name */
1154          false,                 /* partial_inplace */
1155          0,                     /* src_mask */
1156          0xffffffff,            /* dst_mask */
1157          false),                /* pcrel_offset */
1158
1159   /* Delete a 32 bit instruction.  */
1160   /* FIXME: Not handled correctly.  */
1161   HOWTO (R_MIPS_DELETE,         /* type */
1162          0,                     /* rightshift */
1163          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1164          32,                    /* bitsize */
1165          false,                 /* pc_relative */
1166          0,                     /* bitpos */
1167          complain_overflow_dont, /* complain_on_overflow */
1168          bfd_elf_generic_reloc, /* special_function */
1169          "R_MIPS_DELETE",       /* name */
1170          false,                 /* partial_inplace */
1171          0,                     /* src_mask */
1172          0xffffffff,            /* dst_mask */
1173          false),                /* pcrel_offset */
1174
1175   /* Get the higher value of a 64 bit addend.  */
1176   HOWTO (R_MIPS_HIGHER,         /* type */
1177          0,                     /* rightshift */
1178          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1179          16,                    /* bitsize */
1180          false,                 /* pc_relative */
1181          0,                     /* bitpos */
1182          complain_overflow_dont, /* complain_on_overflow */
1183          bfd_elf_generic_reloc, /* special_function */
1184          "R_MIPS_HIGHER",       /* name */
1185          false,                 /* partial_inplace */
1186          0,                     /* src_mask */
1187          0x0000ffff,            /* dst_mask */
1188          false),                /* pcrel_offset */
1189
1190   /* Get the highest value of a 64 bit addend.  */
1191   HOWTO (R_MIPS_HIGHEST,        /* type */
1192          0,                     /* rightshift */
1193          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1194          16,                    /* bitsize */
1195          false,                 /* pc_relative */
1196          0,                     /* bitpos */
1197          complain_overflow_dont, /* complain_on_overflow */
1198          bfd_elf_generic_reloc, /* special_function */
1199          "R_MIPS_HIGHEST",      /* name */
1200          false,                 /* partial_inplace */
1201          0,                     /* src_mask */
1202          0x0000ffff,            /* dst_mask */
1203          false),                /* pcrel_offset */
1204
1205   /* High 16 bits of displacement in global offset table.  */
1206   /* FIXME: Not handled correctly.  */
1207   HOWTO (R_MIPS_CALL_HI16,      /* type */
1208          0,                     /* rightshift */
1209          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1210          16,                    /* bitsize */
1211          false,                 /* pc_relative */
1212          0,                     /* bitpos */
1213          complain_overflow_dont, /* complain_on_overflow */
1214          bfd_elf_generic_reloc, /* special_function */
1215          "R_MIPS_CALL_HI16",    /* name */
1216          false,                 /* partial_inplace */
1217          0,                     /* src_mask */
1218          0x0000ffff,            /* dst_mask */
1219          false),                /* pcrel_offset */
1220
1221   /* Low 16 bits of displacement in global offset table.  */
1222   /* FIXME: Not handled correctly.  */
1223   HOWTO (R_MIPS_CALL_LO16,      /* type */
1224          0,                     /* rightshift */
1225          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1226          16,                    /* bitsize */
1227          false,                 /* pc_relative */
1228          0,                     /* bitpos */
1229          complain_overflow_dont, /* complain_on_overflow */
1230          bfd_elf_generic_reloc, /* special_function */
1231          "R_MIPS_CALL_LO16",    /* name */
1232          false,                 /* partial_inplace */
1233          0,                     /* src_mask */
1234          0x0000ffff,            /* dst_mask */
1235          false),                /* pcrel_offset */
1236
1237   /* Section displacement, used by an associated event location section.  */
1238   /* FIXME: Not handled correctly.  */
1239   HOWTO (R_MIPS_SCN_DISP,       /* type */
1240          0,                     /* rightshift */
1241          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1242          32,                    /* bitsize */
1243          false,                 /* pc_relative */
1244          0,                     /* bitpos */
1245          complain_overflow_dont, /* complain_on_overflow */
1246          bfd_elf_generic_reloc, /* special_function */
1247          "R_MIPS_SCN_DISP",     /* name */
1248          false,                 /* partial_inplace */
1249          0,                     /* src_mask */
1250          0xffffffff,            /* dst_mask */
1251          false),                /* pcrel_offset */
1252
1253   HOWTO (R_MIPS_REL16,          /* type */
1254          0,                     /* rightshift */
1255          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1256          16,                    /* bitsize */
1257          false,                 /* pc_relative */
1258          0,                     /* bitpos */
1259          complain_overflow_signed, /* complain_on_overflow */
1260          bfd_elf_generic_reloc, /* special_function */
1261          "R_MIPS_REL16",        /* name */
1262          false,                 /* partial_inplace */
1263          0,                     /* src_mask */
1264          0xffff,                /* dst_mask */
1265          false),                /* pcrel_offset */
1266
1267   /* These two are obsolete.  */
1268   EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
1269   EMPTY_HOWTO (R_MIPS_PJUMP),
1270
1271   /* Similiar to R_MIPS_REL32, but used for relocations in a GOT section.
1272      It must be used for multigot GOT's (and only there).  */
1273   HOWTO (R_MIPS_RELGOT,         /* type */
1274          0,                     /* rightshift */
1275          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1276          32,                    /* bitsize */
1277          false,                 /* pc_relative */
1278          0,                     /* bitpos */
1279          complain_overflow_dont, /* complain_on_overflow */
1280          bfd_elf_generic_reloc, /* special_function */
1281          "R_MIPS_RELGOT",       /* name */
1282          false,                 /* partial_inplace */
1283          0,                     /* src_mask */
1284          0xffffffff,            /* dst_mask */
1285          false),                /* pcrel_offset */
1286
1287   /* Protected jump conversion.  This is an optimization hint.  No
1288      relocation is required for correctness.  */
1289   HOWTO (R_MIPS_JALR,           /* type */
1290          0,                     /* rightshift */
1291          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1292          32,                    /* bitsize */
1293          false,                 /* pc_relative */
1294          0,                     /* bitpos */
1295          complain_overflow_dont, /* complain_on_overflow */
1296          bfd_elf_generic_reloc, /* special_function */
1297          "R_MIPS_JALR",         /* name */
1298          false,                 /* partial_inplace */
1299          0,                     /* src_mask */
1300          0x00000000,            /* dst_mask */
1301          false),                /* pcrel_offset */
1302 };
1303
1304 /* Swap in a MIPS 64-bit Rel reloc.  */
1305
1306 static void
1307 mips_elf64_swap_reloc_in (abfd, src, dst)
1308      bfd *abfd;
1309      const Elf64_Mips_External_Rel *src;
1310      Elf64_Mips_Internal_Rel *dst;
1311 {
1312   dst->r_offset = H_GET_64 (abfd, src->r_offset);
1313   dst->r_sym = H_GET_32 (abfd, src->r_sym);
1314   dst->r_ssym = H_GET_8 (abfd, src->r_ssym);
1315   dst->r_type3 = H_GET_8 (abfd, src->r_type3);
1316   dst->r_type2 = H_GET_8 (abfd, src->r_type2);
1317   dst->r_type = H_GET_8 (abfd, src->r_type);
1318 }
1319
1320 /* Swap in a MIPS 64-bit Rela reloc.  */
1321
1322 static void
1323 mips_elf64_swap_reloca_in (abfd, src, dst)
1324      bfd *abfd;
1325      const Elf64_Mips_External_Rela *src;
1326      Elf64_Mips_Internal_Rela *dst;
1327 {
1328   dst->r_offset = H_GET_64 (abfd, src->r_offset);
1329   dst->r_sym = H_GET_32 (abfd, src->r_sym);
1330   dst->r_ssym = H_GET_8 (abfd, src->r_ssym);
1331   dst->r_type3 = H_GET_8 (abfd, src->r_type3);
1332   dst->r_type2 = H_GET_8 (abfd, src->r_type2);
1333   dst->r_type = H_GET_8 (abfd, src->r_type);
1334   dst->r_addend = H_GET_S64 (abfd, src->r_addend);
1335 }
1336
1337 /* Swap out a MIPS 64-bit Rel reloc.  */
1338
1339 static void
1340 mips_elf64_swap_reloc_out (abfd, src, dst)
1341      bfd *abfd;
1342      const Elf64_Mips_Internal_Rel *src;
1343      Elf64_Mips_External_Rel *dst;
1344 {
1345   H_PUT_64 (abfd, src->r_offset, dst->r_offset);
1346   H_PUT_32 (abfd, src->r_sym, dst->r_sym);
1347   H_PUT_8 (abfd, src->r_ssym, dst->r_ssym);
1348   H_PUT_8 (abfd, src->r_type3, dst->r_type3);
1349   H_PUT_8 (abfd, src->r_type2, dst->r_type2);
1350   H_PUT_8 (abfd, src->r_type, dst->r_type);
1351 }
1352
1353 /* Swap out a MIPS 64-bit Rela reloc.  */
1354
1355 static void
1356 mips_elf64_swap_reloca_out (abfd, src, dst)
1357      bfd *abfd;
1358      const Elf64_Mips_Internal_Rela *src;
1359      Elf64_Mips_External_Rela *dst;
1360 {
1361   H_PUT_64 (abfd, src->r_offset, dst->r_offset);
1362   H_PUT_32 (abfd, src->r_sym, dst->r_sym);
1363   H_PUT_8 (abfd, src->r_ssym, dst->r_ssym);
1364   H_PUT_8 (abfd, src->r_type3, dst->r_type3);
1365   H_PUT_8 (abfd, src->r_type2, dst->r_type2);
1366   H_PUT_8 (abfd, src->r_type, dst->r_type);
1367   H_PUT_S64 (abfd, src->r_addend, dst->r_addend);
1368 }
1369
1370 /* Swap in a MIPS 64-bit Rel reloc.  */
1371
1372 static void
1373 mips_elf64_be_swap_reloc_in (abfd, src, dst)
1374      bfd *abfd;
1375      const bfd_byte *src;
1376      Elf_Internal_Rel *dst;
1377 {
1378   Elf64_Mips_Internal_Rel mirel;
1379
1380   mips_elf64_swap_reloc_in (abfd,
1381                             (const Elf64_Mips_External_Rel *) src,
1382                             &mirel);
1383
1384   dst[0].r_offset = mirel.r_offset;
1385   dst[0].r_info = ELF64_R_INFO (mirel.r_sym, mirel.r_type);
1386   dst[1].r_offset = mirel.r_offset;
1387   dst[1].r_info = ELF64_R_INFO (mirel.r_ssym, mirel.r_type2);
1388   dst[2].r_offset = mirel.r_offset;
1389   dst[2].r_info = ELF64_R_INFO (STN_UNDEF, mirel.r_type3);
1390 }
1391
1392 /* Swap in a MIPS 64-bit Rela reloc.  */
1393
1394 static void
1395 mips_elf64_be_swap_reloca_in (abfd, src, dst)
1396      bfd *abfd;
1397      const bfd_byte *src;
1398      Elf_Internal_Rela *dst;
1399 {
1400   Elf64_Mips_Internal_Rela mirela;
1401
1402   mips_elf64_swap_reloca_in (abfd,
1403                              (const Elf64_Mips_External_Rela *) src,
1404                              &mirela);
1405
1406   dst[0].r_offset = mirela.r_offset;
1407   dst[0].r_info = ELF64_R_INFO (mirela.r_sym, mirela.r_type);
1408   dst[0].r_addend = mirela.r_addend;
1409   dst[1].r_offset = mirela.r_offset;
1410   dst[1].r_info = ELF64_R_INFO (mirela.r_ssym, mirela.r_type2);
1411   dst[1].r_addend = 0;
1412   dst[2].r_offset = mirela.r_offset;
1413   dst[2].r_info = ELF64_R_INFO (STN_UNDEF, mirela.r_type3);
1414   dst[2].r_addend = 0;
1415 }
1416
1417 /* Swap out a MIPS 64-bit Rel reloc.  */
1418
1419 static void
1420 mips_elf64_be_swap_reloc_out (abfd, src, dst)
1421      bfd *abfd;
1422      const Elf_Internal_Rel *src;
1423      bfd_byte *dst;
1424 {
1425   Elf64_Mips_Internal_Rel mirel;
1426
1427   mirel.r_offset = src[0].r_offset;
1428   BFD_ASSERT(src[0].r_offset == src[1].r_offset);
1429   BFD_ASSERT(src[0].r_offset == src[2].r_offset);
1430
1431   mirel.r_type = ELF64_MIPS_R_TYPE (src[0].r_info);
1432   mirel.r_sym = ELF64_R_SYM (src[0].r_info);
1433   mirel.r_type2 = ELF64_MIPS_R_TYPE2 (src[1].r_info);
1434   mirel.r_ssym = ELF64_MIPS_R_SSYM (src[1].r_info);
1435   mirel.r_type3 = ELF64_MIPS_R_TYPE3 (src[2].r_info);
1436
1437   mips_elf64_swap_reloc_out (abfd, &mirel,
1438                              (Elf64_Mips_External_Rel *) dst);
1439 }
1440
1441 /* Swap out a MIPS 64-bit Rela reloc.  */
1442
1443 static void
1444 mips_elf64_be_swap_reloca_out (abfd, src, dst)
1445      bfd *abfd;
1446      const Elf_Internal_Rela *src;
1447      bfd_byte *dst;
1448 {
1449   Elf64_Mips_Internal_Rela mirela;
1450
1451   mirela.r_offset = src[0].r_offset;
1452   BFD_ASSERT(src[0].r_offset == src[1].r_offset);
1453   BFD_ASSERT(src[0].r_offset == src[2].r_offset);
1454
1455   mirela.r_type = ELF64_MIPS_R_TYPE (src[0].r_info);
1456   mirela.r_sym = ELF64_R_SYM (src[0].r_info);
1457   mirela.r_addend = src[0].r_addend;
1458   BFD_ASSERT(src[1].r_addend == 0);
1459   BFD_ASSERT(src[2].r_addend == 0);
1460
1461   mirela.r_type2 = ELF64_MIPS_R_TYPE2 (src[1].r_info);
1462   mirela.r_ssym = ELF64_MIPS_R_SSYM (src[1].r_info);
1463   mirela.r_type3 = ELF64_MIPS_R_TYPE3 (src[2].r_info);
1464
1465   mips_elf64_swap_reloca_out (abfd, &mirela,
1466                               (Elf64_Mips_External_Rela *) dst);
1467 }
1468
1469 /* Calculate the %high function.  */
1470
1471 static bfd_vma
1472 mips_elf64_high (value)
1473      bfd_vma value;
1474 {
1475   return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
1476 }
1477
1478 /* Calculate the %higher function.  */
1479
1480 static bfd_vma
1481 mips_elf64_higher (value)
1482      bfd_vma value;
1483 {
1484   return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
1485 }
1486
1487 /* Calculate the %highest function.  */
1488
1489 static bfd_vma 
1490 mips_elf64_highest (value)
1491      bfd_vma value;
1492 {
1493   return ((value + (bfd_vma) 0x800080008000) >> 48) & 0xffff;
1494 }
1495
1496 /* Do a R_MIPS_HI16 relocation.  */
1497
1498 bfd_reloc_status_type
1499 mips_elf64_hi16_reloc (abfd,
1500                      reloc_entry,
1501                      symbol,
1502                      data,
1503                      input_section,
1504                      output_bfd,
1505                      error_message)
1506      bfd *abfd ATTRIBUTE_UNUSED;
1507      arelent *reloc_entry;
1508      asymbol *symbol;
1509      PTR data ATTRIBUTE_UNUSED;
1510      asection *input_section;
1511      bfd *output_bfd;
1512      char **error_message ATTRIBUTE_UNUSED;
1513 {
1514   /* If we're relocating, and this is an external symbol, we don't
1515      want to change anything.  */
1516   if (output_bfd != (bfd *) NULL
1517       && (symbol->flags & BSF_SECTION_SYM) == 0
1518       && (! reloc_entry->howto->partial_inplace
1519           || reloc_entry->addend == 0))
1520     {
1521       reloc_entry->address += input_section->output_offset;
1522       return bfd_reloc_ok;
1523     }
1524
1525   if (((reloc_entry->addend & 0xffff) + 0x8000) & ~0xffff)
1526     reloc_entry->addend += 0x8000;
1527
1528   return bfd_reloc_continue;
1529 }
1530
1531 /* Do a R_MIPS_HIGHER relocation.  */
1532
1533 bfd_reloc_status_type
1534 mips_elf64_higher_reloc (abfd,
1535                          reloc_entry,
1536                          symbol,
1537                          data,
1538                          input_section,
1539                          output_bfd,
1540                          error_message)
1541      bfd *abfd ATTRIBUTE_UNUSED;
1542      arelent *reloc_entry;
1543      asymbol *symbol;
1544      PTR data ATTRIBUTE_UNUSED;
1545      asection *input_section;
1546      bfd *output_bfd;
1547      char **error_message ATTRIBUTE_UNUSED;
1548 {
1549   /* If we're relocating, and this is an external symbol, we don't
1550      want to change anything.  */
1551   if (output_bfd != (bfd *) NULL
1552       && (symbol->flags & BSF_SECTION_SYM) == 0
1553       && (! reloc_entry->howto->partial_inplace
1554           || reloc_entry->addend == 0))
1555     {
1556       reloc_entry->address += input_section->output_offset;
1557       return bfd_reloc_ok;
1558     }
1559
1560   if (((reloc_entry->addend & 0xffffffff) + 0x80008000)
1561       & ~0xffffffff)
1562     reloc_entry->addend += 0x80008000;
1563
1564   return bfd_reloc_continue;
1565 }
1566
1567 /* Do a R_MIPS_HIGHEST relocation.  */
1568
1569 bfd_reloc_status_type
1570 mips_elf64_highest_reloc (abfd,
1571                           reloc_entry,
1572                           symbol,
1573                           data,
1574                           input_section,
1575                           output_bfd,
1576                           error_message)
1577      bfd *abfd ATTRIBUTE_UNUSED;
1578      arelent *reloc_entry;
1579      asymbol *symbol;
1580      PTR data ATTRIBUTE_UNUSED;
1581      asection *input_section;
1582      bfd *output_bfd;
1583      char **error_message ATTRIBUTE_UNUSED;
1584 {
1585   /* If we're relocating, and this is an external symbol, we don't
1586      want to change anything.  */
1587   if (output_bfd != (bfd *) NULL
1588       && (symbol->flags & BSF_SECTION_SYM) == 0
1589       && (! reloc_entry->howto->partial_inplace
1590           || reloc_entry->addend == 0))
1591     {
1592       reloc_entry->address += input_section->output_offset;
1593       return bfd_reloc_ok;
1594     }
1595
1596   if (((reloc_entry->addend & 0xffffffffffff) + 0x800080008000)
1597       & ~0xffffffffffff)
1598     reloc_entry->addend += 0x800080008000;
1599
1600   return bfd_reloc_continue;
1601 }
1602
1603 /* Do a R_MIPS_GOT16 reloc.  This is a reloc against the global offset
1604    table used for PIC code.  If the symbol is an external symbol, the
1605    instruction is modified to contain the offset of the appropriate
1606    entry in the global offset table.  If the symbol is a section
1607    symbol, the next reloc is a R_MIPS_LO16 reloc.  The two 16 bit
1608    addends are combined to form the real addend against the section
1609    symbol; the GOT16 is modified to contain the offset of an entry in
1610    the global offset table, and the LO16 is modified to offset it
1611    appropriately.  Thus an offset larger than 16 bits requires a
1612    modified value in the global offset table.
1613
1614    This implementation suffices for the assembler, but the linker does
1615    not yet know how to create global offset tables.  */
1616
1617 bfd_reloc_status_type
1618 mips_elf64_got16_reloc (abfd,
1619                       reloc_entry,
1620                       symbol,
1621                       data,
1622                       input_section,
1623                       output_bfd,
1624                       error_message)
1625      bfd *abfd;
1626      arelent *reloc_entry;
1627      asymbol *symbol;
1628      PTR data;
1629      asection *input_section;
1630      bfd *output_bfd;
1631      char **error_message;
1632 {
1633   /* If we're relocating, and this an external symbol, we don't want
1634      to change anything.  */
1635   if (output_bfd != (bfd *) NULL
1636       && (symbol->flags & BSF_SECTION_SYM) == 0
1637       && reloc_entry->addend == 0)
1638     {
1639       reloc_entry->address += input_section->output_offset;
1640       return bfd_reloc_ok;
1641     }
1642
1643   /* If we're relocating, and this is a local symbol, we can handle it
1644      just like HI16.  */
1645   if (output_bfd != (bfd *) NULL
1646       && (symbol->flags & BSF_SECTION_SYM) != 0)
1647     return mips_elf64_hi16_reloc (abfd, reloc_entry, symbol, data,
1648                                   input_section, output_bfd, error_message);
1649
1650   abort ();
1651 }
1652
1653 /* Set the GP value for OUTPUT_BFD.  Returns false if this is a
1654    dangerous relocation.  */
1655
1656 static boolean
1657 mips_elf64_assign_gp (output_bfd, pgp)
1658      bfd *output_bfd;
1659      bfd_vma *pgp;
1660 {
1661   unsigned int count;
1662   asymbol **sym;
1663   unsigned int i;
1664
1665   /* If we've already figured out what GP will be, just return it.  */
1666   *pgp = _bfd_get_gp_value (output_bfd);
1667   if (*pgp)
1668     return true;
1669
1670   count = bfd_get_symcount (output_bfd);
1671   sym = bfd_get_outsymbols (output_bfd);
1672
1673   /* The linker script will have created a symbol named `_gp' with the
1674      appropriate value.  */
1675   if (sym == (asymbol **) NULL)
1676     i = count;
1677   else
1678     {
1679       for (i = 0; i < count; i++, sym++)
1680         {
1681           register CONST char *name;
1682
1683           name = bfd_asymbol_name (*sym);
1684           if (*name == '_' && strcmp (name, "_gp") == 0)
1685             {
1686               *pgp = bfd_asymbol_value (*sym);
1687               _bfd_set_gp_value (output_bfd, *pgp);
1688               break;
1689             }
1690         }
1691     }
1692
1693   if (i >= count)
1694     {
1695       /* Only get the error once.  */
1696       *pgp = 4;
1697       _bfd_set_gp_value (output_bfd, *pgp);
1698       return false;
1699     }
1700
1701   return true;
1702 }
1703
1704 /* We have to figure out the gp value, so that we can adjust the
1705    symbol value correctly.  We look up the symbol _gp in the output
1706    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
1707    target data.  We don't need to adjust the symbol value for an
1708    external symbol if we are producing relocateable output.  */
1709
1710 static bfd_reloc_status_type
1711 mips_elf64_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1712      bfd *output_bfd;
1713      asymbol *symbol;
1714      boolean relocateable;
1715      char **error_message;
1716      bfd_vma *pgp;
1717 {
1718   if (bfd_is_und_section (symbol->section)
1719       && ! relocateable)
1720     {
1721       *pgp = 0;
1722       return bfd_reloc_undefined;
1723     }
1724
1725   *pgp = _bfd_get_gp_value (output_bfd);
1726   if (*pgp == 0
1727       && (! relocateable
1728           || (symbol->flags & BSF_SECTION_SYM) != 0))
1729     {
1730       if (relocateable)
1731         {
1732           /* Make up a value.  */
1733           *pgp = symbol->section->output_section->vma + 0x4000;
1734           _bfd_set_gp_value (output_bfd, *pgp);
1735         }
1736       else if (!mips_elf64_assign_gp (output_bfd, pgp))
1737         {
1738           *error_message =
1739             (char *) _("GP relative relocation when _gp not defined");
1740           return bfd_reloc_dangerous;
1741         }
1742     }
1743
1744   return bfd_reloc_ok;
1745 }
1746
1747 /* Do a R_MIPS_GPREL16 relocation.  This is a 16 bit value which must
1748    become the offset from the gp register.  */
1749
1750 bfd_reloc_status_type
1751 mips_elf64_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1752                           output_bfd, error_message)
1753      bfd *abfd;
1754      arelent *reloc_entry;
1755      asymbol *symbol;
1756      PTR data;
1757      asection *input_section;
1758      bfd *output_bfd;
1759      char **error_message;
1760 {
1761   boolean relocateable;
1762   bfd_reloc_status_type ret;
1763   bfd_vma gp;
1764
1765   /* If we're relocating, and this is an external symbol with no
1766      addend, we don't want to change anything.  We will only have an
1767      addend if this is a newly created reloc, not read from an ELF
1768      file.  */
1769   if (output_bfd != (bfd *) NULL
1770       && (symbol->flags & BSF_SECTION_SYM) == 0
1771       && reloc_entry->addend == 0)
1772     {
1773       reloc_entry->address += input_section->output_offset;
1774       return bfd_reloc_ok;
1775     }
1776
1777   if (output_bfd != (bfd *) NULL)
1778     relocateable = true;
1779   else
1780     {
1781       relocateable = false;
1782       output_bfd = symbol->section->output_section->owner;
1783     }
1784
1785   ret = mips_elf64_final_gp (output_bfd, symbol, relocateable, error_message,
1786                              &gp);
1787   if (ret != bfd_reloc_ok)
1788     return ret;
1789
1790   return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1791                           relocateable, data, gp);
1792 }
1793
1794 static bfd_reloc_status_type
1795 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1796                  gp)
1797      bfd *abfd;
1798      asymbol *symbol;
1799      arelent *reloc_entry;
1800      asection *input_section;
1801      boolean relocateable;
1802      PTR data;
1803      bfd_vma gp;
1804 {
1805   bfd_vma relocation;
1806   unsigned long insn;
1807   unsigned long val;
1808
1809   if (bfd_is_com_section (symbol->section))
1810     relocation = 0;
1811   else
1812     relocation = symbol->value;
1813
1814   relocation += symbol->section->output_section->vma;
1815   relocation += symbol->section->output_offset;
1816
1817   if (reloc_entry->address > input_section->_cooked_size)
1818     return bfd_reloc_outofrange;
1819
1820   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1821
1822   /* Set val to the offset into the section or symbol.  */
1823   if (reloc_entry->howto->src_mask == 0)
1824     {
1825       /* This case occurs with the 64-bit MIPS ELF ABI.  */
1826       val = reloc_entry->addend;
1827     }
1828   else
1829     {
1830       val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1831       if (val & 0x8000)
1832         val -= 0x10000;
1833     }
1834
1835   /* Adjust val for the final section location and GP value.  If we
1836      are producing relocateable output, we don't want to do this for
1837      an external symbol.  */
1838   if (! relocateable
1839       || (symbol->flags & BSF_SECTION_SYM) != 0)
1840     val += relocation - gp;
1841
1842   insn = (insn & ~0xffff) | (val & 0xffff);
1843   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1844
1845   if (relocateable)
1846     reloc_entry->address += input_section->output_offset;
1847
1848   else if ((long) val >= 0x8000 || (long) val < -0x8000)
1849     return bfd_reloc_overflow;
1850
1851   return bfd_reloc_ok;
1852 }
1853
1854 /* Do a R_MIPS_GPREL16 RELA relocation.  */
1855
1856 bfd_reloc_status_type
1857 mips_elf64_gprel16_reloca (abfd, reloc_entry, symbol, data, input_section,
1858                            output_bfd, error_message)
1859      bfd *abfd;
1860      arelent *reloc_entry;
1861      asymbol *symbol;
1862      PTR data ATTRIBUTE_UNUSED;
1863      asection *input_section;
1864      bfd *output_bfd;
1865      char **error_message;
1866 {
1867   boolean relocateable;
1868   bfd_vma gp;
1869
1870   /* This works only for NewABI.  */
1871   BFD_ASSERT (reloc_entry->howto->src_mask == 0);
1872
1873   /* If we're relocating, and this is an external symbol with no
1874      addend, we don't want to change anything.  We will only have an
1875      addend if this is a newly created reloc, not read from an ELF
1876      file.  */
1877   if (output_bfd != (bfd *) NULL
1878       && (symbol->flags & BSF_SECTION_SYM) == 0
1879       && reloc_entry->addend == 0)
1880     {
1881       reloc_entry->address += input_section->output_offset;
1882       return bfd_reloc_ok;
1883     }
1884
1885   if (output_bfd != (bfd *) NULL)
1886     relocateable = true;
1887   else
1888     {
1889       relocateable = false;
1890       output_bfd = symbol->section->output_section->owner;
1891     }
1892
1893   if (prev_reloc_address != reloc_entry->address)
1894     prev_reloc_address = reloc_entry->address;
1895   else
1896     {
1897       mips_elf64_final_gp (output_bfd, symbol, relocateable, error_message,
1898                            &gp);
1899       prev_reloc_addend = reloc_entry->addend + reloc_entry->address - gp;
1900       if (symbol->flags & BSF_LOCAL)
1901         prev_reloc_addend += _bfd_get_gp_value (abfd);
1902 /*fprintf(stderr, "Addend: %lx, Next Addend: %lx\n", reloc_entry->addend, prev_reloc_addend);*/
1903     }
1904
1905   return bfd_reloc_ok;
1906 }
1907
1908 /* Do a R_MIPS_LITERAL relocation.  */
1909
1910 bfd_reloc_status_type
1911 mips_elf64_literal_reloc (abfd, reloc_entry, symbol, data, input_section,
1912                           output_bfd, error_message)
1913      bfd *abfd;
1914      arelent *reloc_entry;
1915      asymbol *symbol;
1916      PTR data;
1917      asection *input_section;
1918      bfd *output_bfd;
1919      char **error_message;
1920 {
1921   /* If we're relocating, and this is an external symbol, we don't
1922      want to change anything.  */
1923   if (output_bfd != (bfd *) NULL
1924       && (symbol->flags & BSF_SECTION_SYM) == 0
1925       && (! reloc_entry->howto->partial_inplace
1926           || reloc_entry->addend == 0))
1927     {
1928       reloc_entry->address += input_section->output_offset;
1929       return bfd_reloc_ok;
1930     }
1931
1932   /* FIXME: The entries in the .lit8 and .lit4 sections should be merged.
1933      Currently we simply call mips_elf64_gprel16_reloc.  */
1934   return mips_elf64_gprel16_reloc (abfd, reloc_entry, symbol, data,
1935                                    input_section, output_bfd, error_message);
1936 }
1937
1938 /* Do a R_MIPS_GPREL32 relocation.  Is this 32 bit value the offset
1939    from the gp register? XXX */
1940
1941 bfd_reloc_status_type
1942 mips_elf64_gprel32_reloc (abfd,
1943                         reloc_entry,
1944                         symbol,
1945                         data,
1946                         input_section,
1947                         output_bfd,
1948                         error_message)
1949      bfd *abfd;
1950      arelent *reloc_entry;
1951      asymbol *symbol;
1952      PTR data;
1953      asection *input_section;
1954      bfd *output_bfd;
1955      char **error_message;
1956 {
1957   boolean relocateable;
1958   bfd_reloc_status_type ret;
1959   bfd_vma gp;
1960   bfd_vma relocation;
1961   unsigned long val;
1962
1963   /* If we're relocating, and this is an external symbol with no
1964      addend, we don't want to change anything.  We will only have an
1965      addend if this is a newly created reloc, not read from an ELF
1966      file.  */
1967   if (output_bfd != (bfd *) NULL
1968       && (symbol->flags & BSF_SECTION_SYM) == 0
1969       && reloc_entry->addend == 0)
1970     {
1971       *error_message = (char *)
1972         _("32bits gp relative relocation occurs for an external symbol");
1973       return bfd_reloc_outofrange;
1974     }
1975
1976   if (output_bfd != (bfd *) NULL)
1977     {
1978       relocateable = true;
1979       gp = _bfd_get_gp_value (output_bfd);
1980     }
1981   else
1982     {
1983       relocateable = false;
1984       output_bfd = symbol->section->output_section->owner;
1985
1986       ret = mips_elf64_final_gp (output_bfd, symbol, relocateable,
1987                                  error_message, &gp);
1988       if (ret != bfd_reloc_ok)
1989         return ret;
1990     }
1991
1992   if (bfd_is_com_section (symbol->section))
1993     relocation = 0;
1994   else
1995     relocation = symbol->value;
1996
1997   relocation += symbol->section->output_section->vma;
1998   relocation += symbol->section->output_offset;
1999
2000   if (reloc_entry->address > input_section->_cooked_size)
2001     return bfd_reloc_outofrange;
2002
2003   if (reloc_entry->howto->src_mask == 0)
2004     {
2005       /* This case arises with the 64-bit MIPS ELF ABI.  */
2006       val = 0;
2007     }
2008   else
2009     val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
2010
2011   /* Set val to the offset into the section or symbol.  */
2012   val += reloc_entry->addend;
2013
2014   /* Adjust val for the final section location and GP value.  If we
2015      are producing relocateable output, we don't want to do this for
2016      an external symbol.  */
2017   if (! relocateable
2018       || (symbol->flags & BSF_SECTION_SYM) != 0)
2019     val += relocation - gp;
2020
2021   bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
2022
2023   if (relocateable)
2024     reloc_entry->address += input_section->output_offset;
2025
2026   return bfd_reloc_ok;
2027 }
2028
2029 /* Do a R_MIPS_SHIFT6 relocation. The MSB of the shift is stored at bit 2,
2030    the rest is at bits 6-10. The bitpos alredy got right by the howto.   */
2031
2032 bfd_reloc_status_type
2033 mips_elf64_shift6_reloc (abfd, reloc_entry, symbol, data, input_section,
2034                          output_bfd, error_message)
2035      bfd *abfd ATTRIBUTE_UNUSED;
2036      arelent *reloc_entry;
2037      asymbol *symbol;
2038      PTR data ATTRIBUTE_UNUSED;
2039      asection *input_section;
2040      bfd *output_bfd;
2041      char **error_message ATTRIBUTE_UNUSED;
2042 {
2043   /* If we're relocating, and this is an external symbol, we don't
2044      want to change anything.  */
2045   if (output_bfd != (bfd *) NULL
2046       && (symbol->flags & BSF_SECTION_SYM) == 0
2047       && (! reloc_entry->howto->partial_inplace
2048           || reloc_entry->addend == 0))
2049     {
2050       reloc_entry->address += input_section->output_offset;
2051       return bfd_reloc_ok;
2052     }
2053
2054   reloc_entry->addend = (reloc_entry->addend & 0x00007c0)
2055                         | (reloc_entry->addend & 0x00000800) >> 9;
2056
2057   return bfd_reloc_continue;
2058 }
2059
2060 static int
2061 mips_elf64_additional_program_headers (abfd)
2062      bfd *abfd;
2063 {
2064   int ret = 0;
2065
2066   /* See if we need a PT_MIPS_OPTIONS segment.  */
2067   if (bfd_get_section_by_name (abfd, ".MIPS.options"))
2068     ++ret;
2069
2070   return ret;
2071 }
2072
2073 /* Given a BFD reloc type, return a howto structure.  */
2074
2075 static reloc_howto_type *
2076 mips_elf64_reloc_type_lookup (abfd, code)
2077      bfd *abfd ATTRIBUTE_UNUSED;
2078      bfd_reloc_code_real_type code;
2079 {
2080   /* FIXME: We default to RELA here instead of choosing the right
2081      relocation variant.  */
2082   reloc_howto_type *howto_table = mips_elf64_howto_table_rela;
2083
2084   switch (code)
2085     {
2086     case BFD_RELOC_NONE:
2087       return &howto_table[R_MIPS_NONE];
2088     case BFD_RELOC_16:
2089       return &howto_table[R_MIPS_16];
2090     case BFD_RELOC_32:
2091       return &howto_table[R_MIPS_32];
2092     case BFD_RELOC_64:
2093     case BFD_RELOC_CTOR:
2094       /* We need to handle these specially.  Select the right
2095          relocation (R_MIPS_32 or R_MIPS_64) based on the
2096          size of addresses on this architecture.  */
2097       if (bfd_arch_bits_per_address (abfd) == 32)
2098         return &howto_table[R_MIPS_32];
2099       else
2100         return &howto_table[R_MIPS_64];
2101
2102     case BFD_RELOC_16_PCREL:
2103       return &howto_table[R_MIPS_PC16];
2104     case BFD_RELOC_HI16_S:
2105       return &howto_table[R_MIPS_HI16];
2106     case BFD_RELOC_LO16:
2107       return &howto_table[R_MIPS_LO16];
2108     case BFD_RELOC_GPREL16:
2109       return &howto_table[R_MIPS_GPREL16];
2110     case BFD_RELOC_GPREL32:
2111       return &howto_table[R_MIPS_GPREL32];
2112     case BFD_RELOC_MIPS_JMP:
2113       return &howto_table[R_MIPS_26];
2114     case BFD_RELOC_MIPS_LITERAL:
2115       return &howto_table[R_MIPS_LITERAL];
2116     case BFD_RELOC_MIPS_GOT16:
2117       return &howto_table[R_MIPS_GOT16];
2118     case BFD_RELOC_MIPS_CALL16:
2119       return &howto_table[R_MIPS_CALL16];
2120     case BFD_RELOC_MIPS_SHIFT5:
2121       return &howto_table[R_MIPS_SHIFT5];
2122     case BFD_RELOC_MIPS_SHIFT6:
2123       return &howto_table[R_MIPS_SHIFT6];
2124     case BFD_RELOC_MIPS_GOT_DISP:
2125       return &howto_table[R_MIPS_GOT_DISP];
2126     case BFD_RELOC_MIPS_GOT_PAGE:
2127       return &howto_table[R_MIPS_GOT_PAGE];
2128     case BFD_RELOC_MIPS_GOT_OFST:
2129       return &howto_table[R_MIPS_GOT_OFST];
2130     case BFD_RELOC_MIPS_GOT_HI16:
2131       return &howto_table[R_MIPS_GOT_HI16];
2132     case BFD_RELOC_MIPS_GOT_LO16:
2133       return &howto_table[R_MIPS_GOT_LO16];
2134     case BFD_RELOC_MIPS_SUB:
2135       return &howto_table[R_MIPS_SUB];
2136     case BFD_RELOC_MIPS_INSERT_A:
2137       return &howto_table[R_MIPS_INSERT_A];
2138     case BFD_RELOC_MIPS_INSERT_B:
2139       return &howto_table[R_MIPS_INSERT_B];
2140     case BFD_RELOC_MIPS_DELETE:
2141       return &howto_table[R_MIPS_DELETE];
2142     case BFD_RELOC_MIPS_HIGHEST:
2143       return &howto_table[R_MIPS_HIGHEST];
2144     case BFD_RELOC_MIPS_HIGHER:
2145       return &howto_table[R_MIPS_HIGHER];
2146     case BFD_RELOC_MIPS_CALL_HI16:
2147       return &howto_table[R_MIPS_CALL_HI16];
2148     case BFD_RELOC_MIPS_CALL_LO16:
2149       return &howto_table[R_MIPS_CALL_LO16];
2150     case BFD_RELOC_MIPS_SCN_DISP:
2151       return &howto_table[R_MIPS_SCN_DISP];
2152     case BFD_RELOC_MIPS_REL16:
2153       return &howto_table[R_MIPS_REL16];
2154     /* Use of R_MIPS_ADD_IMMEDIATE and R_MIPS_PJUMP is deprecated.  */
2155     case BFD_RELOC_MIPS_RELGOT:
2156       return &howto_table[R_MIPS_RELGOT];
2157     case BFD_RELOC_MIPS_JALR:
2158       return &howto_table[R_MIPS_JALR];
2159 /*
2160     case BFD_RELOC_MIPS16_JMP:
2161       return &elf_mips16_jump_howto;
2162     case BFD_RELOC_MIPS16_GPREL:
2163       return &elf_mips16_gprel_howto;
2164     case BFD_RELOC_VTABLE_INHERIT:
2165       return &elf_mips_gnu_vtinherit_howto;
2166     case BFD_RELOC_VTABLE_ENTRY:
2167       return &elf_mips_gnu_vtentry_howto;
2168     case BFD_RELOC_PCREL_HI16_S:
2169       return &elf_mips_gnu_rel_hi16;
2170     case BFD_RELOC_PCREL_LO16:
2171       return &elf_mips_gnu_rel_lo16;
2172     case BFD_RELOC_16_PCREL_S2:
2173       return &elf_mips_gnu_rel16_s2;
2174     case BFD_RELOC_64_PCREL:
2175       return &elf_mips_gnu_pcrel64;
2176     case BFD_RELOC_32_PCREL:
2177       return &elf_mips_gnu_pcrel32;
2178 */
2179     default:
2180       bfd_set_error (bfd_error_bad_value);
2181       return NULL;
2182     }
2183 }
2184
2185 /* Prevent relocation handling by bfd for MIPS ELF64.  */
2186
2187 static void
2188 mips_elf64_info_to_howto_rel (abfd, cache_ptr, dst)
2189      bfd *abfd ATTRIBUTE_UNUSED;
2190      arelent *cache_ptr ATTRIBUTE_UNUSED;
2191      Elf64_Internal_Rel *dst ATTRIBUTE_UNUSED;
2192 {
2193   BFD_ASSERT (0);
2194 }
2195
2196 static void
2197 mips_elf64_info_to_howto_rela (abfd, cache_ptr, dst)
2198      bfd *abfd ATTRIBUTE_UNUSED;
2199      arelent *cache_ptr ATTRIBUTE_UNUSED;
2200      Elf64_Internal_Rela *dst ATTRIBUTE_UNUSED;
2201 {
2202   BFD_ASSERT (0);
2203 }
2204
2205 /* Since each entry in an SHT_REL or SHT_RELA section can represent up
2206    to three relocs, we must tell the user to allocate more space.  */
2207
2208 static long
2209 mips_elf64_get_reloc_upper_bound (abfd, sec)
2210      bfd *abfd ATTRIBUTE_UNUSED;
2211      asection *sec;
2212 {
2213   return (sec->reloc_count * 3 + 1) * sizeof (arelent *);
2214 }
2215
2216 /* Read the relocations from one reloc section.  */
2217
2218 static boolean
2219 mips_elf64_slurp_one_reloc_table (abfd, asect, symbols, rel_hdr)
2220      bfd *abfd;
2221      asection *asect;
2222      asymbol **symbols;
2223      const Elf_Internal_Shdr *rel_hdr;
2224 {
2225   PTR allocated = NULL;
2226   bfd_byte *native_relocs;
2227   arelent *relents;
2228   arelent *relent;
2229   bfd_vma count;
2230   bfd_vma i;
2231   int entsize;
2232   reloc_howto_type *howto_table;
2233
2234   allocated = (PTR) bfd_malloc (rel_hdr->sh_size);
2235   if (allocated == NULL)
2236     return false;
2237
2238   if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
2239       || (bfd_bread (allocated, rel_hdr->sh_size, abfd) != rel_hdr->sh_size))
2240     goto error_return;
2241
2242   native_relocs = (bfd_byte *) allocated;
2243
2244   relents = asect->relocation + asect->reloc_count;
2245
2246   entsize = rel_hdr->sh_entsize;
2247   BFD_ASSERT (entsize == sizeof (Elf64_Mips_External_Rel)
2248               || entsize == sizeof (Elf64_Mips_External_Rela));
2249
2250   count = rel_hdr->sh_size / entsize;
2251
2252   if (entsize == sizeof (Elf64_Mips_External_Rel))
2253     howto_table = mips_elf64_howto_table_rel;
2254   else
2255     howto_table = mips_elf64_howto_table_rela;
2256
2257   relent = relents;
2258   for (i = 0; i < count; i++, native_relocs += entsize)
2259     {
2260       Elf64_Mips_Internal_Rela rela;
2261       boolean used_sym, used_ssym;
2262       int ir;
2263
2264       if (entsize == sizeof (Elf64_Mips_External_Rela))
2265         mips_elf64_swap_reloca_in (abfd,
2266                                    (Elf64_Mips_External_Rela *) native_relocs,
2267                                    &rela);
2268       else
2269         {
2270           Elf64_Mips_Internal_Rel rel;
2271
2272           mips_elf64_swap_reloc_in (abfd,
2273                                     (Elf64_Mips_External_Rel *) native_relocs,
2274                                     &rel);
2275           rela.r_offset = rel.r_offset;
2276           rela.r_sym = rel.r_sym;
2277           rela.r_ssym = rel.r_ssym;
2278           rela.r_type3 = rel.r_type3;
2279           rela.r_type2 = rel.r_type2;
2280           rela.r_type = rel.r_type;
2281           rela.r_addend = 0;
2282         }
2283
2284       /* Each entry represents up to three actual relocations.  */
2285
2286       used_sym = false;
2287       used_ssym = false;
2288       for (ir = 0; ir < 3; ir++)
2289         {
2290           enum elf_mips_reloc_type type;
2291
2292           switch (ir)
2293             {
2294             default:
2295               abort ();
2296             case 0:
2297               type = (enum elf_mips_reloc_type) rela.r_type;
2298               break;
2299             case 1:
2300               type = (enum elf_mips_reloc_type) rela.r_type2;
2301               break;
2302             case 2:
2303               type = (enum elf_mips_reloc_type) rela.r_type3;
2304               break;
2305             }
2306
2307           if (type == R_MIPS_NONE)
2308             {
2309               /* There are no more relocations in this entry.  If this
2310                  is the first entry, we need to generate a dummy
2311                  relocation so that the generic linker knows that
2312                  there has been a break in the sequence of relocations
2313                  applying to a particular address.  */
2314               if (ir == 0)
2315                 {
2316                   relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
2317                   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2318                     relent->address = rela.r_offset;
2319                   else
2320                     relent->address = rela.r_offset - asect->vma;
2321                   relent->addend = 0;
2322                   relent->howto = &howto_table[(int) R_MIPS_NONE];
2323                   ++relent;
2324                 }
2325               break;
2326             }
2327
2328           /* Some types require symbols, whereas some do not.  */
2329           switch (type)
2330             {
2331             case R_MIPS_NONE:
2332             case R_MIPS_LITERAL:
2333             case R_MIPS_INSERT_A:
2334             case R_MIPS_INSERT_B:
2335             case R_MIPS_DELETE:
2336               relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
2337               break;
2338
2339             default:
2340               if (! used_sym)
2341                 {
2342                   if (rela.r_sym == 0)
2343                     relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
2344                   else
2345                     {
2346                       asymbol **ps, *s;
2347
2348                       ps = symbols + rela.r_sym - 1;
2349                       s = *ps;
2350                       if ((s->flags & BSF_SECTION_SYM) == 0)
2351                         relent->sym_ptr_ptr = ps;
2352                       else
2353                         relent->sym_ptr_ptr = s->section->symbol_ptr_ptr;
2354                     }
2355
2356                   used_sym = true;
2357                 }
2358               else if (! used_ssym)
2359                 {
2360                   switch (rela.r_ssym)
2361                     {
2362                     case RSS_UNDEF:
2363                       relent->sym_ptr_ptr =
2364                         bfd_abs_section_ptr->symbol_ptr_ptr;
2365                       break;
2366
2367                     case RSS_GP:
2368                     case RSS_GP0:
2369                     case RSS_LOC:
2370                       /* FIXME: I think these need to be handled using
2371                          special howto structures.  */
2372                       BFD_ASSERT (0);
2373                       break;
2374
2375                     default:
2376                       BFD_ASSERT (0);
2377                       break;
2378                     }
2379
2380                   used_ssym = true;
2381                 }
2382               else
2383                 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
2384
2385               break;
2386             }
2387
2388           /* The address of an ELF reloc is section relative for an
2389              object file, and absolute for an executable file or
2390              shared library.  The address of a BFD reloc is always
2391              section relative.  */
2392           if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2393             relent->address = rela.r_offset;
2394           else
2395             relent->address = rela.r_offset - asect->vma;
2396
2397           relent->addend = rela.r_addend;
2398
2399           relent->howto = &howto_table[(int) type];
2400
2401           ++relent;
2402         }
2403     }
2404
2405   asect->reloc_count += relent - relents;
2406
2407   if (allocated != NULL)
2408     free (allocated);
2409
2410   return true;
2411
2412  error_return:
2413   if (allocated != NULL)
2414     free (allocated);
2415   return false;
2416 }
2417
2418 /* Read the relocations.  On Irix 6, there can be two reloc sections
2419    associated with a single data section.  */
2420
2421 static boolean
2422 mips_elf64_slurp_reloc_table (abfd, asect, symbols, dynamic)
2423      bfd *abfd;
2424      asection *asect;
2425      asymbol **symbols;
2426      boolean dynamic;
2427 {
2428   bfd_size_type amt;
2429   struct bfd_elf_section_data * const d = elf_section_data (asect);
2430
2431   if (dynamic)
2432     {
2433       bfd_set_error (bfd_error_invalid_operation);
2434       return false;
2435     }
2436
2437   if (asect->relocation != NULL
2438       || (asect->flags & SEC_RELOC) == 0
2439       || asect->reloc_count == 0)
2440     return true;
2441
2442   /* Allocate space for 3 arelent structures for each Rel structure.  */
2443   amt = asect->reloc_count;
2444   amt *= 3 * sizeof (arelent);
2445   asect->relocation = (arelent *) bfd_alloc (abfd, amt);
2446   if (asect->relocation == NULL)
2447     return false;
2448
2449   /* The slurp_one_reloc_table routine increments reloc_count.  */
2450   asect->reloc_count = 0;
2451
2452   if (! mips_elf64_slurp_one_reloc_table (abfd, asect, symbols, &d->rel_hdr))
2453     return false;
2454   if (d->rel_hdr2 != NULL)
2455     {
2456       if (! mips_elf64_slurp_one_reloc_table (abfd, asect, symbols,
2457                                               d->rel_hdr2))
2458         return false;
2459     }
2460
2461   return true;
2462 }
2463
2464 /* Write out the relocations.  */
2465
2466 static void
2467 mips_elf64_write_relocs (abfd, sec, data)
2468      bfd *abfd;
2469      asection *sec;
2470      PTR data;
2471 {
2472   boolean *failedp = (boolean *) data;
2473   int count;
2474   Elf_Internal_Shdr *rel_hdr;
2475   unsigned int idx;
2476
2477   /* If we have already failed, don't do anything.  */
2478   if (*failedp)
2479     return;
2480
2481   if ((sec->flags & SEC_RELOC) == 0)
2482     return;
2483
2484   /* The linker backend writes the relocs out itself, and sets the
2485      reloc_count field to zero to inhibit writing them here.  Also,
2486      sometimes the SEC_RELOC flag gets set even when there aren't any
2487      relocs.  */
2488   if (sec->reloc_count == 0)
2489     return;
2490
2491   /* We can combine up to three relocs that refer to the same address
2492      if the latter relocs have no associated symbol.  */
2493   count = 0;
2494   for (idx = 0; idx < sec->reloc_count; idx++)
2495     {
2496       bfd_vma addr;
2497       unsigned int i;
2498
2499       ++count;
2500
2501       addr = sec->orelocation[idx]->address;
2502       for (i = 0; i < 2; i++)
2503         {
2504           arelent *r;
2505
2506           if (idx + 1 >= sec->reloc_count)
2507             break;
2508           r = sec->orelocation[idx + 1];
2509           if (r->address != addr
2510               || ! bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
2511               || (*r->sym_ptr_ptr)->value != 0)
2512             break;
2513
2514           /* We can merge the reloc at IDX + 1 with the reloc at IDX.  */
2515
2516           ++idx;
2517         }
2518     }
2519
2520   rel_hdr = &elf_section_data (sec)->rel_hdr;
2521
2522   /* Do the actual relocation.  */
2523
2524   if (rel_hdr->sh_entsize == sizeof(Elf64_Mips_External_Rel))
2525     mips_elf64_write_rel (abfd, sec, rel_hdr, &count, data);
2526   else if (rel_hdr->sh_entsize == sizeof(Elf64_Mips_External_Rela))
2527     mips_elf64_write_rela (abfd, sec, rel_hdr, &count, data);
2528   else
2529     BFD_ASSERT (0);
2530 }
2531
2532 static void
2533 mips_elf64_write_rel (abfd, sec, rel_hdr, count, data)
2534      bfd *abfd;
2535      asection *sec;
2536      Elf_Internal_Shdr *rel_hdr;
2537      int *count;
2538      PTR data;
2539 {
2540   boolean *failedp = (boolean *) data;
2541   Elf64_Mips_External_Rel *ext_rel;
2542   unsigned int idx;
2543   asymbol *last_sym = 0;
2544   int last_sym_idx = 0;
2545
2546   rel_hdr->sh_size = (bfd_vma)(rel_hdr->sh_entsize * *count);
2547   rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size);
2548   if (rel_hdr->contents == NULL)
2549     {
2550       *failedp = true;
2551       return;
2552     }
2553
2554   ext_rel = (Elf64_Mips_External_Rel *) rel_hdr->contents;
2555   for (idx = 0; idx < sec->reloc_count; idx++, ext_rel++)
2556     {
2557       arelent *ptr;
2558       Elf64_Mips_Internal_Rel int_rel;
2559       asymbol *sym;
2560       int n;
2561       unsigned int i;
2562
2563       ptr = sec->orelocation[idx];
2564
2565       /* The address of an ELF reloc is section relative for an object
2566          file, and absolute for an executable file or shared library.
2567          The address of a BFD reloc is always section relative.  */
2568       if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2569         int_rel.r_offset = ptr->address;
2570       else
2571         int_rel.r_offset = ptr->address + sec->vma;
2572
2573       sym = *ptr->sym_ptr_ptr;
2574       if (sym == last_sym)
2575         n = last_sym_idx;
2576       else
2577         {
2578           last_sym = sym;
2579           n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
2580           if (n < 0)
2581             {
2582               *failedp = true;
2583               return;
2584             }
2585           last_sym_idx = n;
2586         }
2587
2588       int_rel.r_sym = n;
2589       int_rel.r_ssym = RSS_UNDEF;
2590
2591       if ((*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
2592           && ! _bfd_elf_validate_reloc (abfd, ptr))
2593         {
2594           *failedp = true;
2595           return;
2596         }
2597
2598       int_rel.r_type = ptr->howto->type;
2599       int_rel.r_type2 = (int) R_MIPS_NONE;
2600       int_rel.r_type3 = (int) R_MIPS_NONE;
2601
2602       for (i = 0; i < 2; i++)
2603         {
2604           arelent *r;
2605
2606           if (idx + 1 >= sec->reloc_count)
2607             break;
2608           r = sec->orelocation[idx + 1];
2609           if (r->address != ptr->address
2610               || ! bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
2611               || (*r->sym_ptr_ptr)->value != 0)
2612             break;
2613
2614           /* We can merge the reloc at IDX + 1 with the reloc at IDX.  */
2615
2616           if (i == 0)
2617             int_rel.r_type2 = r->howto->type;
2618           else
2619             int_rel.r_type3 = r->howto->type;
2620
2621           ++idx;
2622         }
2623
2624       mips_elf64_swap_reloc_out (abfd, &int_rel, ext_rel);
2625     }
2626
2627   BFD_ASSERT (ext_rel - (Elf64_Mips_External_Rel *) rel_hdr->contents
2628               == *count);
2629 }
2630
2631 static void
2632 mips_elf64_write_rela (abfd, sec, rela_hdr, count, data)
2633      bfd *abfd;
2634      asection *sec;
2635      Elf_Internal_Shdr *rela_hdr;
2636      int *count;
2637      PTR data;
2638 {
2639   boolean *failedp = (boolean *) data;
2640   Elf64_Mips_External_Rela *ext_rela;
2641   unsigned int idx;
2642   asymbol *last_sym = 0;
2643   int last_sym_idx = 0;
2644
2645   rela_hdr->sh_size = (bfd_vma)(rela_hdr->sh_entsize * *count);
2646   rela_hdr->contents = (PTR) bfd_alloc (abfd, rela_hdr->sh_size);
2647   if (rela_hdr->contents == NULL)
2648     {
2649       *failedp = true;
2650       return;
2651     }
2652
2653   ext_rela = (Elf64_Mips_External_Rela *) rela_hdr->contents;
2654   for (idx = 0; idx < sec->reloc_count; idx++, ext_rela++)
2655     {
2656       arelent *ptr;
2657       Elf64_Mips_Internal_Rela int_rela;
2658       asymbol *sym;
2659       int n;
2660       unsigned int i;
2661
2662       ptr = sec->orelocation[idx];
2663
2664       /* The address of an ELF reloc is section relative for an object
2665          file, and absolute for an executable file or shared library.
2666          The address of a BFD reloc is always section relative.  */
2667       if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2668         int_rela.r_offset = ptr->address;
2669       else
2670         int_rela.r_offset = ptr->address + sec->vma;
2671
2672       sym = *ptr->sym_ptr_ptr;
2673       if (sym == last_sym)
2674         n = last_sym_idx;
2675       else
2676         {
2677           last_sym = sym;
2678           n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
2679           if (n < 0)
2680             {
2681               *failedp = true;
2682               return;
2683             }
2684           last_sym_idx = n;
2685         }
2686
2687       int_rela.r_sym = n;
2688       int_rela.r_addend = ptr->addend;
2689       int_rela.r_ssym = RSS_UNDEF;
2690
2691       if ((*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
2692           && ! _bfd_elf_validate_reloc (abfd, ptr))
2693         {
2694           *failedp = true;
2695           return;
2696         }
2697
2698       int_rela.r_type = ptr->howto->type;
2699       int_rela.r_type2 = (int) R_MIPS_NONE;
2700       int_rela.r_type3 = (int) R_MIPS_NONE;
2701
2702       for (i = 0; i < 2; i++)
2703         {
2704           arelent *r;
2705
2706           if (idx + 1 >= sec->reloc_count)
2707             break;
2708           r = sec->orelocation[idx + 1];
2709           if (r->address != ptr->address
2710               || ! bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
2711               || (*r->sym_ptr_ptr)->value != 0)
2712             break;
2713
2714           /* We can merge the reloc at IDX + 1 with the reloc at IDX.  */
2715
2716           if (i == 0)
2717             int_rela.r_type2 = r->howto->type;
2718           else
2719             int_rela.r_type3 = r->howto->type;
2720
2721           ++idx;
2722         }
2723
2724       mips_elf64_swap_reloca_out (abfd, &int_rela, ext_rela);
2725     }
2726
2727   BFD_ASSERT (ext_rela - (Elf64_Mips_External_Rela *) rela_hdr->contents
2728               == *count);
2729 }
2730 \f
2731 /* This structure is used to hold .got information when linking.  It
2732    is stored in the tdata field of the bfd_elf_section_data structure.  */
2733
2734 struct mips_elf64_got_info
2735 {
2736   /* The global symbol in the GOT with the lowest index in the dynamic
2737      symbol table.  */
2738   struct elf_link_hash_entry *global_gotsym;
2739   /* The number of global .got entries.  */
2740   unsigned int global_gotno;
2741   /* The number of local .got entries.  */
2742   unsigned int local_gotno;
2743   /* The number of local .got entries we have used.  */
2744   unsigned int assigned_gotno;
2745 };
2746
2747 /* The MIPS ELF64 linker needs additional information for each symbol in
2748    the global hash table.  */
2749
2750 struct mips_elf64_link_hash_entry
2751 {
2752   struct elf_link_hash_entry root;
2753
2754   /* External symbol information.  */
2755   EXTR esym;
2756
2757   /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
2758      this symbol.  */ 
2759   unsigned int possibly_dynamic_relocs;
2760
2761   /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
2762      a readonly section.  */
2763   boolean readonly_reloc;
2764
2765   /* The index of the first dynamic relocation (in the .rel.dyn
2766      section) against this symbol.  */
2767   unsigned int min_dyn_reloc_index;
2768
2769   /* We must not create a stub for a symbol that has relocations
2770      related to taking the function's address, i.e. any but
2771      R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
2772      p. 4-20.  */
2773   boolean no_fn_stub;
2774
2775   /* If there is a stub that 32 bit functions should use to call this
2776      16 bit function, this points to the section containing the stub.  */
2777   asection *fn_stub;
2778
2779   /* Whether we need the fn_stub; this is set if this symbol appears
2780      in any relocs other than a 16 bit call.  */
2781   boolean need_fn_stub;
2782
2783   /* If there is a stub that 16 bit functions should use to call this
2784      32 bit function, this points to the section containing the stub.  */
2785   asection *call_stub;
2786
2787   /* This is like the call_stub field, but it is used if the function
2788      being called returns a floating point value.  */
2789   asection *call_fp_stub;
2790 };
2791 \f
2792   /* The mips16 compiler uses a couple of special sections to handle
2793      floating point arguments.
2794
2795      Section names that look like .mips16.fn.FNNAME contain stubs that
2796      copy floating point arguments from the fp regs to the gp regs and
2797      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
2798      call should be redirected to the stub instead.  If no 32 bit
2799      function calls FNNAME, the stub should be discarded.  We need to
2800      consider any reference to the function, not just a call, because
2801      if the address of the function is taken we will need the stub,
2802      since the address might be passed to a 32 bit function.
2803
2804      Section names that look like .mips16.call.FNNAME contain stubs
2805      that copy floating point arguments from the gp regs to the fp
2806      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
2807      then any 16 bit function that calls FNNAME should be redirected
2808      to the stub instead.  If FNNAME is not a 32 bit function, the
2809      stub should be discarded.
2810
2811      .mips16.call.fp.FNNAME sections are similar, but contain stubs
2812      which call FNNAME and then copy the return value from the fp regs
2813      to the gp regs.  These stubs store the return value in $18 while
2814      calling FNNAME; any function which might call one of these stubs
2815      must arrange to save $18 around the call.  (This case is not
2816      needed for 32 bit functions that call 16 bit functions, because
2817      16 bit functions always return floating point values in both
2818      $f0/$f1 and $2/$3.)
2819
2820      Note that in all cases FNNAME might be defined statically.
2821      Therefore, FNNAME is not used literally.  Instead, the relocation
2822      information will indicate which symbol the section is for.
2823
2824      We record any stubs that we find in the symbol table.  */
2825
2826 #define FN_STUB ".mips16.fn."
2827 #define CALL_STUB ".mips16.call."
2828 #define CALL_FP_STUB ".mips16.call.fp."
2829
2830 /* MIPS ELF64 linker hash table.  */
2831
2832 struct mips_elf64_link_hash_table
2833 {
2834   struct elf_link_hash_table root;
2835   /* This is set if we see any mips16 stub sections.  */
2836   boolean mips16_stubs_seen;
2837 };
2838
2839 /* Look up an entry in a MIPS ELF64 linker hash table.  */
2840
2841 #define mips_elf64_link_hash_lookup(table, string, create, copy, follow) \
2842   ((struct mips_elf64_link_hash_entry *)                                \
2843    elf_link_hash_lookup (&(table)->root, (string), (create),            \
2844                          (copy), (follow)))
2845
2846 /* Traverse a MIPS ELF linker hash table.  */
2847
2848 #define mips_elf64_link_hash_traverse(table, func, info)                \
2849   (elf_link_hash_traverse                                               \
2850    (&(table)->root,                                                     \
2851     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
2852     (info)))
2853
2854 /* Get the MIPS ELF64 linker hash table from a link_info structure.  */
2855
2856 #define mips_elf64_hash_table(p) \
2857   ((struct mips_elf64_link_hash_table *) ((p)->hash))
2858
2859 /* Create an entry in a MIPS ELF64 linker hash table.  */
2860
2861 static struct bfd_hash_entry *
2862 mips_elf64_link_hash_newfunc (entry, table, string)
2863      struct bfd_hash_entry *entry;
2864      struct bfd_hash_table *table;
2865      const char *string;
2866 {
2867   struct mips_elf64_link_hash_entry *ret =
2868     (struct mips_elf64_link_hash_entry *) entry;
2869
2870   /* Allocate the structure if it has not already been allocated by a
2871      subclass.  */
2872   if (ret == (struct mips_elf64_link_hash_entry *) NULL)
2873     ret = ((struct mips_elf64_link_hash_entry *)
2874            bfd_hash_allocate (table,
2875                               sizeof (struct mips_elf64_link_hash_entry)));
2876   if (ret == (struct mips_elf64_link_hash_entry *) NULL)
2877     return (struct bfd_hash_entry *) ret;
2878
2879   /* Call the allocation method of the superclass.  */
2880   ret = ((struct mips_elf64_link_hash_entry *)
2881          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2882                                      table, string));
2883   if (ret != (struct mips_elf64_link_hash_entry *) NULL)
2884     {
2885       /* Set local fields.  */
2886       memset (&ret->esym, 0, sizeof (EXTR));
2887       /* We use -2 as a marker to indicate that the information has
2888          not been set.  -1 means there is no associated ifd.  */
2889       ret->esym.ifd = -2;
2890       ret->possibly_dynamic_relocs = 0;
2891       ret->readonly_reloc = false;
2892       ret->min_dyn_reloc_index = 0;
2893       ret->no_fn_stub = false;
2894       ret->fn_stub = NULL;
2895       ret->need_fn_stub = false;
2896       ret->call_stub = NULL;
2897       ret->call_fp_stub = NULL;
2898     }
2899
2900   return (struct bfd_hash_entry *) ret;
2901 }
2902
2903 /* Create a MIPS ELF64 linker hash table.  */
2904
2905 struct bfd_link_hash_table *
2906 mips_elf64_link_hash_table_create (abfd)
2907      bfd *abfd;
2908 {
2909   struct mips_elf64_link_hash_table *ret;
2910
2911   ret = ((struct mips_elf64_link_hash_table *)
2912          bfd_alloc (abfd, sizeof (struct mips_elf64_link_hash_table)));
2913   if (ret == (struct mips_elf64_link_hash_table *) NULL)
2914     return NULL;
2915
2916   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2917                                        mips_elf64_link_hash_newfunc))
2918     {
2919       bfd_release (abfd, ret);
2920       return NULL;
2921     }
2922
2923   ret->mips16_stubs_seen = false;
2924
2925   return &ret->root.root;
2926 }
2927 \f
2928 /* Returns the offset for the entry at the INDEXth position
2929    in the GOT.  */
2930
2931 static bfd_vma
2932 mips_elf64_got_offset_from_index (dynobj, output_bfd, index)
2933      bfd *dynobj;
2934      bfd *output_bfd;
2935      bfd_vma index;
2936 {
2937   asection *sgot;
2938   bfd_vma gp;
2939
2940   sgot = bfd_get_section_by_name (dynobj, ".got");
2941   gp = _bfd_get_gp_value (output_bfd);
2942   return (sgot->output_section->vma + sgot->output_offset + index - 
2943           gp);
2944 }
2945
2946 /* Returns the GOT information associated with the link indicated by
2947    INFO.  If SGOTP is non-NULL, it is filled in with the GOT 
2948    section.  */
2949
2950 static struct mips_elf64_got_info *
2951 _mips_elf64_got_info (abfd, sgotp)
2952      bfd *abfd;
2953      asection **sgotp;
2954 {
2955   asection *sgot;
2956   struct mips_elf64_got_info *g;
2957
2958   sgot = bfd_get_section_by_name (abfd, ".got");
2959   BFD_ASSERT (sgot != NULL);
2960   BFD_ASSERT (elf_section_data (sgot) != NULL);
2961   g = (struct mips_elf64_got_info *) elf_section_data (sgot)->tdata;
2962   BFD_ASSERT (g != NULL);
2963
2964   if (sgotp)
2965     *sgotp = sgot;
2966   return g;
2967 }
2968
2969 /* Sign-extend VALUE, which has the indicated number of BITS.  */
2970
2971 static bfd_vma
2972 mips_elf64_sign_extend (value, bits)
2973      bfd_vma value;
2974      int bits;
2975 {
2976   if (value & ((bfd_vma)1 << (bits - 1)))
2977     /* VALUE is negative.  */
2978     value |= ((bfd_vma) - 1) << bits;      
2979   
2980   return value;
2981 }
2982
2983 /* Return non-zero if the indicated VALUE has overflowed the maximum
2984    range expressable by a signed number with the indicated number of
2985    BITS.  */
2986
2987 static boolean
2988 mips_elf64_overflow_p (value, bits)
2989      bfd_vma value;
2990      int bits;
2991 {
2992   bfd_signed_vma svalue = (bfd_signed_vma) value;
2993
2994   if (svalue > (1 << (bits - 1)) - 1)
2995     /* The value is too big.  */
2996     return true;
2997   else if (svalue < -(1 << (bits - 1)))
2998     /* The value is too small.  */
2999     return true;
3000     
3001   /* All is well.  */
3002   return false;
3003 }
3004 \f
3005 /* Returns the GOT index for the global symbol indicated by H.  */
3006
3007 static bfd_vma 
3008 mips_elf64_global_got_index (abfd, h)
3009      bfd *abfd;
3010      struct elf_link_hash_entry *h;
3011 {
3012   bfd_vma index;
3013   asection *sgot;
3014   struct mips_elf64_got_info *g;
3015
3016   g = _mips_elf64_got_info (abfd, &sgot);
3017
3018   /* Once we determine the global GOT entry with the lowest dynamic
3019      symbol table index, we must put all dynamic symbols with greater
3020      indices into the GOT.  That makes it easy to calculate the GOT
3021      offset.  */
3022   BFD_ASSERT (h->dynindx >= g->global_gotsym->dynindx);
3023   index = ((h->dynindx - g->global_gotsym->dynindx + g->local_gotno) 
3024            * (get_elf_backend_data (abfd)->s->arch_size / 8));
3025   BFD_ASSERT (index < sgot->_raw_size);
3026
3027   return index;
3028 }
3029
3030 struct mips_elf64_hash_sort_data
3031 {
3032   /* The symbol in the global GOT with the lowest dynamic symbol table
3033      index.  */
3034   struct elf_link_hash_entry *low;
3035   /* The least dynamic symbol table index corresponding to a symbol
3036      with a GOT entry.  */
3037   long min_got_dynindx;
3038   /* The greatest dynamic symbol table index not corresponding to a
3039      symbol without a GOT entry.  */
3040   long max_non_got_dynindx;
3041 };
3042
3043 /* If H needs a GOT entry, assign it the highest available dynamic
3044    index.  Otherwise, assign it the lowest available dynamic 
3045    index.  */
3046
3047 static boolean
3048 mips_elf64_sort_hash_table_f (h, data)
3049      struct mips_elf64_link_hash_entry *h;
3050      PTR data;
3051 {
3052   struct mips_elf64_hash_sort_data *hsd 
3053     = (struct mips_elf64_hash_sort_data *) data;
3054
3055   if (h->root.root.type == bfd_link_hash_warning)
3056     h = (struct mips_elf64_link_hash_entry *) h->root.root.u.i.link;
3057
3058   /* Symbols without dynamic symbol table entries aren't interesting
3059      at all.  */
3060   if (h->root.dynindx == -1)
3061     return true;
3062
3063   if (h->root.got.offset != 1)
3064     h->root.dynindx = hsd->max_non_got_dynindx++;
3065   else
3066     {
3067       h->root.dynindx = --hsd->min_got_dynindx;
3068       hsd->low = (struct elf_link_hash_entry *) h;
3069     }
3070
3071   return true;
3072 }
3073
3074 /* Sort the dynamic symbol table so that symbols that need GOT entries
3075    appear towards the end.  This reduces the amount of GOT space
3076    required.  MAX_LOCAL is used to set the number of local symbols
3077    known to be in the dynamic symbol table.  During
3078    mips_elf64_size_dynamic_sections, this value is 1.  Afterward, the
3079    section symbols are added and the count is higher.  */
3080
3081 static boolean
3082 mips_elf64_sort_hash_table (info, max_local)
3083      struct bfd_link_info *info;
3084      unsigned long max_local;
3085 {
3086   struct mips_elf64_hash_sort_data hsd;
3087   struct mips_elf64_got_info *g;
3088   bfd *dynobj;
3089
3090   dynobj = elf_hash_table (info)->dynobj;
3091
3092   hsd.low = NULL;
3093   hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount;
3094   hsd.max_non_got_dynindx = max_local;
3095   mips_elf64_link_hash_traverse (((struct mips_elf64_link_hash_table *) 
3096                                   elf_hash_table (info)), 
3097                                  mips_elf64_sort_hash_table_f, 
3098                                  &hsd);
3099
3100   /* There shoud have been enough room in the symbol table to
3101      accomodate both the GOT and non-GOT symbols.  */
3102   BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3103
3104   /* Now we know which dynamic symbol has the lowest dynamic symbol
3105      table index in the GOT.  */
3106   g = _mips_elf64_got_info (dynobj, NULL);
3107   g->global_gotsym = hsd.low;
3108
3109   return true;
3110 }
3111 \f
3112 #if 0
3113 /* Swap in an MSYM entry.  */
3114
3115 static void
3116 mips_elf64_swap_msym_in (abfd, ex, in)
3117      bfd *abfd;
3118      const Elf32_External_Msym *ex;
3119      Elf32_Internal_Msym *in;
3120 {
3121   in->ms_hash_value = H_GET_32 (abfd, ex->ms_hash_value);
3122   in->ms_info = H_GET_32 (abfd, ex->ms_info);
3123 }
3124 #endif
3125 /* Swap out an MSYM entry.  */
3126
3127 static void
3128 mips_elf64_swap_msym_out (abfd, in, ex)
3129      bfd *abfd;
3130      const Elf32_Internal_Msym *in;
3131      Elf32_External_Msym *ex;
3132 {
3133   H_PUT_32 (abfd, in->ms_hash_value, ex->ms_hash_value);
3134   H_PUT_32 (abfd, in->ms_info, ex->ms_info);
3135 }
3136 \f
3137 /* Create a local GOT entry for VALUE.  Return the index of the entry,
3138    or -1 if it could not be created.  */
3139
3140 static bfd_vma
3141 mips_elf64_create_local_got_entry (abfd, g, sgot, value)
3142      bfd *abfd;
3143      struct mips_elf64_got_info *g;
3144      asection *sgot;
3145      bfd_vma value;
3146 {
3147   CONST bfd_vma got_size = get_elf_backend_data (abfd)->s->arch_size / 8;
3148   
3149   if (g->assigned_gotno >= g->local_gotno)
3150     {
3151       /* We didn't allocate enough space in the GOT.  */
3152       (*_bfd_error_handler)
3153         (_("not enough GOT space for local GOT entries"));
3154       bfd_set_error (bfd_error_bad_value);
3155       return (bfd_vma) -1;
3156     }
3157
3158   bfd_put_64 (abfd, value, (sgot->contents + got_size * g->assigned_gotno));
3159   return got_size * g->assigned_gotno++;
3160 }
3161
3162 /* Returns the GOT offset at which the indicated address can be found.
3163    If there is not yet a GOT entry for this value, create one.  Returns
3164    -1 if no satisfactory GOT offset can be found.  */
3165
3166 static bfd_vma
3167 mips_elf64_local_got_index (abfd, info, value)
3168      bfd *abfd;
3169      struct bfd_link_info *info;
3170      bfd_vma value;
3171 {
3172   CONST bfd_vma got_size = get_elf_backend_data (abfd)->s->arch_size / 8;
3173   asection *sgot;
3174   struct mips_elf64_got_info *g;
3175   bfd_byte *entry;
3176
3177   g = _mips_elf64_got_info (elf_hash_table (info)->dynobj, &sgot);
3178
3179   /* Look to see if we already have an appropriate entry.  */
3180   for (entry = (sgot->contents + got_size * MIPS_RESERVED_GOTNO); 
3181        entry != sgot->contents + got_size * g->assigned_gotno;
3182        entry += got_size)
3183     {
3184       bfd_vma address = bfd_get_64 (abfd, entry);
3185       if (address == value)
3186         return entry - sgot->contents;
3187     }
3188
3189   return mips_elf64_create_local_got_entry (abfd, g, sgot, value);
3190 }
3191
3192 /* Find a GOT entry that is within 32KB of the VALUE.  These entries
3193    are supposed to be placed at small offsets in the GOT, i.e.,
3194    within 32KB of GP.  Return the index into the GOT for this page,
3195    and store the offset from this entry to the desired address in
3196    OFFSETP, if it is non-NULL.  */
3197
3198 static bfd_vma
3199 mips_elf64_got_page (abfd, info, value, offsetp)
3200      bfd *abfd;
3201      struct bfd_link_info *info;
3202      bfd_vma value;
3203      bfd_vma *offsetp;
3204 {
3205   CONST bfd_vma got_size = get_elf_backend_data (abfd)->s->arch_size / 8;
3206   asection *sgot;
3207   struct mips_elf64_got_info *g;
3208   bfd_byte *entry;
3209   bfd_byte *last_entry;
3210   bfd_vma index = 0;
3211   bfd_vma address;
3212
3213   g = _mips_elf64_got_info (elf_hash_table (info)->dynobj, &sgot);
3214
3215   /* Look to see if we aleady have an appropriate entry.  */
3216   last_entry = sgot->contents + got_size * g->assigned_gotno;
3217   for (entry = (sgot->contents + got_size * MIPS_RESERVED_GOTNO);
3218        entry != last_entry;
3219        entry += got_size)
3220     {
3221       address = bfd_get_64 (abfd, entry);
3222
3223       if (!mips_elf64_overflow_p (value - address, 16))
3224         {
3225           /* This entry will serve as the page pointer.  We can add a
3226              16-bit number to it to get the actual address.  */
3227           index = entry - sgot->contents;
3228           break;
3229         }
3230     }
3231
3232   /* If we didn't have an appropriate entry, we create one now.  */
3233   if (entry == last_entry)
3234     index = mips_elf64_create_local_got_entry (abfd, g, sgot, value);
3235
3236   if (offsetp)
3237     {
3238       address = bfd_get_64 (abfd, entry);
3239       *offsetp = value - address;
3240     }
3241
3242   return index;
3243 }
3244
3245 /* Find a GOT entry whose higher-order 16 bits are the same as those
3246    for value.  Return the index into the GOT for this entry.  */
3247
3248 static bfd_vma
3249 mips_elf64_got16_entry (abfd, info, value, external)
3250      bfd *abfd;
3251      struct bfd_link_info *info;
3252      bfd_vma value;
3253      boolean external;
3254 {
3255   CONST bfd_vma got_size = get_elf_backend_data (abfd)->s->arch_size / 8;
3256   asection *sgot;
3257   struct mips_elf64_got_info *g;
3258   bfd_byte *entry;
3259   bfd_byte *last_entry;
3260   bfd_vma index = 0;
3261   bfd_vma address;
3262
3263   if (! external)
3264     {
3265       /* Although the ABI says that it is "the high-order 16 bits" that we
3266          want, it is really the %high value.  The complete value is
3267          calculated with a `addiu' of a LO16 relocation, just as with a
3268          HI16/LO16 pair.  */
3269       value = mips_elf64_high (value) << 16;
3270     }
3271
3272   g = _mips_elf64_got_info (elf_hash_table (info)->dynobj, &sgot);
3273
3274   /* Look to see if we already have an appropriate entry.  */
3275   last_entry = sgot->contents + got_size * g->assigned_gotno;
3276   for (entry = (sgot->contents + got_size * MIPS_RESERVED_GOTNO);
3277        entry != last_entry;
3278        entry += got_size)
3279     {
3280       address = bfd_get_64 (abfd, entry);
3281       if (address == value)
3282         {
3283           /* This entry has the right high-order 16 bits, and the low-order
3284              16 bits are set to zero.  */
3285           index = entry - sgot->contents;
3286           break;
3287         }
3288     }
3289
3290   /* If we didn't have an appropriate entry, we create one now.  */
3291   if (entry == last_entry)
3292     index = mips_elf64_create_local_got_entry (abfd, g, sgot, value);
3293
3294   return index;
3295 }
3296 \f
3297 /* Return whether a relocation is against a local symbol.  */
3298
3299 static boolean
3300 mips_elf64_local_relocation_p (input_bfd, relocation, local_sections,
3301                              check_forced)
3302      bfd *input_bfd;
3303      const Elf_Internal_Rela *relocation;
3304      asection **local_sections;
3305      boolean check_forced;
3306 {
3307   unsigned long r_symndx;
3308   Elf_Internal_Shdr *symtab_hdr;
3309   struct mips_elf64_link_hash_entry* h;
3310   size_t extsymoff;
3311
3312   r_symndx = ELF64_R_SYM (relocation->r_info);
3313   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3314   extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
3315
3316   if (r_symndx < extsymoff)
3317     return true;
3318   if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
3319     return true;
3320
3321   if (check_forced)
3322     {
3323        /* Look up the hash table to check whether the symbol
3324          was forced local.  */
3325        h = (struct mips_elf64_link_hash_entry *)
3326           elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
3327        /* Find the real hash-table entry for this symbol.  */
3328        while (h->root.root.type == bfd_link_hash_indirect
3329              || h->root.root.type == bfd_link_hash_warning)
3330          h = (struct mips_elf64_link_hash_entry *) h->root.root.u.i.link;
3331        if ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
3332          return true;
3333     }
3334
3335   return false;
3336 }
3337 \f
3338 /* Returns the first relocation of type r_type found, beginning with
3339    RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
3340
3341 static const Elf_Internal_Rela *
3342 mips_elf64_next_relocation (r_type, relocation, relend)
3343      unsigned int r_type;
3344      const Elf_Internal_Rela *relocation;
3345      const Elf_Internal_Rela *relend;
3346 {
3347   /* According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must be
3348      immediately following.  However, for the IRIX6 ABI, the next
3349      relocation may be a composed relocation consisting of several
3350      relocations for the same address.  In that case, the R_MIPS_LO16
3351      relocation may occur as one of these.  We permit a similar
3352      extension in general, as that is useful for GCC.  */
3353   while (relocation < relend)
3354     {
3355       if (ELF64_MIPS_R_TYPE (relocation->r_info) == r_type)
3356         return relocation;
3357
3358       ++relocation;
3359     }
3360
3361   /* We didn't find it.  */
3362   bfd_set_error (bfd_error_bad_value);
3363   return NULL;
3364 }
3365
3366 /* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
3367    is the original relocation, which is now being transformed into a
3368    dynamic relocation.  The ADDENDP is adjusted if necessary; the
3369    caller should store the result in place of the original addend.  */
3370
3371 static boolean
3372 mips_elf64_create_dynamic_relocation (output_bfd, info, rel, h, sec,
3373                                     symbol, addendp, input_section)
3374      bfd *output_bfd;
3375      struct bfd_link_info *info;
3376      const Elf_Internal_Rela *rel;
3377      struct mips_elf64_link_hash_entry *h;
3378      asection *sec;
3379      bfd_vma symbol;
3380      bfd_vma *addendp;
3381      asection *input_section;
3382 {
3383   Elf_Internal_Rel outrel[3];
3384   boolean skip;
3385   asection *sreloc;
3386   bfd *dynobj;
3387   int r_type;
3388
3389   r_type = ELF64_MIPS_R_TYPE (rel->r_info);
3390   dynobj = elf_hash_table (info)->dynobj;
3391   sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
3392   BFD_ASSERT (sreloc != NULL);
3393   BFD_ASSERT (sreloc->contents != NULL);
3394   BFD_ASSERT ((sreloc->reloc_count
3395                * get_elf_backend_data (output_bfd)->s->sizeof_rel)
3396               < sreloc->_raw_size);
3397
3398   skip = false;
3399   outrel[0].r_offset = _bfd_elf_section_offset (output_bfd, info,
3400                                                 input_section,
3401                                                 rel[0].r_offset);
3402   /* FIXME: For -2 runtime relocation needs to be skipped, but
3403      properly resolved statically and installed.  */
3404   BFD_ASSERT (outrel[0].r_offset != (bfd_vma) -2);
3405
3406   /* We begin by assuming that the offset for the dynamic relocation
3407      is the same as for the original relocation.  We'll adjust this
3408      later to reflect the correct output offsets.  */
3409   if (elf_section_data (input_section)->sec_info_type != ELF_INFO_TYPE_STABS)
3410     {
3411       outrel[1].r_offset = rel[1].r_offset;
3412       outrel[2].r_offset = rel[2].r_offset;
3413     }
3414   else
3415     {
3416       /* Except that in a stab section things are more complex.
3417          Because we compress stab information, the offset given in the
3418          relocation may not be the one we want; we must let the stabs
3419          machinery tell us the offset.  */
3420       outrel[1].r_offset = outrel[0].r_offset;
3421       outrel[2].r_offset = outrel[0].r_offset;
3422       /* If we didn't need the relocation at all, this value will be
3423          -1.  */
3424       if (outrel[0].r_offset == (bfd_vma) -1)
3425         skip = true;
3426     }
3427
3428   /* If we've decided to skip this relocation, just output an empty
3429      record.  Note that R_MIPS_NONE == 0, so that this call to memset
3430      is a way of setting R_TYPE to R_MIPS_NONE.  */
3431   if (skip)
3432     memset (outrel, 0, sizeof (Elf_Internal_Rel) * 3);
3433   else
3434     {
3435       long indx;
3436       bfd_vma section_offset;
3437
3438       /* We must now calculate the dynamic symbol table index to use
3439          in the relocation.  */
3440       if (h != NULL
3441           && (! info->symbolic || (h->root.elf_link_hash_flags
3442                                    & ELF_LINK_HASH_DEF_REGULAR) == 0))
3443         {
3444           indx = h->root.dynindx;
3445           /* h->root.dynindx may be -1 if this symbol was marked to
3446              become local.  */
3447           if (indx == -1)
3448                 indx = 0;
3449         }
3450       else
3451         {
3452           if (sec != NULL && bfd_is_abs_section (sec))
3453             indx = 0;
3454           else if (sec == NULL || sec->owner == NULL)
3455             {
3456               bfd_set_error (bfd_error_bad_value);
3457               return false;
3458             }
3459           else
3460             {
3461               indx = elf_section_data (sec->output_section)->dynindx;
3462               if (indx == 0)
3463                 abort ();
3464             }
3465
3466           /* Figure out how far the target of the relocation is from
3467              the beginning of its section.  */
3468           section_offset = symbol - sec->output_section->vma;
3469           /* The relocation we're building is section-relative.
3470              Therefore, the original addend must be adjusted by the
3471              section offset.  */
3472           *addendp += section_offset;
3473           /* Now, the relocation is just against the section.  */
3474           symbol = sec->output_section->vma;
3475         }
3476       
3477       /* If the relocation was previously an absolute relocation and
3478          this symbol will not be referred to by the relocation, we must
3479          adjust it by the value we give it in the dynamic symbol table.
3480          Otherwise leave the job up to the dynamic linker.  */
3481       if (!indx && r_type != R_MIPS_REL32)
3482         *addendp += symbol;
3483
3484       /* The relocation is always an REL32 relocation because we don't
3485          know where the shared library will wind up at load-time.  */
3486       outrel[0].r_info = ELF64_R_INFO (indx, R_MIPS_REL32);
3487
3488       /* Adjust the output offset of the relocation to reference the
3489          correct location in the output file.  */
3490       outrel[0].r_offset += (input_section->output_section->vma
3491                              + input_section->output_offset);
3492       outrel[1].r_offset += (input_section->output_section->vma
3493                              + input_section->output_offset);
3494       outrel[2].r_offset += (input_section->output_section->vma
3495                              + input_section->output_offset);
3496     }
3497
3498   /* Put the relocation back out.  */
3499   mips_elf64_be_swap_reloc_out (output_bfd, outrel,
3500                                 (sreloc->contents 
3501                                  + sreloc->reloc_count
3502                                    * sizeof (Elf64_Mips_External_Rel)));
3503
3504   /* Record the index of the first relocation referencing H.  This
3505      information is later emitted in the .msym section.  */
3506   if (h != NULL
3507       && (h->min_dyn_reloc_index == 0 
3508           || sreloc->reloc_count < h->min_dyn_reloc_index))
3509     h->min_dyn_reloc_index = sreloc->reloc_count;
3510
3511   /* We've now added another relocation.  */
3512   ++sreloc->reloc_count;
3513
3514   /* Make sure the output section is writable.  The dynamic linker
3515      will be writing to it.  */
3516   elf_section_data (input_section->output_section)->this_hdr.sh_flags
3517     |= SHF_WRITE;
3518
3519   return true;
3520 }
3521
3522 /* Calculate the value produced by the RELOCATION (which comes from
3523    the INPUT_BFD).  The ADDEND is the addend to use for this
3524    RELOCATION; RELOCATION->R_ADDEND is ignored.
3525
3526    The result of the relocation calculation is stored in VALUEP.
3527    REQUIRE_JALXP indicates whether or not the opcode used with this
3528    relocation must be JALX.
3529
3530    This function returns bfd_reloc_continue if the caller need take no
3531    further action regarding this relocation, bfd_reloc_notsupported if
3532    something goes dramatically wrong, bfd_reloc_overflow if an
3533    overflow occurs, and bfd_reloc_ok to indicate success.  */
3534
3535 static bfd_reloc_status_type
3536 mips_elf64_calculate_relocation (abfd, input_bfd, input_section, info,
3537                                  relocation, addend, howto, local_syms,
3538                                  local_sections, valuep, namep, require_jalxp)
3539      bfd *abfd;
3540      bfd *input_bfd;
3541      asection *input_section;
3542      struct bfd_link_info *info;
3543      const Elf_Internal_Rela *relocation;
3544      bfd_vma addend;
3545      reloc_howto_type *howto;
3546      Elf_Internal_Sym *local_syms;
3547      asection **local_sections;
3548      bfd_vma *valuep;
3549      const char **namep;
3550      boolean *require_jalxp;
3551 {
3552   /* The eventual value we will return.  */
3553   bfd_vma value;
3554   /* The address of the symbol against which the relocation is
3555      occurring.  */
3556   bfd_vma symbol = 0;
3557   /* The final GP value to be used for the relocatable, executable, or
3558      shared object file being produced.  */
3559   bfd_vma gp = (bfd_vma) - 1;
3560   /* The place (section offset or address) of the storage unit being
3561      relocated.  */
3562   bfd_vma p;
3563   /* The value of GP used to create the relocatable object.  */
3564   bfd_vma gp0 = (bfd_vma) - 1;
3565   /* The offset into the global offset table at which the address of
3566      the relocation entry symbol, adjusted by the addend, resides
3567      during execution.  */
3568   bfd_vma g = (bfd_vma) - 1;
3569   /* The section in which the symbol referenced by the relocation is
3570      located.  */
3571   asection *sec = NULL;
3572   struct mips_elf64_link_hash_entry* h = NULL;
3573   /* True if the symbol referred to by this relocation is a local
3574      symbol.  */
3575   boolean local_p;
3576   Elf_Internal_Shdr *symtab_hdr;
3577   size_t extsymoff;
3578   unsigned long r_symndx;
3579   int r_type;
3580   /* True if overflow occurred during the calculation of the
3581      relocation value.  */
3582   boolean overflowed_p;
3583   /* True if this relocation refers to a MIPS16 function.  */
3584   boolean target_is_16_bit_code_p = false;
3585
3586   /* Parse the relocation.  */
3587   r_symndx = ELF64_R_SYM (relocation->r_info);
3588   r_type = ELF64_MIPS_R_TYPE (relocation->r_info);
3589   p = (input_section->output_section->vma 
3590        + input_section->output_offset
3591        + relocation->r_offset);
3592
3593   /* Assume that there will be no overflow.  */
3594   overflowed_p = false;
3595
3596   /* Figure out whether or not the symbol is local, and get the offset
3597      used in the array of hash table entries.  */
3598   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3599   local_p = mips_elf64_local_relocation_p (input_bfd, relocation,
3600                                          local_sections, false);
3601   if (! elf_bad_symtab (input_bfd))
3602     extsymoff = symtab_hdr->sh_info;
3603   else
3604     {
3605       /* The symbol table does not follow the rule that local symbols
3606          must come before globals.  */
3607       extsymoff = 0;
3608     }
3609
3610   /* Figure out the value of the symbol.  */
3611   if (local_p)
3612     {
3613       Elf_Internal_Sym *sym;
3614
3615       sym = local_syms + r_symndx;
3616       sec = local_sections[r_symndx];
3617
3618       symbol = sec->output_section->vma + sec->output_offset;
3619       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3620         symbol += sym->st_value;
3621
3622       /* MIPS16 text labels should be treated as odd.  */
3623       if (sym->st_other == STO_MIPS16)
3624         ++symbol;
3625
3626       /* Record the name of this symbol, for our caller.  */
3627       *namep = bfd_elf_string_from_elf_section (input_bfd,
3628                                                 symtab_hdr->sh_link,
3629                                                 sym->st_name);
3630       if (*namep == '\0')
3631         *namep = bfd_section_name (input_bfd, sec);
3632
3633       target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
3634     }
3635   else
3636     {
3637       /* For global symbols we look up the symbol in the hash-table.  */
3638       h = ((struct mips_elf64_link_hash_entry *) 
3639            elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
3640       /* Find the real hash-table entry for this symbol.  */
3641       while (h->root.root.type == bfd_link_hash_indirect
3642              || h->root.root.type == bfd_link_hash_warning)
3643         h = (struct mips_elf64_link_hash_entry *) h->root.root.u.i.link;
3644       
3645       /* Record the name of this symbol, for our caller.  */
3646       *namep = h->root.root.root.string;
3647
3648       /* If this symbol is defined, calculate its address.  */
3649       if ((h->root.root.type == bfd_link_hash_defined
3650            || h->root.root.type == bfd_link_hash_defweak)
3651           && h->root.root.u.def.section)
3652         {
3653           sec = h->root.root.u.def.section;
3654           if (sec->output_section)
3655             symbol = (h->root.root.u.def.value 
3656                       + sec->output_section->vma
3657                       + sec->output_offset);
3658           else
3659             symbol = h->root.root.u.def.value;
3660         }
3661       else if (h->root.root.type == bfd_link_hash_undefweak)
3662         /* We allow relocations against undefined weak symbols, giving
3663            it the value zero, so that you can undefined weak functions
3664            and check to see if they exist by looking at their
3665            addresses.  */
3666         symbol = 0;
3667       else if (info->shared
3668                && (!info->symbolic || info->allow_shlib_undefined)
3669                && !info->no_undefined
3670                && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
3671         symbol = 0;
3672       else if (strcmp (h->root.root.root.string, "_DYNAMIC_LINK") == 0 ||
3673               strcmp (h->root.root.root.string, "_DYNAMIC_LINKING") == 0)
3674         {
3675           /* If this is a dynamic link, we should have created a
3676              _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol 
3677              in in mips_elf64_create_dynamic_sections.
3678              Otherwise, we should define the symbol with a value of 0.
3679              FIXME: It should probably get into the symbol table
3680              somehow as well.  */
3681           BFD_ASSERT (! info->shared);
3682           BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
3683           symbol = 0;
3684         }
3685       else
3686         {
3687           if (! ((*info->callbacks->undefined_symbol)
3688                  (info, h->root.root.root.string, input_bfd,
3689                   input_section, relocation->r_offset,
3690                   (!info->shared || info->no_undefined
3691                    || ELF_ST_VISIBILITY (h->root.other)))))
3692             return bfd_reloc_undefined;
3693           symbol = 0;
3694         }
3695
3696       target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
3697     }
3698
3699   /* If this is a 64-bit call to a 16-bit function with a stub, we
3700      need to redirect the call to the stub, unless we're already *in*
3701      a stub.  */
3702   if (r_type != R_MIPS16_26 && !info->relocateable
3703       && ((h != NULL && h->fn_stub != NULL)
3704           || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
3705               && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
3706       && !mips_elf64_stub_section_p (input_bfd, input_section))
3707     {
3708       /* This is a 64-bit call to a 16-bit function.  We should
3709          have already noticed that we were going to need the
3710          stub.  */
3711       if (local_p)
3712         sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
3713       else
3714         {
3715           BFD_ASSERT (h->need_fn_stub);
3716           sec = h->fn_stub;
3717         }
3718
3719       symbol = sec->output_section->vma + sec->output_offset;
3720     }
3721   /* If this is a 16-bit call to a 64-bit function with a stub, we
3722      need to redirect the call to the stub.  */
3723   else if (r_type == R_MIPS16_26 && !info->relocateable
3724            && h != NULL
3725            && (h->call_stub != NULL || h->call_fp_stub != NULL)
3726            && !target_is_16_bit_code_p)
3727     {
3728       /* If both call_stub and call_fp_stub are defined, we can figure
3729          out which one to use by seeing which one appears in the input
3730          file.  */
3731       if (h->call_stub != NULL && h->call_fp_stub != NULL)
3732         {
3733           asection *o;
3734
3735           sec = NULL;
3736           for (o = input_bfd->sections; o != NULL; o = o->next)
3737             {
3738               if (strncmp (bfd_get_section_name (input_bfd, o),
3739                            CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
3740                 {
3741                   sec = h->call_fp_stub;
3742                   break;
3743                 }
3744             }
3745           if (sec == NULL)
3746             sec = h->call_stub;
3747         }
3748       else if (h->call_stub != NULL)
3749         sec = h->call_stub;
3750       else
3751         sec = h->call_fp_stub;
3752
3753       BFD_ASSERT (sec->_raw_size > 0);
3754       symbol = sec->output_section->vma + sec->output_offset;
3755     }
3756
3757   /* Calls from 16-bit code to 32-bit code and vice versa require the
3758      special jalx instruction.  */
3759   *require_jalxp = (!info->relocateable
3760                     && ((r_type == R_MIPS16_26) != target_is_16_bit_code_p));
3761
3762   local_p = mips_elf64_local_relocation_p (input_bfd, relocation,
3763                                            local_sections, true);
3764
3765   /* If we haven't already determined the GOT offset, or the GP value,
3766      and we're going to need it, get it now.  */
3767   switch (r_type)
3768     {
3769     case R_MIPS_CALL16:
3770     case R_MIPS_GOT16:
3771     case R_MIPS_GOT_DISP:
3772     case R_MIPS_GOT_HI16:
3773     case R_MIPS_CALL_HI16:
3774     case R_MIPS_GOT_LO16:
3775     case R_MIPS_CALL_LO16:
3776       /* Find the index into the GOT where this value is located.  */
3777       if (!local_p)
3778         {
3779           BFD_ASSERT (addend == 0);
3780           g = mips_elf64_global_got_index (elf_hash_table (info)->dynobj,
3781                                          (struct elf_link_hash_entry*) h);
3782           if (! elf_hash_table(info)->dynamic_sections_created
3783               || (info->shared
3784                   && (info->symbolic || h->root.dynindx == -1)
3785                   && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
3786             {
3787               /* This is a static link or a -Bsymbolic link.  The
3788                  symbol is defined locally, or was forced to be local.
3789                  We must initialize this entry in the GOT.  */
3790               bfd *tmpbfd = elf_hash_table (info)->dynobj;
3791
3792               asection *sgot = bfd_get_section_by_name (tmpbfd, ".got");
3793               bfd_put_64 (tmpbfd, symbol + addend, sgot->contents + g);
3794             }
3795         }
3796       else if (r_type == R_MIPS_GOT16 || r_type == R_MIPS_CALL16)
3797         /* There's no need to create a local GOT entry here; the
3798            calculation for a local GOT16 entry does not involve G.  */
3799         break;
3800       else
3801         {
3802           g = mips_elf64_local_got_index (abfd, info, symbol + addend);
3803           if (g == (bfd_vma) -1)
3804             return false;
3805         }
3806
3807       /* Convert GOT indices to actual offsets.  */
3808       g = mips_elf64_got_offset_from_index (elf_hash_table (info)->dynobj,
3809                                             abfd, g);
3810       break;
3811       
3812     case R_MIPS_HI16:
3813     case R_MIPS_LO16:
3814     case R_MIPS_GPREL16:
3815     case R_MIPS_GPREL32:
3816     case R_MIPS_LITERAL:
3817       gp0 = _bfd_get_gp_value (input_bfd);
3818       gp = _bfd_get_gp_value (abfd);
3819       break;
3820
3821     default:
3822       break;
3823     }
3824
3825   /* Figure out what kind of relocation is being performed.  */
3826   switch (r_type)
3827     {
3828     case R_MIPS_NONE:
3829       return bfd_reloc_continue;
3830
3831     case R_MIPS_16:
3832       value = symbol + mips_elf64_sign_extend (addend, 16);
3833       overflowed_p = mips_elf64_overflow_p (value, 16);
3834       break;
3835
3836     case R_MIPS_32:
3837     case R_MIPS_REL32:
3838     case R_MIPS_64:
3839       if ((info->shared
3840            || (elf_hash_table (info)->dynamic_sections_created
3841                && h != NULL
3842                && ((h->root.elf_link_hash_flags
3843                     & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
3844                && ((h->root.elf_link_hash_flags
3845                     & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3846           && r_symndx != 0
3847           && (input_section->flags & SEC_ALLOC) != 0)
3848         {
3849           /* If we're creating a shared library, or this relocation is
3850              against a symbol in a shared library, then we can't know
3851              where the symbol will end up.  So, we create a relocation
3852              record in the output, and leave the job up to the dynamic
3853              linker.  */
3854           value = addend;
3855           if (!mips_elf64_create_dynamic_relocation (abfd, info, relocation,
3856                                                      h, sec, symbol, &value,
3857                                                      input_section))
3858             return false;
3859         }
3860       else
3861         {
3862           if (r_type != R_MIPS_REL32)
3863             value = symbol + addend;
3864           else
3865             value = addend;
3866         }
3867       value &= howto->dst_mask;
3868       break;
3869
3870     case R_MIPS_PC32:
3871     case R_MIPS_PC64:
3872     case R_MIPS_GNU_REL_LO16:
3873       value = symbol + addend - p;
3874       value &= howto->dst_mask;
3875       break;
3876
3877     case R_MIPS_GNU_REL16_S2:
3878       value = symbol + mips_elf64_sign_extend (addend << 2, 18) - p;
3879       overflowed_p = mips_elf64_overflow_p (value, 18);
3880       value = (value >> 2) & howto->dst_mask;
3881       break;
3882
3883     case R_MIPS_GNU_REL_HI16:
3884       value = mips_elf64_high (addend + symbol - p);
3885       value &= howto->dst_mask;
3886       break;
3887
3888     case R_MIPS16_26:
3889       /* The calculation for R_MIPS16_26 is just the same as for an
3890          R_MIPS_26.  It's only the storage of the relocated field into
3891          the output file that's different.  That's handled in
3892          mips_elf_perform_relocation.  So, we just fall through to the
3893          R_MIPS_26 case here.  */
3894     case R_MIPS_26:
3895       if (local_p)
3896         value = (((addend << 2) | ((p + 4) & 0xf0000000)) + symbol) >> 2;
3897       else
3898         value = (mips_elf64_sign_extend (addend << 2, 28) + symbol) >> 2;
3899       value &= howto->dst_mask;
3900       break;
3901
3902     case R_MIPS_HI16:
3903       value = mips_elf64_high (addend + symbol);
3904       value &= howto->dst_mask;
3905       break;
3906
3907     case R_MIPS_LO16:
3908         value = (addend + symbol) & 0xffff;
3909         value &= howto->dst_mask;
3910       break;
3911
3912     case R_MIPS_LITERAL:
3913       /* Because we don't merge literal sections, we can handle this
3914          just like R_MIPS_GPREL16.  In the long run, we should merge
3915          shared literals, and then we will need to additional work
3916          here.  */
3917
3918       /* Fall through.  */
3919
3920     case R_MIPS_GPREL16:
3921       if (local_p)
3922         value = mips_elf64_sign_extend (addend, 16) + symbol + gp0 - gp;
3923       else
3924         value = mips_elf64_sign_extend (addend, 16) + symbol - gp;
3925       overflowed_p = mips_elf64_overflow_p (value, 16);
3926       break;
3927       
3928     case R_MIPS_PC16:
3929       value = mips_elf64_sign_extend (addend, 16) + symbol - p;
3930       overflowed_p = mips_elf64_overflow_p (value, 16);
3931       value = (bfd_vma) ((bfd_signed_vma) value / 4);
3932       break;
3933
3934     case R_MIPS_GOT16:
3935     case R_MIPS_CALL16:
3936       if (local_p)
3937         {
3938           boolean forced;
3939           
3940           /* The special case is when the symbol is forced to be local.  We
3941              need the full address in the GOT since no R_MIPS_LO16 relocation
3942              follows.  */
3943           forced = ! mips_elf64_local_relocation_p (input_bfd, relocation,
3944                                                   local_sections, false);
3945           value = mips_elf64_got16_entry (abfd, info, symbol + addend, forced);
3946           if (value == (bfd_vma) -1)
3947             return false;
3948           value 
3949             = mips_elf64_got_offset_from_index (elf_hash_table (info)->dynobj,
3950                                               abfd,
3951                                               value);
3952           overflowed_p = mips_elf64_overflow_p (value, 16);
3953           break;
3954         }
3955
3956       /* Fall through.  */
3957
3958     case R_MIPS_GOT_DISP:
3959       value = g;
3960       overflowed_p = mips_elf64_overflow_p (value, 16);
3961       break;
3962
3963     case R_MIPS_GPREL32:
3964       value = (addend + symbol + gp0 - gp) & howto->dst_mask;
3965       break;
3966
3967     case R_MIPS_GOT_HI16:
3968     case R_MIPS_CALL_HI16:
3969       /* We're allowed to handle these two relocations identically.
3970          The dynamic linker is allowed to handle the CALL relocations
3971          differently by creating a lazy evaluation stub.  */
3972       value = g;
3973       value = mips_elf64_high (value);
3974       value &= howto->dst_mask;
3975       break;
3976
3977     case R_MIPS_GOT_LO16:
3978     case R_MIPS_CALL_LO16:
3979       value = g & howto->dst_mask;
3980       break;
3981
3982     case R_MIPS_GOT_PAGE:
3983       value = mips_elf64_got_page (abfd, info, symbol + addend, NULL);
3984       if (value == (bfd_vma) -1)
3985         return false;
3986       value = mips_elf64_got_offset_from_index (elf_hash_table (info)->dynobj,
3987                                               abfd,
3988                                               value);
3989       overflowed_p = mips_elf64_overflow_p (value, 16);
3990       break;
3991       
3992     case R_MIPS_GOT_OFST:
3993       mips_elf64_got_page (abfd, info, symbol + addend, &value);
3994       overflowed_p = mips_elf64_overflow_p (value, 16);
3995       break;
3996
3997     case R_MIPS_SUB:
3998       value = symbol - addend;
3999       value &= howto->dst_mask;
4000       break;
4001
4002     case R_MIPS_HIGHER:
4003       value = mips_elf64_higher (addend + symbol);
4004       value &= howto->dst_mask;
4005       break;
4006
4007     case R_MIPS_HIGHEST:
4008       value = mips_elf64_highest (addend + symbol);
4009       value &= howto->dst_mask;
4010       break;
4011       
4012     case R_MIPS_SCN_DISP:
4013       value = symbol + addend - sec->output_offset;
4014       value &= howto->dst_mask;
4015       break;
4016
4017     case R_MIPS_PJUMP:
4018     case R_MIPS_JALR:
4019       /* Both of these may be ignored.  R_MIPS_JALR is an optimization
4020          hint; we could improve performance by honoring that hint.  */
4021       return bfd_reloc_continue;
4022
4023     case R_MIPS_GNU_VTINHERIT:
4024     case R_MIPS_GNU_VTENTRY:
4025       /* We don't do anything with these at present.  */
4026       return bfd_reloc_continue;
4027
4028     default:
4029       /* An unrecognized relocation type.  */
4030       return bfd_reloc_notsupported;
4031     }
4032
4033   /* Store the VALUE for our caller.  */
4034   *valuep = value;
4035   return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
4036 }
4037 \f
4038 /* Obtain the field relocated by RELOCATION.  */
4039
4040 static bfd_vma
4041 mips_elf64_obtain_contents (howto, relocation, input_bfd, contents)
4042      reloc_howto_type *howto;
4043      const Elf_Internal_Rela *relocation;
4044      bfd *input_bfd;
4045      bfd_byte *contents;
4046 {
4047   bfd_byte *location = contents + relocation->r_offset;
4048
4049   /* Obtain the bytes.  */
4050   return bfd_get (8 * bfd_get_reloc_size (howto), input_bfd, location);
4051 }
4052
4053 /* It has been determined that the result of the RELOCATION is the
4054    VALUE.  Use HOWTO to place VALUE into the output file at the
4055    appropriate position.  The SECTION is the section to which the
4056    relocation applies.  If REQUIRE_JALX is true, then the opcode used
4057    for the relocation must be either JAL or JALX, and it is
4058    unconditionally converted to JALX.
4059
4060    Returns false if anything goes wrong.  */
4061
4062 static boolean
4063 mips_elf64_perform_relocation (info, howto, relocation, value,
4064                              input_bfd, input_section,
4065                              contents, require_jalx)
4066      struct bfd_link_info *info;
4067      reloc_howto_type *howto;
4068      const Elf_Internal_Rela *relocation;
4069      bfd_vma value;
4070      bfd *input_bfd;
4071      asection *input_section;
4072      bfd_byte *contents;
4073      boolean require_jalx;
4074 {
4075   bfd_vma x;
4076   bfd_byte *location;
4077   int r_type = ELF32_R_TYPE (relocation->r_info);
4078
4079   /* Figure out where the relocation is occurring.  */
4080   location = contents + relocation->r_offset;
4081
4082   /* Obtain the current value.  */
4083   x = mips_elf64_obtain_contents (howto, relocation, input_bfd, contents);
4084
4085   /* Clear the field we are setting.  */
4086   x &= ~howto->dst_mask;
4087
4088   /* If this is the R_MIPS16_26 relocation, we must store the
4089      value in a funny way.  */
4090   if (r_type == R_MIPS16_26)
4091     {
4092       /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
4093          Most mips16 instructions are 16 bits, but these instructions
4094          are 32 bits.
4095
4096          The format of these instructions is:
4097
4098          +--------------+--------------------------------+
4099          !     JALX     ! X!   Imm 20:16  !   Imm 25:21  !
4100          +--------------+--------------------------------+
4101          !                Immediate  15:0                   !
4102          +-----------------------------------------------+
4103
4104          JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
4105          Note that the immediate value in the first word is swapped.
4106
4107          When producing a relocateable object file, R_MIPS16_26 is
4108          handled mostly like R_MIPS_26.  In particular, the addend is
4109          stored as a straight 26-bit value in a 32-bit instruction.
4110          (gas makes life simpler for itself by never adjusting a
4111          R_MIPS16_26 reloc to be against a section, so the addend is
4112          always zero).  However, the 32 bit instruction is stored as 2
4113          16-bit values, rather than a single 32-bit value.  In a
4114          big-endian file, the result is the same; in a little-endian
4115          file, the two 16-bit halves of the 32 bit value are swapped.
4116          This is so that a disassembler can recognize the jal
4117          instruction.
4118
4119          When doing a final link, R_MIPS16_26 is treated as a 32 bit
4120          instruction stored as two 16-bit values.  The addend A is the
4121          contents of the targ26 field.  The calculation is the same as
4122          R_MIPS_26.  When storing the calculated value, reorder the
4123          immediate value as shown above, and don't forget to store the
4124          value as two 16-bit values.
4125
4126          To put it in MIPS ABI terms, the relocation field is T-targ26-16,
4127          defined as
4128
4129          big-endian:
4130          +--------+----------------------+
4131          |        |                      |
4132          |        |    targ26-16         |
4133          |31    26|25                   0|
4134          +--------+----------------------+
4135
4136          little-endian:
4137          +----------+------+-------------+
4138          |          |      |             |
4139          |  sub1    |      |     sub2    |
4140          |0        9|10  15|16         31|
4141          +----------+--------------------+
4142          where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
4143          ((sub1 << 16) | sub2)).
4144
4145          When producing a relocateable object file, the calculation is
4146          (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4147          When producing a fully linked file, the calculation is
4148          let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
4149          ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)  */
4150
4151       if (!info->relocateable)
4152         /* Shuffle the bits according to the formula above.  */
4153         value = (((value & 0x1f0000) << 5)
4154                  | ((value & 0x3e00000) >> 5)
4155                  | (value & 0xffff));
4156     }
4157   else if (r_type == R_MIPS16_GPREL)
4158     {
4159       /* R_MIPS16_GPREL is used for GP-relative addressing in mips16
4160          mode.  A typical instruction will have a format like this:
4161
4162          +--------------+--------------------------------+
4163          !    EXTEND    !     Imm 10:5    !   Imm 15:11  !
4164          +--------------+--------------------------------+
4165          !    Major     !   rx   !   ry   !   Imm  4:0   !
4166          +--------------+--------------------------------+
4167
4168          EXTEND is the five bit value 11110.  Major is the instruction
4169          opcode.
4170
4171          This is handled exactly like R_MIPS_GPREL16, except that the
4172          addend is retrieved and stored as shown in this diagram; that
4173          is, the Imm fields above replace the V-rel16 field.
4174
4175          All we need to do here is shuffle the bits appropriately.  As
4176          above, the two 16-bit halves must be swapped on a
4177          little-endian system.  */
4178       value = (((value & 0x7e0) << 16)
4179                | ((value & 0xf800) << 5)
4180                | (value & 0x1f));
4181     }
4182
4183   /* Set the field.  */
4184   x |= (value & howto->dst_mask);
4185
4186   /* If required, turn JAL into JALX.  */
4187   if (require_jalx)
4188     {
4189       boolean ok;
4190       bfd_vma opcode = x >> 26;
4191       bfd_vma jalx_opcode;
4192
4193       /* Check to see if the opcode is already JAL or JALX.  */
4194       if (r_type == R_MIPS16_26)
4195         {
4196           ok = ((opcode == 0x6) || (opcode == 0x7));
4197           jalx_opcode = 0x7;
4198         }
4199       else
4200         {
4201           ok = ((opcode == 0x3) || (opcode == 0x1d));
4202           jalx_opcode = 0x1d;
4203         }
4204
4205       /* If the opcode is not JAL or JALX, there's a problem.  */
4206       if (!ok)
4207         {
4208           (*_bfd_error_handler)
4209             (_("%s: %s+0x%lx: jump to stub routine which is not jal"),
4210              bfd_archive_filename (input_bfd),
4211              input_section->name,
4212              (unsigned long) relocation->r_offset);
4213           bfd_set_error (bfd_error_bad_value);
4214           return false;
4215         }
4216
4217       /* Make this the JALX opcode.  */
4218       x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
4219     }
4220
4221   /* Swap the high- and low-order 16 bits on little-endian systems
4222      when doing a MIPS16 relocation.  */
4223   if ((r_type == R_MIPS16_GPREL || r_type == R_MIPS16_26)
4224       && bfd_little_endian (input_bfd))
4225     x = (((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16));
4226
4227   /* Put the value into the output.  */
4228   bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
4229   return true;
4230 }
4231
4232 /* Returns true if SECTION is a MIPS16 stub section.  */
4233
4234 static boolean
4235 mips_elf64_stub_section_p (abfd, section)
4236      bfd *abfd ATTRIBUTE_UNUSED;
4237      asection *section;
4238 {
4239   const char *name = bfd_get_section_name (abfd, section);
4240
4241   return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
4242           || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
4243           || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
4244 }
4245
4246 /* Relocate a MIPS ELF64 section.  */
4247
4248 static boolean
4249 mips_elf64_relocate_section (output_bfd, info, input_bfd, input_section,
4250                              contents, relocs, local_syms, local_sections)
4251      bfd *output_bfd;
4252      struct bfd_link_info *info;
4253      bfd *input_bfd;
4254      asection *input_section;
4255      bfd_byte *contents;
4256      Elf_Internal_Rela *relocs;
4257      Elf_Internal_Sym *local_syms;
4258      asection **local_sections;
4259 {
4260   Elf_Internal_Rela *rel;
4261   const Elf_Internal_Rela *relend;
4262   bfd_vma addend = 0;
4263   boolean use_saved_addend_p = false;
4264   struct elf_backend_data *bed;
4265
4266   bed = get_elf_backend_data (output_bfd);
4267   relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
4268   for (rel = relocs; rel < relend; ++rel)
4269     {
4270       const char *name;
4271       bfd_vma value;
4272       reloc_howto_type *howto;
4273       boolean require_jalx;
4274       /* True if the relocation is a RELA relocation, rather than a
4275          REL relocation.  */
4276       boolean rela_relocation_p = true;
4277       int r_type = ELF64_MIPS_R_TYPE (rel->r_info);
4278       const char *msg = (const char *) NULL;
4279
4280       /* Find the relocation howto for this relocation.  */
4281       howto = &mips_elf64_howto_table_rela[r_type];
4282
4283       if (!use_saved_addend_p)
4284         {
4285           Elf_Internal_Shdr *rel_hdr;
4286
4287           /* If these relocations were originally of the REL variety,
4288              we must pull the addend out of the field that will be
4289              relocated.  Otherwise, we simply use the contents of the
4290              RELA relocation.  To determine which flavor or relocation
4291              this is, we depend on the fact that the INPUT_SECTION's
4292              REL_HDR is read before its REL_HDR2.  */
4293           rel_hdr = &elf_section_data (input_section)->rel_hdr;
4294           if ((size_t) (rel - relocs)
4295               >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
4296             rel_hdr = elf_section_data (input_section)->rel_hdr2;
4297           if (rel_hdr->sh_entsize
4298               == (get_elf_backend_data (input_bfd)->s->sizeof_rel))
4299             {
4300               /* Note that this is a REL relocation.  */
4301               rela_relocation_p = false;
4302
4303               /* Find the relocation howto for this relocation.  */
4304               howto = &mips_elf64_howto_table_rel[r_type];
4305
4306               /* Get the addend, which is stored in the input file.  */
4307               addend = mips_elf64_obtain_contents (howto, 
4308                                                    rel,
4309                                                    input_bfd,
4310                                                    contents);
4311               addend &= howto->src_mask;
4312
4313               /* For some kinds of relocations, the ADDEND is a
4314                  combination of the addend stored in two different
4315                  relocations.   */
4316               if (r_type == R_MIPS_HI16
4317                   || r_type == R_MIPS_GNU_REL_HI16
4318                   || (r_type == R_MIPS_GOT16
4319                       && mips_elf64_local_relocation_p (input_bfd, rel,
4320                                                       local_sections, false)))
4321                 {
4322                   bfd_vma l;
4323                   const Elf_Internal_Rela *lo16_relocation;
4324                   reloc_howto_type *lo16_howto;
4325                   int lo;
4326
4327                   /* The combined value is the sum of the HI16 addend,
4328                      left-shifted by sixteen bits, and the LO16
4329                      addend, sign extended.  (Usually, the code does
4330                      a `lui' of the HI16 value, and then an `addiu' of
4331                      the LO16 value.)  
4332
4333                      Scan ahead to find a matching LO16 relocation.  */
4334                   if (r_type == R_MIPS_GNU_REL_HI16)
4335                     lo = R_MIPS_GNU_REL_LO16;
4336                   else
4337                     lo = R_MIPS_LO16;
4338                   lo16_relocation 
4339                     = mips_elf64_next_relocation (lo, rel, relend); 
4340                   if (lo16_relocation == NULL)
4341                     return false;
4342
4343                   /* Obtain the addend kept there.  */
4344                   if (rela_relocation_p == false)
4345                     lo16_howto = &mips_elf64_howto_table_rel[lo];
4346                   else
4347                     lo16_howto = &mips_elf64_howto_table_rela[lo];
4348                   l = mips_elf64_obtain_contents (lo16_howto,
4349                                                 lo16_relocation,
4350                                                 input_bfd, contents);
4351                   l &= lo16_howto->src_mask;
4352                   l = mips_elf64_sign_extend (l, 16);
4353
4354                   addend <<= 16;
4355
4356                   /* Compute the combined addend.  */
4357                   addend += l;
4358                 }
4359             }
4360           else
4361             addend = rel->r_addend;
4362         }
4363
4364       if (info->relocateable)
4365         {
4366           Elf_Internal_Sym *sym;
4367           unsigned long r_symndx;
4368
4369           /* Since we're just relocating, all we need to do is copy
4370              the relocations back out to the object file, unless
4371              they're against a section symbol, in which case we need
4372              to adjust by the section offset, or unless they're GP
4373              relative in which case we need to adjust by the amount
4374              that we're adjusting GP in this relocateable object.  */
4375
4376           if (!mips_elf64_local_relocation_p (input_bfd, rel, local_sections,
4377                                             false))
4378             /* There's nothing to do for non-local relocations.  */
4379             continue;
4380
4381           if (r_type == R_MIPS_GPREL16
4382               || r_type == R_MIPS_GPREL32
4383               || r_type == R_MIPS_LITERAL)
4384             addend -= (_bfd_get_gp_value (output_bfd)
4385                        - _bfd_get_gp_value (input_bfd));
4386           else if (r_type == R_MIPS_26 || r_type == R_MIPS_GNU_REL16_S2)
4387             /* The addend is stored without its two least
4388                significant bits (which are always zero.)  In a
4389                non-relocateable link, calculate_relocation will do
4390                this shift; here, we must do it ourselves.  */
4391             addend <<= 2;
4392
4393           r_symndx = ELF64_R_SYM (rel->r_info);
4394           sym = local_syms + r_symndx;
4395           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4396             /* Adjust the addend appropriately.  */
4397             addend += local_sections[r_symndx]->output_offset;
4398
4399 #if 0
4400           /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
4401              then we only want to write out the high-order 16 bits.
4402              The subsequent R_MIPS_LO16 will handle the low-order bits.  */
4403           if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16
4404               || r_type == R_MIPS_GNU_REL_HI16)
4405             addend = mips_elf64_high (addend);
4406           else if (r_type == R_MIPS_HIGHER)
4407             addend = mips_elf64_higher (addend);
4408           else if (r_type == R_MIPS_HIGHEST)
4409             addend = mips_elf64_highest (addend);
4410 #endif
4411           /* If the relocation is for an R_MIPS_26 relocation, then
4412              the two low-order bits are not stored in the object file;
4413              they are implicitly zero.  */
4414           if (r_type == R_MIPS_26 || r_type == R_MIPS_GNU_REL16_S2)
4415             addend >>= 2;
4416
4417           if (rela_relocation_p)
4418             /* If this is a RELA relocation, just update the addend.
4419                We have to cast away constness for REL.  */
4420             rel->r_addend = addend;
4421           else
4422             {
4423               /* Otherwise, we have to write the value back out.  Note
4424                  that we use the source mask, rather than the
4425                  destination mask because the place to which we are
4426                  writing will be source of the addend in the final
4427                  link.  */
4428               addend &= howto->src_mask;
4429
4430               if (!mips_elf64_perform_relocation (info, howto, rel, addend,
4431                                                   input_bfd, input_section,
4432                                                   contents, false))
4433                 return false;
4434             }
4435
4436           /* Go on to the next relocation.  */
4437           continue;
4438         }
4439
4440       /* In the N32 and 64-bit ABIs there may be multiple consecutive
4441          relocations for the same offset.  In that case we are
4442          supposed to treat the output of each relocation as the addend
4443          for the next.  */
4444       if (rel + 1 < relend 
4445           && rel->r_offset == rel[1].r_offset
4446           && ELF64_MIPS_R_TYPE (rel[1].r_info) != R_MIPS_NONE)
4447         use_saved_addend_p = true;
4448       else
4449         use_saved_addend_p = false;
4450
4451       /* Figure out what value we are supposed to relocate.  */
4452       switch (mips_elf64_calculate_relocation (output_bfd, input_bfd,
4453                                                input_section, info, rel,
4454                                                addend, howto, local_syms,
4455                                                local_sections, &value, &name,
4456                                                &require_jalx))
4457         {
4458         case bfd_reloc_continue:
4459           /* There's nothing to do.  */
4460           continue;
4461
4462         case bfd_reloc_undefined:
4463           /* mips_elf64_calculate_relocation already called the
4464              undefined_symbol callback.  There's no real point in
4465              trying to perform the relocation at this point, so we
4466              just skip ahead to the next relocation.  */
4467           continue;
4468
4469         case bfd_reloc_notsupported:
4470           msg = _("internal error: unsupported relocation error");
4471           info->callbacks->warning
4472             (info, msg, name, input_bfd, input_section, rel->r_offset);
4473           return false;
4474
4475         case bfd_reloc_overflow:
4476           if (use_saved_addend_p)
4477             /* Ignore overflow until we reach the last relocation for
4478                a given location.  */
4479             ;
4480           else
4481             {
4482               BFD_ASSERT (name != NULL);
4483               if (! ((*info->callbacks->reloc_overflow)
4484                      (info, name, howto->name, (bfd_vma) 0,
4485                       input_bfd, input_section, rel->r_offset)))
4486                 return false;
4487             }
4488           break;
4489
4490         case bfd_reloc_ok:
4491           break;
4492
4493         default:
4494           abort ();
4495           break;
4496         }
4497
4498       /* If we've got another relocation for the address, keep going
4499          until we reach the last one.  */
4500       if (use_saved_addend_p)
4501         {
4502           addend = value;
4503           continue;
4504         }
4505
4506       /* Actually perform the relocation.  */
4507       if (!mips_elf64_perform_relocation (info, howto, rel, value, input_bfd,
4508                                           input_section, contents,
4509                                           require_jalx))
4510         return false;
4511     }
4512
4513   return true;
4514 }
4515
4516 /* Create dynamic sections when linking against a dynamic object.  */
4517
4518 boolean
4519 mips_elf64_create_dynamic_sections (abfd, info)
4520      bfd *abfd;
4521      struct bfd_link_info *info;
4522 {
4523   flagword flags;
4524   register asection *s;
4525
4526   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4527            | SEC_LINKER_CREATED | SEC_READONLY);
4528
4529   /* Mips ABI requests the .dynamic section to be read only.  */
4530   s = bfd_get_section_by_name (abfd, ".dynamic");
4531   if (s != NULL)
4532     {
4533       if (! bfd_set_section_flags (abfd, s, flags))
4534         return false;
4535     }
4536
4537   /* We need to create .got section.  */
4538   if (! mips_elf64_create_got_section (abfd, info))
4539     return false;
4540
4541   /* Create the .msym section on IRIX6.  It is used by the dynamic
4542      linker to speed up dynamic relocations, and to avoid computing
4543      the ELF hash for symbols.  */
4544   if (!mips_elf64_create_msym_section (abfd))
4545     return false;
4546
4547   /* Create .stub section.  */
4548   if (bfd_get_section_by_name (abfd, ".MIPS.stubs") == NULL)
4549     {
4550       s = bfd_make_section (abfd, ".MIPS.stubs");
4551       if (s == NULL
4552           || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
4553           || ! bfd_set_section_alignment (abfd, s, 3))
4554         return false;
4555     }
4556
4557   return true;
4558 }
4559
4560 /* Adjust a symbol defined by a dynamic object and referenced by a
4561    regular object.  The current definition is in some section of the
4562    dynamic object, but we're not including those sections.  We have to
4563    change the definition to something the rest of the link can
4564    understand.  */
4565
4566 boolean
4567 mips_elf64_adjust_dynamic_symbol (info, h)
4568      struct bfd_link_info *info;
4569      struct elf_link_hash_entry *h;
4570 {
4571   bfd *dynobj;
4572   struct mips_elf64_link_hash_entry *hmips;
4573   asection *s;
4574
4575   dynobj = elf_hash_table (info)->dynobj;
4576
4577   /* Make sure we know what is going on here.  */
4578   BFD_ASSERT (dynobj != NULL
4579               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
4580                   || h->weakdef != NULL
4581                   || ((h->elf_link_hash_flags
4582                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4583                       && (h->elf_link_hash_flags
4584                           & ELF_LINK_HASH_REF_REGULAR) != 0
4585                       && (h->elf_link_hash_flags
4586                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
4587
4588   /* If this symbol is defined in a dynamic object, we need to copy
4589      any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
4590      file.  */
4591   hmips = (struct mips_elf64_link_hash_entry *) h;
4592   if (! info->relocateable
4593       && hmips->possibly_dynamic_relocs != 0
4594       && (h->root.type == bfd_link_hash_defweak
4595           || (h->elf_link_hash_flags
4596               & ELF_LINK_HASH_DEF_REGULAR) == 0))
4597     {
4598       mips_elf64_allocate_dynamic_relocations (dynobj,
4599                                                hmips->possibly_dynamic_relocs);
4600       if (hmips->readonly_reloc)
4601         /* We tell the dynamic linker that there are relocations
4602            against the text segment.  */
4603         info->flags |= DF_TEXTREL;
4604     }
4605
4606   /* For a function, create a stub, if allowed.  */
4607   if (! hmips->no_fn_stub
4608       && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4609     {
4610       if (! elf_hash_table (info)->dynamic_sections_created)
4611         return true;
4612
4613       /* If this symbol is not defined in a regular file, then set
4614          the symbol to the stub location.  This is required to make
4615          function pointers compare as equal between the normal
4616          executable and the shared library.  */
4617       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4618         {
4619           /* We need .stub section.  */
4620           s = bfd_get_section_by_name (dynobj, ".MIPS.stubs");
4621           BFD_ASSERT (s != NULL);
4622
4623           h->root.u.def.section = s;
4624           h->root.u.def.value = s->_raw_size;
4625
4626           /* XXX Write this stub address somewhere.  */
4627           h->plt.offset = s->_raw_size;
4628
4629           /* Make room for this stub code.  */
4630           s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4631
4632           /* The last half word of the stub will be filled with the index
4633              of this symbol in .dynsym section.  */
4634           return true;
4635         }
4636     }
4637   else if ((h->type == STT_FUNC)
4638            && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4639     {
4640       /* This will set the entry for this symbol in the GOT to 0, and
4641          the dynamic linker will take care of this.  */
4642       h->root.u.def.value = 0;
4643       return true;
4644     }
4645
4646   /* If this is a weak symbol, and there is a real definition, the
4647      processor independent code will have arranged for us to see the
4648      real definition first, and we can just use the same value.  */
4649   if (h->weakdef != NULL)
4650     {
4651       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4652                   || h->weakdef->root.type == bfd_link_hash_defweak);
4653       h->root.u.def.section = h->weakdef->root.u.def.section;
4654       h->root.u.def.value = h->weakdef->root.u.def.value;
4655       return true;
4656     }
4657
4658   /* This is a reference to a symbol defined by a dynamic object which
4659      is not a function.  */
4660
4661   return true;
4662 }
4663
4664 /* This function is called after all the input files have been read,
4665    and the input sections have been assigned to output sections.  */
4666
4667 boolean
4668 mips_elf64_always_size_sections (output_bfd, info)
4669      bfd *output_bfd ATTRIBUTE_UNUSED;
4670      struct bfd_link_info *info ATTRIBUTE_UNUSED;
4671 {
4672   if (info->relocateable
4673       || ! mips_elf64_hash_table (info)->mips16_stubs_seen)
4674     return true;
4675
4676   mips_elf64_link_hash_traverse (mips_elf64_hash_table (info),
4677                                  mips_elf64_check_mips16_stubs,
4678                                  (PTR) NULL);
4679
4680   return true;
4681 }
4682
4683 /* Check the mips16 stubs for a particular symbol, and see if we can
4684    discard them.  */
4685
4686 static boolean
4687 mips_elf64_check_mips16_stubs (h, data)
4688      struct mips_elf64_link_hash_entry *h;
4689      PTR data ATTRIBUTE_UNUSED;
4690 {
4691   if (h->root.root.type == bfd_link_hash_warning)
4692     h = (struct mips_elf64_link_hash_entry *) h->root.root.u.i.link;
4693
4694   if (h->fn_stub != NULL
4695       && ! h->need_fn_stub)
4696     {
4697       /* We don't need the fn_stub; the only references to this symbol
4698          are 16 bit calls.  Clobber the size to 0 to prevent it from
4699          being included in the link.  */
4700       h->fn_stub->_raw_size = 0;
4701       h->fn_stub->_cooked_size = 0;
4702       h->fn_stub->flags &= ~SEC_RELOC;
4703       h->fn_stub->reloc_count = 0;
4704       h->fn_stub->flags |= SEC_EXCLUDE;
4705     }
4706
4707   if (h->call_stub != NULL
4708       && h->root.other == STO_MIPS16)
4709     {
4710       /* We don't need the call_stub; this is a 16 bit function, so
4711          calls from other 16 bit functions are OK.  Clobber the size
4712          to 0 to prevent it from being included in the link.  */
4713       h->call_stub->_raw_size = 0;
4714       h->call_stub->_cooked_size = 0;
4715       h->call_stub->flags &= ~SEC_RELOC;
4716       h->call_stub->reloc_count = 0;
4717       h->call_stub->flags |= SEC_EXCLUDE;
4718     }
4719
4720   if (h->call_fp_stub != NULL
4721       && h->root.other == STO_MIPS16)
4722     {
4723       /* We don't need the call_stub; this is a 16 bit function, so
4724          calls from other 16 bit functions are OK.  Clobber the size
4725          to 0 to prevent it from being included in the link.  */
4726       h->call_fp_stub->_raw_size = 0;
4727       h->call_fp_stub->_cooked_size = 0;
4728       h->call_fp_stub->flags &= ~SEC_RELOC;
4729       h->call_fp_stub->reloc_count = 0;
4730       h->call_fp_stub->flags |= SEC_EXCLUDE;
4731     }
4732
4733   return true;
4734 }
4735
4736 /* Set the sizes of the dynamic sections.  */
4737
4738 boolean
4739 mips_elf64_size_dynamic_sections (output_bfd, info)
4740      bfd *output_bfd;
4741      struct bfd_link_info *info;
4742 {
4743   bfd *dynobj;
4744   asection *s;
4745   boolean reltext;
4746   struct mips_elf64_got_info *g = NULL;
4747
4748   dynobj = elf_hash_table (info)->dynobj;
4749   BFD_ASSERT (dynobj != NULL);
4750
4751   if (elf_hash_table (info)->dynamic_sections_created)
4752     {
4753       /* Set the contents of the .interp section to the interpreter.  */
4754       if (! info->shared)
4755         {
4756           s = bfd_get_section_by_name (dynobj, ".interp");
4757           BFD_ASSERT (s != NULL);
4758           s->_raw_size = strlen ("/usr/lib64/libc.so.1") + 1;
4759           s->contents = (bfd_byte *) "/usr/lib64/libc.so.1";
4760         }
4761     }
4762
4763   /* The check_relocs and adjust_dynamic_symbol entry points have
4764      determined the sizes of the various dynamic sections.  Allocate
4765      memory for them.  */
4766   reltext = false;
4767   for (s = dynobj->sections; s != NULL; s = s->next)
4768     {
4769       const char *name;
4770       boolean strip;
4771
4772       /* It's OK to base decisions on the section name, because none
4773          of the dynobj section names depend upon the input files.  */
4774       name = bfd_get_section_name (dynobj, s);
4775
4776       if ((s->flags & SEC_LINKER_CREATED) == 0)
4777         continue;
4778
4779       strip = false;
4780
4781       if (strncmp (name, ".rel", 4) == 0)
4782         {
4783           if (s->_raw_size == 0)
4784             {
4785               /* We only strip the section if the output section name
4786                  has the same name.  Otherwise, there might be several
4787                  input sections for this output section.  FIXME: This
4788                  code is probably not needed these days anyhow, since
4789                  the linker now does not create empty output sections.  */
4790               if (s->output_section != NULL
4791                   && strcmp (name,
4792                              bfd_get_section_name (s->output_section->owner,
4793                                                    s->output_section)) == 0)
4794                 strip = true;
4795             }
4796           else
4797             {
4798               const char *outname;
4799               asection *target;
4800
4801               /* If this relocation section applies to a read only
4802                  section, then we probably need a DT_TEXTREL entry.
4803                  If the relocation section is .rel.dyn, we always
4804                  assert a DT_TEXTREL entry rather than testing whether
4805                  there exists a relocation to a read only section or
4806                  not.  */
4807               outname = bfd_get_section_name (output_bfd,
4808                                               s->output_section);
4809               target = bfd_get_section_by_name (output_bfd, outname + 4);
4810               if ((target != NULL
4811                    && (target->flags & SEC_READONLY) != 0
4812                    && (target->flags & SEC_ALLOC) != 0)
4813                   || strcmp (outname, "rel.dyn") == 0)
4814                 reltext = true;
4815
4816               /* We use the reloc_count field as a counter if we need
4817                  to copy relocs into the output file.  */
4818               if (strcmp (name, "rel.dyn") != 0)
4819                 s->reloc_count = 0;
4820             }
4821         }
4822       else if (strncmp (name, ".got", 4) == 0)
4823         {
4824           int i;
4825           bfd_size_type loadable_size = 0;
4826           bfd_size_type local_gotno;
4827           bfd *sub;
4828
4829           BFD_ASSERT (elf_section_data (s) != NULL);
4830           g = (struct mips_elf64_got_info *) elf_section_data (s)->tdata;
4831           BFD_ASSERT (g != NULL);
4832
4833           /* Calculate the total loadable size of the output.  That
4834              will give us the maximum number of GOT_PAGE entries
4835              required.  */
4836           for (sub = info->input_bfds; sub; sub = sub->link_next)
4837             {
4838               asection *subsection;
4839
4840               for (subsection = sub->sections;
4841                    subsection;
4842                    subsection = subsection->next)
4843                 {
4844                   if ((subsection->flags & SEC_ALLOC) == 0)
4845                     continue;
4846                   loadable_size += (subsection->_raw_size + 0xf) & ~0xf;
4847                 }
4848             }
4849           loadable_size += MIPS_FUNCTION_STUB_SIZE;
4850
4851           /* Assume there are two loadable segments consisting of
4852              contiguous sections.  Is 5 enough?  */
4853           local_gotno = (loadable_size >> 16) + 5;
4854             /* It's possible we will need GOT_PAGE entries as well as
4855                GOT16 entries.  Often, these will be able to share GOT
4856                entries, but not always.  */
4857             local_gotno *= 2;
4858
4859           g->local_gotno += local_gotno;
4860           s->_raw_size += local_gotno * 8;
4861
4862           /* There has to be a global GOT entry for every symbol with
4863              a dynamic symbol table index of DT_MIPS_GOTSYM or
4864              higher.  Therefore, it make sense to put those symbols
4865              that need GOT entries at the end of the symbol table.  We
4866              do that here.  */
4867           if (!mips_elf64_sort_hash_table (info, 1))
4868             return false;
4869
4870           if (g->global_gotsym != NULL)
4871             i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
4872           else
4873             /* If there are no global symbols, or none requiring
4874                relocations, then GLOBAL_GOTSYM will be NULL.  */
4875             i = 0;
4876           g->global_gotno = i;
4877           s->_raw_size += i * 8;
4878         }
4879       else if (strcmp (name, ".MIPS.stubs") == 0)
4880         {
4881           /* Irix rld assumes that the function stub isn't at the end
4882              of .text section. So put a dummy. XXX  */
4883           s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4884         }
4885       else if (strcmp (name, ".msym")
4886                == 0)
4887         s->_raw_size = (sizeof (Elf32_External_Msym)
4888                         * (elf_hash_table (info)->dynsymcount
4889                            + bfd_count_sections (output_bfd)));
4890       else if (strncmp (name, ".init", 5) != 0)
4891         {
4892           /* It's not one of our sections, so don't allocate space.  */
4893           continue;
4894         }
4895
4896       if (strip)
4897         {
4898           _bfd_strip_section_from_output (info, s);
4899           continue;
4900         }
4901
4902       /* Allocate memory for the section contents.  */
4903       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4904       if (s->contents == NULL && s->_raw_size != 0)
4905         {
4906           bfd_set_error (bfd_error_no_memory);
4907           return false;
4908         }
4909     }
4910
4911   if (elf_hash_table (info)->dynamic_sections_created)
4912     {
4913       /* Add some entries to the .dynamic section.  We fill in the
4914          values later, in elf_mips_finish_dynamic_sections, but we
4915          must add the entries now so that we get the correct size for
4916          the .dynamic section.  The DT_DEBUG entry is filled in by the
4917          dynamic linker and used by the debugger.  */
4918       if (! info->shared)
4919         {
4920           /* SGI object has the equivalence of DT_DEBUG in the
4921              DT_MIPS_RLD_MAP entry.  */
4922           if (!bfd_elf64_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
4923             return false;
4924           if (!SGI_COMPAT (output_bfd))
4925             {
4926               if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
4927                 return false;
4928             }
4929         }
4930       else
4931         {
4932           /* Shared libraries on traditional mips have DT_DEBUG.  */
4933           if (!SGI_COMPAT (output_bfd))
4934             {
4935               if (!bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
4936                 return false;
4937             }
4938         }
4939
4940       if (reltext && SGI_COMPAT (output_bfd))
4941         info->flags |= DF_TEXTREL;
4942
4943       if ((info->flags & DF_TEXTREL) != 0)
4944         {
4945           if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
4946             return false;
4947         }
4948
4949       if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0))
4950         return false;
4951
4952       if (bfd_get_section_by_name (dynobj, "rel.dyn"))
4953         {
4954           if (! bfd_elf64_add_dynamic_entry (info, DT_REL, 0))
4955             return false;
4956
4957           if (! bfd_elf64_add_dynamic_entry (info, DT_RELSZ, 0))
4958             return false;
4959
4960           if (! bfd_elf64_add_dynamic_entry (info, DT_RELENT, 0))
4961             return false;
4962         }
4963
4964       if (SGI_COMPAT (output_bfd))
4965         {
4966           if (!bfd_elf64_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
4967             return false;
4968         }
4969
4970       if (SGI_COMPAT (output_bfd))
4971         {
4972           if (!bfd_elf64_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
4973             return false;
4974         }
4975
4976       if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
4977         {
4978           if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
4979             return false;
4980
4981           s = bfd_get_section_by_name (dynobj, ".liblist");
4982           BFD_ASSERT (s != NULL);
4983
4984           if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
4985             return false;
4986         }
4987
4988       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
4989         return false;
4990
4991       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
4992         return false;
4993
4994 #if 0
4995       /* Time stamps in executable files are a bad idea.  */
4996       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
4997         return false;
4998 #endif
4999
5000 #if 0 /* FIXME  */
5001       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
5002         return false;
5003 #endif
5004
5005 #if 0 /* FIXME  */
5006       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
5007         return false;
5008 #endif
5009
5010       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
5011         return false;
5012
5013       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
5014         return false;
5015
5016       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
5017         return false;
5018
5019       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
5020         return false;
5021
5022       if (! bfd_elf64_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
5023         return false;
5024
5025       if ((bfd_get_section_by_name(dynobj, ".MIPS.options"))
5026           && !bfd_elf64_add_dynamic_entry (info, DT_MIPS_OPTIONS, 0))
5027         return false;
5028
5029       if (bfd_get_section_by_name (dynobj, ".msym")
5030           && !bfd_elf64_add_dynamic_entry (info, DT_MIPS_MSYM, 0))
5031         return false;
5032     }
5033
5034   return true;
5035 }
5036
5037 /* Finish up dynamic symbol handling.  We set the contents of various
5038    dynamic sections here.  */
5039
5040 boolean
5041 mips_elf64_finish_dynamic_symbol (output_bfd, info, h, sym)
5042      bfd *output_bfd;
5043      struct bfd_link_info *info;
5044      struct elf_link_hash_entry *h;
5045      Elf_Internal_Sym *sym;
5046 {
5047   bfd *dynobj;
5048   bfd_vma gval;
5049   asection *sgot;
5050   asection *smsym;
5051   struct mips_elf64_got_info *g;
5052   const char *name;
5053   struct mips_elf64_link_hash_entry *mh;
5054
5055   dynobj = elf_hash_table (info)->dynobj;
5056   gval = sym->st_value;
5057   mh = (struct mips_elf64_link_hash_entry *) h;
5058
5059   if (h->plt.offset != (bfd_vma) -1)
5060     {
5061       asection *s;
5062       bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
5063
5064       /* This symbol has a stub.  Set it up.  */
5065
5066       BFD_ASSERT (h->dynindx != -1);
5067
5068       s = bfd_get_section_by_name (dynobj, ".MIPS.stubs");
5069       BFD_ASSERT (s != NULL);
5070
5071       /* FIXME: Can h->dynindex be more than 64K?  */
5072       if (h->dynindx & 0xffff0000)
5073         return false;
5074
5075       /* Fill the stub.  */
5076       bfd_put_32 (output_bfd, STUB_LW, stub);
5077       bfd_put_32 (output_bfd, STUB_MOVE, stub + 4);
5078       bfd_put_32 (output_bfd, STUB_JALR, stub + 8);
5079       bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, stub + 12);
5080
5081       BFD_ASSERT (h->plt.offset <= s->_raw_size);
5082       memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
5083
5084       /* Mark the symbol as undefined.  plt.offset != -1 occurs
5085          only for the referenced symbol.  */
5086       sym->st_shndx = SHN_UNDEF;
5087
5088       /* The run-time linker uses the st_value field of the symbol
5089          to reset the global offset table entry for this external
5090          to its stub address when unlinking a shared object.  */
5091       gval = s->output_section->vma + s->output_offset + h->plt.offset;
5092       sym->st_value = gval;
5093     }
5094
5095   BFD_ASSERT (h->dynindx != -1
5096               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0);
5097
5098   sgot = bfd_get_section_by_name (dynobj, ".got");
5099   BFD_ASSERT (sgot != NULL);
5100   BFD_ASSERT (elf_section_data (sgot) != NULL);
5101   g = (struct mips_elf64_got_info *) elf_section_data (sgot)->tdata;
5102   BFD_ASSERT (g != NULL);
5103
5104   /* Run through the global symbol table, creating GOT entries for all
5105      the symbols that need them.  */
5106   if (g->global_gotsym != NULL
5107       && h->dynindx >= g->global_gotsym->dynindx)
5108     {
5109       bfd_vma offset;
5110       bfd_vma value;
5111
5112       if (sym->st_value)
5113         value = sym->st_value;
5114       else
5115         {
5116           /* For an entity defined in a shared object, this will be
5117              NULL.  (For functions in shared objects for
5118              which we have created stubs, ST_VALUE will be non-NULL.
5119              That's because such the functions are now no longer defined
5120              in a shared object.)  */
5121
5122           if (info->shared && h->root.type == bfd_link_hash_undefined)
5123             value = 0;
5124           else
5125             value = h->root.u.def.value;
5126         }
5127       offset = mips_elf64_global_got_index (dynobj, h);
5128       bfd_put_64 (output_bfd, value, sgot->contents + offset);
5129     }
5130
5131   /* Create a .msym entry, if appropriate.  */
5132   smsym = bfd_get_section_by_name (dynobj, ".msym");
5133   if (smsym)
5134     {
5135       Elf32_Internal_Msym msym;
5136
5137       msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
5138       /* It is undocumented what the `1' indicates, but IRIX6 uses
5139          this value.  */
5140       msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
5141       mips_elf64_swap_msym_out
5142         (dynobj, &msym,
5143          ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
5144     }
5145
5146   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
5147   name = h->root.root.string;
5148   if (strcmp (name, "_DYNAMIC") == 0
5149       || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
5150     sym->st_shndx = SHN_ABS;
5151   else if (strcmp (name, "_DYNAMIC_LINK") == 0
5152            || strcmp (name, "_DYNAMIC_LINKING") == 0)
5153     {
5154       sym->st_shndx = SHN_ABS;
5155       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5156       sym->st_value = 1;
5157     }
5158   else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
5159     {
5160       if (h->type == STT_FUNC)
5161         sym->st_shndx = SHN_MIPS_TEXT;
5162       else if (h->type == STT_OBJECT)
5163         sym->st_shndx = SHN_MIPS_DATA;
5164     }
5165
5166   /* Handle the IRIX6-specific symbols.  */
5167
5168     {
5169   /* The linker script takes care of providing names and values for
5170      these, but we must place them into the right sections.  */
5171   static const char* const text_section_symbols[] = {
5172     "_ftext",
5173     "_etext",
5174     "__dso_displacement",
5175     "__elf_header",
5176     "__program_header_table",
5177     NULL
5178   };
5179
5180   static const char* const data_section_symbols[] = {
5181     "_fdata",
5182     "_edata",
5183     "_end",
5184     "_fbss",
5185     NULL
5186   };
5187
5188   const char* const *p;
5189   int i;
5190
5191   for (i = 0; i < 2; ++i)
5192     for (p = (i == 0) ? text_section_symbols : data_section_symbols;
5193          *p;
5194          ++p)
5195       if (strcmp (*p, name) == 0)
5196         {
5197           /* All of these symbols are given type STT_SECTION by the
5198              IRIX6 linker.  */
5199           sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5200
5201           /* The IRIX linker puts these symbols in special sections.  */
5202           if (i == 0)
5203             sym->st_shndx = SHN_MIPS_TEXT;
5204           else
5205             sym->st_shndx = SHN_MIPS_DATA;
5206
5207           break;
5208         }
5209     }
5210
5211   return true;
5212 }
5213
5214 /* Finish up the dynamic sections.  */
5215
5216 boolean
5217 mips_elf64_finish_dynamic_sections (output_bfd, info)
5218      bfd *output_bfd;
5219      struct bfd_link_info *info;
5220 {
5221   bfd *dynobj;
5222   asection *sdyn;
5223   asection *sgot;
5224   struct mips_elf64_got_info *g;
5225
5226   dynobj = elf_hash_table (info)->dynobj;
5227
5228   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5229
5230   sgot = bfd_get_section_by_name (dynobj, ".got");
5231   if (sgot == NULL)
5232     g = NULL;
5233   else
5234     {
5235       BFD_ASSERT (elf_section_data (sgot) != NULL);
5236       g = (struct mips_elf64_got_info *) elf_section_data (sgot)->tdata;
5237       BFD_ASSERT (g != NULL);
5238     }
5239
5240   if (elf_hash_table (info)->dynamic_sections_created)
5241     {
5242       bfd_byte *b;
5243
5244       BFD_ASSERT (sdyn != NULL);
5245       BFD_ASSERT (g != NULL);
5246
5247       for (b = sdyn->contents;
5248            b < sdyn->contents + sdyn->_raw_size;
5249            b += get_elf_backend_data (dynobj)->s->sizeof_dyn)
5250         {
5251           Elf_Internal_Dyn dyn;
5252           const char *name;
5253           size_t elemsize;
5254           asection *s;
5255           boolean swap_out_p;
5256
5257           /* Read in the current dynamic entry.  */
5258           (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
5259
5260           /* Assume that we're going to modify it and write it out.  */
5261           swap_out_p = true;
5262
5263           switch (dyn.d_tag)
5264             {
5265             case DT_RELENT:
5266               s = bfd_get_section_by_name(dynobj, "rel.dyn");
5267               BFD_ASSERT (s != NULL);
5268               dyn.d_un.d_val = get_elf_backend_data (dynobj)->s->sizeof_rel;
5269               break;
5270
5271             case DT_STRSZ:
5272               /* Rewrite DT_STRSZ.  */
5273               dyn.d_un.d_val =
5274                 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5275               break;
5276
5277             case DT_PLTGOT:
5278               name = ".got";
5279               goto get_vma;
5280             case DT_MIPS_CONFLICT:
5281               name = ".conflict";
5282               goto get_vma;
5283             case DT_MIPS_LIBLIST:
5284               name = ".liblist";
5285             get_vma:
5286               s = bfd_get_section_by_name (output_bfd, name);
5287               BFD_ASSERT (s != NULL);
5288               dyn.d_un.d_ptr = s->vma;
5289               break;
5290
5291             case DT_MIPS_RLD_VERSION:
5292               dyn.d_un.d_val = 1; /* XXX */
5293               break;
5294
5295             case DT_MIPS_FLAGS:
5296               dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
5297               break;
5298
5299             case DT_MIPS_CONFLICTNO:
5300               name = ".conflict";
5301               elemsize = sizeof (Elf32_Conflict);
5302               goto set_elemno;
5303
5304             case DT_MIPS_LIBLISTNO:
5305               name = ".liblist";
5306               elemsize = sizeof (Elf32_Lib);
5307             set_elemno:
5308               s = bfd_get_section_by_name (output_bfd, name);
5309               if (s != NULL)
5310                 {
5311                   if (s->_cooked_size != 0)
5312                     dyn.d_un.d_val = s->_cooked_size / elemsize;
5313                   else
5314                     dyn.d_un.d_val = s->_raw_size / elemsize;
5315                 }
5316               else
5317                 dyn.d_un.d_val = 0;
5318               break;
5319
5320             case DT_MIPS_TIME_STAMP:
5321               time ((time_t *) &dyn.d_un.d_val);
5322               break;
5323
5324             case DT_MIPS_ICHECKSUM:
5325               /* XXX FIXME: */
5326               swap_out_p = false;
5327               break;
5328
5329             case DT_MIPS_IVERSION:
5330               /* XXX FIXME: */
5331               swap_out_p = false;
5332               break;
5333
5334             case DT_MIPS_BASE_ADDRESS:
5335               s = output_bfd->sections;
5336               BFD_ASSERT (s != NULL);
5337               dyn.d_un.d_ptr = s->vma & ~(0xffff);
5338               break;
5339
5340             case DT_MIPS_LOCAL_GOTNO:
5341               dyn.d_un.d_val = g->local_gotno;
5342               break;
5343
5344             case DT_MIPS_UNREFEXTNO:
5345               /* The index into the dynamic symbol table which is the
5346                  entry of the first external symbol that is not
5347                  referenced within the same object.  */
5348               dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
5349               break;
5350
5351             case DT_MIPS_GOTSYM:
5352               if (g->global_gotsym)
5353                 {
5354                   dyn.d_un.d_val = g->global_gotsym->dynindx;
5355                   break;
5356                 }
5357               /* In case if we don't have global got symbols we default
5358                  to setting DT_MIPS_GOTSYM to the same value as
5359                  DT_MIPS_SYMTABNO, so we just fall through.  */
5360
5361             case DT_MIPS_SYMTABNO:
5362               name = ".dynsym";
5363               elemsize = get_elf_backend_data (output_bfd)->s->sizeof_sym;
5364               s = bfd_get_section_by_name (output_bfd, name);
5365               BFD_ASSERT (s != NULL);
5366
5367               if (s->_cooked_size != 0)
5368                 dyn.d_un.d_val = s->_cooked_size / elemsize;
5369               else
5370                 dyn.d_un.d_val = s->_raw_size / elemsize;
5371               break;
5372
5373             case DT_MIPS_HIPAGENO:
5374               dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
5375               break;
5376
5377             case DT_MIPS_OPTIONS:
5378               s = bfd_get_section_by_name(output_bfd, ".MIPS.options");
5379               dyn.d_un.d_ptr = s->vma;
5380               break;
5381
5382             case DT_MIPS_MSYM:
5383               s = bfd_get_section_by_name(output_bfd, ".msym");
5384               dyn.d_un.d_ptr = s->vma;
5385               break;
5386
5387             default:
5388               swap_out_p = false;
5389               break;
5390             }
5391
5392           if (swap_out_p)
5393             (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
5394               (dynobj, &dyn, b);
5395         }
5396     }
5397
5398   /* The first entry of the global offset table will be filled at
5399      runtime. The second entry will be used by some runtime loaders.
5400      This isn't the case of Irix rld.  */
5401   if (sgot != NULL && sgot->_raw_size > 0)
5402     {
5403       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
5404       bfd_put_64 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 8);
5405     }
5406
5407   if (sgot != NULL)
5408     elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 8;
5409
5410   {
5411     asection *smsym;
5412     asection *s;
5413
5414     /* ??? The section symbols for the output sections were set up in
5415        _bfd_elf_final_link.  SGI sets the STT_NOTYPE attribute for these
5416        symbols.  Should we do so?  */
5417
5418     smsym = bfd_get_section_by_name (dynobj, ".msym");
5419     if (smsym != NULL)
5420       {
5421         Elf32_Internal_Msym msym;
5422
5423         msym.ms_hash_value = 0;
5424         msym.ms_info = ELF32_MS_INFO (0, 1);
5425
5426         for (s = output_bfd->sections; s != NULL; s = s->next)
5427           {
5428             long dynindx = elf_section_data (s)->dynindx;
5429
5430             mips_elf64_swap_msym_out
5431               (output_bfd, &msym,
5432                (((Elf32_External_Msym *) smsym->contents)
5433                 + dynindx));
5434           }
5435       }
5436
5437     /* Clean up a first relocation in .rel.dyn.  */
5438     s = bfd_get_section_by_name (dynobj, "rel.dyn");
5439     if (s != NULL && s->_raw_size > 0)
5440       memset (s->contents, 0, get_elf_backend_data (dynobj)->s->sizeof_rel);
5441   }
5442
5443   return true;
5444 }
5445
5446 /* Return the section that should be marked against GC for a given
5447    relocation.  */
5448
5449 asection *
5450 mips_elf64_gc_mark_hook (abfd, info, rel, h, sym)
5451      bfd *abfd;
5452      struct bfd_link_info *info ATTRIBUTE_UNUSED;
5453      Elf_Internal_Rela *rel;
5454      struct elf_link_hash_entry *h;
5455      Elf_Internal_Sym *sym;
5456 {
5457   if (h != NULL)
5458     {
5459       switch (ELF64_R_TYPE (rel->r_info))
5460         {
5461         case R_MIPS_GNU_VTINHERIT:
5462         case R_MIPS_GNU_VTENTRY:
5463           break;
5464
5465         default:
5466           switch (h->root.type)
5467             {
5468             case bfd_link_hash_defined:
5469             case bfd_link_hash_defweak:
5470               return h->root.u.def.section;
5471
5472             case bfd_link_hash_common:
5473               return h->root.u.c.p->section;
5474
5475             default:
5476               break;
5477             }
5478         }
5479     }
5480   else
5481     {
5482       return bfd_section_from_elf_index (abfd, sym->st_shndx);
5483     }
5484
5485   return NULL;
5486 }
5487
5488 /* Update the got entry reference counts for the section being removed.  */
5489
5490 boolean
5491 mips_elf64_gc_sweep_hook (abfd, info, sec, relocs)
5492      bfd *abfd ATTRIBUTE_UNUSED;
5493      struct bfd_link_info *info ATTRIBUTE_UNUSED;
5494      asection *sec ATTRIBUTE_UNUSED;
5495      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
5496 {
5497 #if 0
5498   Elf_Internal_Shdr *symtab_hdr;
5499   struct elf_link_hash_entry **sym_hashes;
5500   bfd_signed_vma *local_got_refcounts;
5501   const Elf_Internal_Rela *rel, *relend;
5502   unsigned long r_symndx;
5503   struct elf_link_hash_entry *h;
5504
5505   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5506   sym_hashes = elf_sym_hashes (abfd);
5507   local_got_refcounts = elf_local_got_refcounts (abfd);
5508
5509   relend = relocs + sec->reloc_count;
5510   for (rel = relocs; rel < relend; rel++)
5511     switch (ELF64_R_TYPE (rel->r_info))
5512       {
5513       case R_MIPS_GOT16:
5514       case R_MIPS_CALL16:
5515       case R_MIPS_CALL_HI16:
5516       case R_MIPS_CALL_LO16:
5517       case R_MIPS_GOT_HI16:
5518       case R_MIPS_GOT_LO16:
5519         /* ??? It would seem that the existing MIPS code does no sort
5520            of reference counting or whatnot on its GOT and PLT entries,
5521            so it is not possible to garbage collect them at this time.  */
5522         break;
5523
5524       default:
5525         break;
5526       }
5527 #endif
5528
5529   return true;
5530 }
5531 \f
5532 /* Create the .got section to hold the global offset table. */
5533
5534 static boolean
5535 mips_elf64_create_got_section (abfd, info)
5536      bfd *abfd;
5537      struct bfd_link_info *info;
5538 {
5539   flagword flags;
5540   register asection *s;
5541   struct elf_link_hash_entry *h;
5542   struct mips_elf64_got_info *g;
5543
5544   /* This function may be called more than once.  */
5545   if (bfd_get_section_by_name (abfd, ".got"))
5546     return true;
5547
5548   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5549            | SEC_LINKER_CREATED);
5550
5551   s = bfd_make_section (abfd, ".got");
5552   if (s == NULL
5553       || ! bfd_set_section_flags (abfd, s, flags)
5554       || ! bfd_set_section_alignment (abfd, s, 4))
5555     return false;
5556
5557   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
5558      linker script because we don't want to define the symbol if we
5559      are not creating a global offset table.  */
5560   h = NULL;
5561   if (! (_bfd_generic_link_add_one_symbol
5562          (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5563           (bfd_vma) 0, (const char *) NULL, false,
5564           get_elf_backend_data (abfd)->collect,
5565           (struct bfd_link_hash_entry **) &h)))
5566     return false;
5567   h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5568   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5569   h->type = STT_OBJECT;
5570
5571   if (info->shared
5572       && ! bfd_elf64_link_record_dynamic_symbol (info, h))
5573     return false;
5574
5575   /* The first several global offset table entries are reserved.  */
5576   s->_raw_size = MIPS_RESERVED_GOTNO * (get_elf_backend_data (abfd)->s->arch_size / 8);
5577
5578   g = (struct mips_elf64_got_info *) bfd_alloc (abfd,
5579                                           sizeof (struct mips_elf64_got_info));
5580   if (g == NULL)
5581     return false;
5582   g->global_gotsym = NULL;
5583   g->local_gotno = MIPS_RESERVED_GOTNO;
5584   g->assigned_gotno = MIPS_RESERVED_GOTNO;
5585   if (elf_section_data (s) == NULL)
5586     {
5587       s->used_by_bfd =
5588         (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5589       if (elf_section_data (s) == NULL)
5590         return false;
5591     }
5592   elf_section_data (s)->tdata = (PTR) g;
5593   elf_section_data (s)->this_hdr.sh_flags 
5594     |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5595
5596   return true;
5597 }
5598
5599 /* If H is a symbol that needs a global GOT entry, but has a dynamic
5600    symbol table index lower than any we've seen to date, record it for
5601    posterity.  */
5602
5603 static boolean
5604 mips_elf64_record_global_got_symbol (h, info, g)
5605      struct elf_link_hash_entry *h;
5606      struct bfd_link_info *info;
5607      struct mips_elf64_got_info *g ATTRIBUTE_UNUSED;
5608 {
5609   /* A global symbol in the GOT must also be in the dynamic symbol
5610      table.  */
5611   if (h->dynindx == -1
5612       && !bfd_elf64_link_record_dynamic_symbol (info, h))
5613     return false;
5614   
5615   /* If we've already marked this entry as needing GOT space, we don't
5616      need to do it again.  */
5617   if (h->got.offset != (bfd_vma) - 1)
5618     return true;
5619
5620   /* By setting this to a value other than -1, we are indicating that
5621      there needs to be a GOT entry for H.  Avoid using zero, as the
5622      generic ELF copy_indirect_symbol tests for <= 0.  */
5623   h->got.offset = 1;
5624
5625   return true;
5626 }
5627
5628 /* Returns the .msym section for ABFD, creating it if it does not
5629    already exist.  Returns NULL to indicate error.  */
5630
5631 static asection *
5632 mips_elf64_create_msym_section (abfd)
5633      bfd *abfd;
5634 {
5635   asection *s;
5636
5637   s = bfd_get_section_by_name (abfd, ".msym");
5638   if (!s)
5639     {
5640       s = bfd_make_section (abfd, ".msym");
5641       if (!s
5642           || !bfd_set_section_flags (abfd, s,
5643                                      SEC_ALLOC
5644                                      | SEC_LOAD
5645                                      | SEC_HAS_CONTENTS
5646                                      | SEC_LINKER_CREATED
5647                                      | SEC_READONLY)
5648           || !bfd_set_section_alignment (abfd, s, 3))
5649         return NULL;
5650     }
5651
5652   return s;
5653 }
5654
5655 /* Add room for N relocations to the .rel.dyn section in ABFD.  */
5656
5657 static void
5658 mips_elf64_allocate_dynamic_relocations (abfd, n)
5659      bfd *abfd;
5660      unsigned int n;
5661 {
5662   asection *s;
5663
5664   s = bfd_get_section_by_name (abfd, ".rel.dyn");
5665   BFD_ASSERT (s != NULL);
5666   
5667   if (s->_raw_size == 0)
5668     {
5669       /* Make room for a null element. */
5670       s->_raw_size += get_elf_backend_data (abfd)->s->sizeof_rel;
5671       ++s->reloc_count;
5672     }
5673   s->_raw_size += n * get_elf_backend_data (abfd)->s->sizeof_rel;
5674 }
5675
5676 /* Look through the relocs for a section during the first phase, and
5677    allocate space in the global offset table.  */
5678
5679 boolean
5680 mips_elf64_check_relocs (abfd, info, sec, relocs)
5681      bfd *abfd;
5682      struct bfd_link_info *info;
5683      asection *sec;
5684      const Elf_Internal_Rela *relocs;
5685 {
5686   const char *name;
5687   bfd *dynobj;
5688   Elf_Internal_Shdr *symtab_hdr;
5689   struct elf_link_hash_entry **sym_hashes;
5690   struct mips_elf64_got_info *g;
5691   size_t extsymoff;
5692   const Elf_Internal_Rela *rel;
5693   const Elf_Internal_Rela *rel_end;
5694   asection *sgot;
5695   asection *sreloc;
5696   struct elf_backend_data *bed;
5697
5698   if (info->relocateable)
5699     return true;
5700
5701   dynobj = elf_hash_table (info)->dynobj;
5702   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5703   sym_hashes = elf_sym_hashes (abfd);
5704   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5705
5706   /* Check for the mips16 stub sections.  */
5707
5708   name = bfd_get_section_name (abfd, sec);
5709   if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5710     {
5711       unsigned long r_symndx;
5712
5713       /* Look at the relocation information to figure out which symbol
5714          this is for.  */
5715
5716       r_symndx = ELF64_R_SYM (relocs->r_info);
5717
5718       if (r_symndx < extsymoff
5719           || sym_hashes[r_symndx - extsymoff] == NULL)
5720         {
5721           asection *o;
5722
5723           /* This stub is for a local symbol.  This stub will only be
5724              needed if there is some relocation in this BFD, other
5725              than a 16 bit function call, which refers to this symbol.  */
5726           for (o = abfd->sections; o != NULL; o = o->next)
5727             {
5728               Elf_Internal_Rela *sec_relocs;
5729               const Elf_Internal_Rela *r, *rend;
5730
5731               /* We can ignore stub sections when looking for relocs.  */
5732               if ((o->flags & SEC_RELOC) == 0
5733                   || o->reloc_count == 0
5734                   || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
5735                               sizeof FN_STUB - 1) == 0
5736                   || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
5737                               sizeof CALL_STUB - 1) == 0
5738                   || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
5739                               sizeof CALL_FP_STUB - 1) == 0)
5740                 continue;
5741
5742               sec_relocs = (_bfd_elf64_link_read_relocs
5743                             (abfd, o, (PTR) NULL,
5744                              (Elf_Internal_Rela *) NULL,
5745                              info->keep_memory));
5746               if (sec_relocs == NULL)
5747                 return false;
5748
5749               rend = sec_relocs + o->reloc_count;
5750               for (r = sec_relocs; r < rend; r++)
5751                 if (ELF64_R_SYM (r->r_info) == r_symndx
5752                     && ELF64_R_TYPE (r->r_info) != R_MIPS16_26)
5753                   break;
5754
5755               if (! info->keep_memory)
5756                 free (sec_relocs);
5757
5758               if (r < rend)
5759                 break;
5760             }
5761
5762           if (o == NULL)
5763             {
5764               /* There is no non-call reloc for this stub, so we do
5765                  not need it.  Since this function is called before
5766                  the linker maps input sections to output sections, we
5767                  can easily discard it by setting the SEC_EXCLUDE
5768                  flag.  */
5769               sec->flags |= SEC_EXCLUDE;
5770               return true;
5771             }
5772
5773           /* Record this stub in an array of local symbol stubs for
5774              this BFD.  */
5775           if (elf_tdata (abfd)->local_stubs == NULL)
5776             {
5777               unsigned long symcount;
5778               asection **n;
5779               bfd_size_type amt;
5780
5781               if (elf_bad_symtab (abfd))
5782                 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
5783               else
5784                 symcount = symtab_hdr->sh_info;
5785               amt = symcount * sizeof (asection *);
5786               n = (asection **) bfd_zalloc (abfd, amt);
5787               if (n == NULL)
5788                 return false;
5789               elf_tdata (abfd)->local_stubs = n;
5790             }
5791
5792           elf_tdata (abfd)->local_stubs[r_symndx] = sec;
5793
5794           /* We don't need to set mips16_stubs_seen in this case.
5795              That flag is used to see whether we need to look through
5796              the global symbol table for stubs.  We don't need to set
5797              it here, because we just have a local stub.  */
5798         }
5799       else
5800         {
5801           struct mips_elf64_link_hash_entry *h;
5802
5803           h = ((struct mips_elf64_link_hash_entry *)
5804                sym_hashes[r_symndx - extsymoff]);
5805
5806           /* H is the symbol this stub is for.  */
5807
5808           h->fn_stub = sec;
5809           mips_elf64_hash_table (info)->mips16_stubs_seen = true;
5810         }
5811     }
5812   else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
5813            || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5814     {
5815       unsigned long r_symndx;
5816       struct mips_elf64_link_hash_entry *h;
5817       asection **loc;
5818
5819       /* Look at the relocation information to figure out which symbol
5820          this is for.  */
5821
5822       r_symndx = ELF64_R_SYM (relocs->r_info);
5823
5824       if (r_symndx < extsymoff
5825           || sym_hashes[r_symndx - extsymoff] == NULL)
5826         {
5827           /* This stub was actually built for a static symbol defined
5828              in the same file.  We assume that all static symbols in
5829              mips16 code are themselves mips16, so we can simply
5830              discard this stub.  Since this function is called before
5831              the linker maps input sections to output sections, we can
5832              easily discard it by setting the SEC_EXCLUDE flag.  */
5833           sec->flags |= SEC_EXCLUDE;
5834           return true;
5835         }
5836
5837       h = ((struct mips_elf64_link_hash_entry *)
5838            sym_hashes[r_symndx - extsymoff]);
5839
5840       /* H is the symbol this stub is for.  */
5841
5842       if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5843         loc = &h->call_fp_stub;
5844       else
5845         loc = &h->call_stub;
5846
5847       /* If we already have an appropriate stub for this function, we
5848          don't need another one, so we can discard this one.  Since
5849          this function is called before the linker maps input sections
5850          to output sections, we can easily discard it by setting the
5851          SEC_EXCLUDE flag.  We can also discard this section if we
5852          happen to already know that this is a mips16 function; it is
5853          not necessary to check this here, as it is checked later, but
5854          it is slightly faster to check now.  */
5855       if (*loc != NULL || h->root.other == STO_MIPS16)
5856         {
5857           sec->flags |= SEC_EXCLUDE;
5858           return true;
5859         }
5860
5861       *loc = sec;
5862       mips_elf64_hash_table (info)->mips16_stubs_seen = true;
5863     }
5864
5865   if (dynobj == NULL)
5866     {
5867       sgot = NULL;
5868       g = NULL;
5869     }
5870   else
5871     {
5872       sgot = bfd_get_section_by_name (dynobj, ".got");
5873       if (sgot == NULL)
5874         g = NULL;
5875       else
5876         {
5877           BFD_ASSERT (elf_section_data (sgot) != NULL);
5878           g = (struct mips_elf64_got_info *) elf_section_data (sgot)->tdata;
5879           BFD_ASSERT (g != NULL);
5880         }
5881     }
5882
5883   sreloc = NULL;
5884   bed = get_elf_backend_data (abfd);
5885   rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
5886   for (rel = relocs; rel < rel_end; ++rel)
5887     {
5888       unsigned long r_symndx;
5889       int r_type;
5890       struct elf_link_hash_entry *h;
5891
5892       r_symndx = ELF64_R_SYM (rel->r_info);
5893       r_type = ELF64_MIPS_R_TYPE (rel->r_info);
5894
5895       if (r_symndx < extsymoff)
5896         h = NULL;
5897       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
5898         {
5899           (*_bfd_error_handler)
5900             (_("%s: Malformed reloc detected for section %s"),
5901              bfd_archive_filename (abfd), name);
5902           bfd_set_error (bfd_error_bad_value);
5903           return false;
5904         }
5905       else
5906         {
5907           h = sym_hashes[r_symndx - extsymoff];
5908
5909           /* This may be an indirect symbol created because of a version.  */
5910           if (h != NULL)
5911             {
5912               while (h->root.type == bfd_link_hash_indirect)
5913                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5914             }
5915         }
5916
5917       /* Some relocs require a global offset table.  */
5918       if (dynobj == NULL || sgot == NULL)
5919         {
5920           switch (r_type)
5921             {
5922             case R_MIPS_GOT16:
5923             case R_MIPS_CALL16:
5924             case R_MIPS_CALL_HI16:
5925             case R_MIPS_CALL_LO16:
5926             case R_MIPS_GOT_HI16:
5927             case R_MIPS_GOT_LO16:
5928             case R_MIPS_GOT_PAGE:
5929             case R_MIPS_GOT_OFST:
5930             case R_MIPS_GOT_DISP:
5931               if (dynobj == NULL)
5932                 elf_hash_table (info)->dynobj = dynobj = abfd;
5933               if (! mips_elf64_create_got_section (dynobj, info))
5934                 return false;
5935               g = _mips_elf64_got_info (dynobj, &sgot);
5936               break;
5937
5938             case R_MIPS_32:
5939             case R_MIPS_REL32:
5940             case R_MIPS_64:
5941               if (dynobj == NULL
5942                   && (info->shared || h != NULL)
5943                   && (sec->flags & SEC_ALLOC) != 0)
5944                 elf_hash_table (info)->dynobj = dynobj = abfd;
5945               break;
5946
5947             default:
5948               break;
5949             }
5950         }
5951
5952       if (!h && (r_type == R_MIPS_CALL_LO16
5953                  || r_type == R_MIPS_GOT_LO16
5954                  || r_type == R_MIPS_GOT_DISP))
5955         {
5956           /* We may need a local GOT entry for this relocation.  We
5957              don't count R_MIPS_GOT_PAGE because we can estimate the
5958              maximum number of pages needed by looking at the size of
5959              the segment.  Similar comments apply to R_MIPS_GOT16 and
5960              R_MIPS_CALL16.  We don't count R_MIPS_GOT_HI16, or
5961              R_MIPS_CALL_HI16 because these are always followed by an
5962              R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
5963
5964              This estimation is very conservative since we can merge
5965              duplicate entries in the GOT.  In order to be less
5966              conservative, we could actually build the GOT here,
5967              rather than in relocate_section.  */
5968           g->local_gotno++;
5969           sgot->_raw_size += get_elf_backend_data (dynobj)->s->arch_size / 8;
5970         }
5971
5972       switch (r_type)
5973         {
5974         case R_MIPS_CALL16:
5975           if (h == NULL)
5976             {
5977               (*_bfd_error_handler)
5978                 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
5979                  bfd_archive_filename (abfd), (unsigned long) rel->r_offset);
5980               bfd_set_error (bfd_error_bad_value);
5981               return false;
5982             }
5983           /* Fall through.  */
5984
5985         case R_MIPS_CALL_HI16:
5986         case R_MIPS_CALL_LO16:
5987           if (h != NULL)
5988             {
5989               /* This symbol requires a global offset table entry.  */
5990               if (!mips_elf64_record_global_got_symbol (h, info, g))
5991                 return false;
5992
5993               /* We need a stub, not a plt entry for the undefined
5994                  function.  But we record it as if it needs plt.  See
5995                  elf_adjust_dynamic_symbol in elflink.h.  */
5996               h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
5997               h->type = STT_FUNC;
5998             }
5999           break;
6000
6001         case R_MIPS_GOT16:
6002         case R_MIPS_GOT_HI16:
6003         case R_MIPS_GOT_LO16:
6004         case R_MIPS_GOT_DISP:
6005           /* This symbol requires a global offset table entry.  */
6006           if (h && !mips_elf64_record_global_got_symbol (h, info, g))
6007             return false;
6008           break;
6009
6010         case R_MIPS_32:
6011         case R_MIPS_REL32:
6012         case R_MIPS_64:
6013           if ((info->shared || h != NULL)
6014               && (sec->flags & SEC_ALLOC) != 0)
6015             {
6016               if (sreloc == NULL)
6017                 {
6018                   const char *name = ".rel.dyn";
6019
6020                   sreloc = bfd_get_section_by_name (dynobj, name);
6021                   if (sreloc == NULL)
6022                     {
6023                       sreloc = bfd_make_section (dynobj, name);
6024                       if (sreloc == NULL
6025                           || ! bfd_set_section_flags (dynobj, sreloc,
6026                                                       (SEC_ALLOC
6027                                                        | SEC_LOAD
6028                                                        | SEC_HAS_CONTENTS
6029                                                        | SEC_IN_MEMORY
6030                                                        | SEC_LINKER_CREATED
6031                                                        | SEC_READONLY))
6032                           || ! bfd_set_section_alignment (dynobj, sreloc,
6033                                                           4))
6034                         return false;
6035                     }
6036                 }
6037 #define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
6038               if (info->shared)
6039                 {
6040                   /* When creating a shared object, we must copy these
6041                      reloc types into the output file as R_MIPS_REL32
6042                      relocs.  We make room for this reloc in the
6043                      .rel.dyn reloc section.  */
6044                   mips_elf64_allocate_dynamic_relocations (dynobj, 1);
6045                   if ((sec->flags & MIPS_READONLY_SECTION)
6046                       == MIPS_READONLY_SECTION)
6047                     /* We tell the dynamic linker that there are
6048                        relocations against the text segment.  */
6049                     info->flags |= DF_TEXTREL;
6050                 }
6051               else
6052                 {
6053                   struct mips_elf64_link_hash_entry *hmips;
6054
6055                   /* We only need to copy this reloc if the symbol is
6056                      defined in a dynamic object.  */
6057                   hmips = (struct mips_elf64_link_hash_entry *) h;
6058                   ++hmips->possibly_dynamic_relocs;
6059                   if ((sec->flags & MIPS_READONLY_SECTION)
6060                       == MIPS_READONLY_SECTION)
6061                     /* We need it to tell the dynamic linker if there
6062                        are relocations against the text segment.  */
6063                     hmips->readonly_reloc = true;
6064                 }
6065              
6066               /* Even though we don't directly need a GOT entry for
6067                  this symbol, a symbol must have a dynamic symbol
6068                  table index greater that DT_MIPS_GOTSYM if there are
6069                  dynamic relocations against it.  */
6070               if (h != NULL
6071                   && !mips_elf64_record_global_got_symbol (h, info, g))
6072                 return false;
6073             }
6074           break;
6075
6076         case R_MIPS_26:
6077         case R_MIPS_GPREL16:
6078         case R_MIPS_LITERAL:
6079         case R_MIPS_GPREL32:
6080           break;
6081
6082           /* This relocation describes the C++ object vtable hierarchy.
6083              Reconstruct it for later use during GC.  */
6084         case R_MIPS_GNU_VTINHERIT:
6085           if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6086             return false;
6087           break;
6088
6089           /* This relocation describes which C++ vtable entries are actually
6090              used.  Record for later use during GC.  */
6091         case R_MIPS_GNU_VTENTRY:
6092           if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6093             return false;
6094           break;
6095
6096         default:
6097           break;
6098         }
6099     }
6100
6101   return true;
6102 }
6103
6104 /* Structure used to pass information to mips_elf64_output_extsym.  */
6105
6106 struct extsym_info
6107 {
6108   bfd *abfd;
6109   struct bfd_link_info *info;
6110   struct ecoff_debug_info *debug;
6111   const struct ecoff_debug_swap *swap;
6112   boolean failed;
6113 };
6114
6115 /* This routine is used to write out ECOFF debugging external symbol
6116    information.  It is called via mips_elf64_link_hash_traverse.  The
6117    ECOFF external symbol information must match the ELF external
6118    symbol information.  Unfortunately, at this point we don't know
6119    whether a symbol is required by reloc information, so the two
6120    tables may wind up being different.  We must sort out the external
6121    symbol information before we can set the final size of the .mdebug
6122    section, and we must set the size of the .mdebug section before we
6123    can relocate any sections, and we can't know which symbols are
6124    required by relocation until we relocate the sections.
6125    Fortunately, it is relatively unlikely that any symbol will be
6126    stripped but required by a reloc.  In particular, it can not happen
6127    when generating a final executable.  */
6128
6129 static boolean
6130 mips_elf64_output_extsym (h, data)
6131      struct mips_elf64_link_hash_entry *h;
6132      PTR data;
6133 {
6134   struct extsym_info *einfo = (struct extsym_info *) data;
6135   boolean strip;
6136   asection *sec, *output_section;
6137
6138   if (h->root.root.type == bfd_link_hash_warning)
6139     h = (struct mips_elf64_link_hash_entry *) h->root.root.u.i.link;
6140
6141   if (h->root.indx == -2)
6142     strip = false;
6143   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6144             || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
6145            && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
6146            && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
6147     strip = true;
6148   else if (einfo->info->strip == strip_all
6149            || (einfo->info->strip == strip_some
6150                && bfd_hash_lookup (einfo->info->keep_hash,
6151                                    h->root.root.root.string,
6152                                    false, false) == NULL))
6153     strip = true;
6154   else
6155     strip = false;
6156
6157   if (strip)
6158     return true;
6159
6160   if (h->esym.ifd == -2)
6161     {
6162       h->esym.jmptbl = 0;
6163       h->esym.cobol_main = 0;
6164       h->esym.weakext = 0;
6165       h->esym.reserved = 0;
6166       h->esym.ifd = ifdNil;
6167       h->esym.asym.value = 0;
6168       h->esym.asym.st = stGlobal;
6169
6170       if (h->root.root.type == bfd_link_hash_undefined
6171               || h->root.root.type == bfd_link_hash_undefweak)
6172         {
6173           const char *name;
6174
6175           /* Use undefined class.  Also, set class and type for some
6176              special symbols.  */
6177           name = h->root.root.root.string;
6178           h->esym.asym.sc = scUndefined;
6179         }
6180       else if (h->root.root.type != bfd_link_hash_defined
6181           && h->root.root.type != bfd_link_hash_defweak)
6182         h->esym.asym.sc = scAbs;
6183       else
6184         {
6185           const char *name;
6186
6187           sec = h->root.root.u.def.section;
6188           output_section = sec->output_section;
6189
6190           /* When making a shared library and symbol h is the one from
6191              the another shared library, OUTPUT_SECTION may be null.  */
6192           if (output_section == NULL)
6193             h->esym.asym.sc = scUndefined;
6194           else
6195             {
6196               name = bfd_section_name (output_section->owner, output_section);
6197
6198               if (strcmp (name, ".text") == 0)
6199                 h->esym.asym.sc = scText;
6200               else if (strcmp (name, ".data") == 0)
6201                 h->esym.asym.sc = scData;
6202               else if (strcmp (name, ".sdata") == 0)
6203                 h->esym.asym.sc = scSData;
6204               else if (strcmp (name, ".rodata") == 0
6205                        || strcmp (name, ".rdata") == 0)
6206                 h->esym.asym.sc = scRData;
6207               else if (strcmp (name, ".bss") == 0)
6208                 h->esym.asym.sc = scBss;
6209               else if (strcmp (name, ".sbss") == 0)
6210                 h->esym.asym.sc = scSBss;
6211               else if (strcmp (name, ".init") == 0)
6212                 h->esym.asym.sc = scInit;
6213               else if (strcmp (name, ".fini") == 0)
6214                 h->esym.asym.sc = scFini;
6215               else
6216                 h->esym.asym.sc = scAbs;
6217             }
6218         }
6219
6220       h->esym.asym.reserved = 0;
6221       h->esym.asym.index = indexNil;
6222     }
6223
6224   if (h->root.root.type == bfd_link_hash_common)
6225     h->esym.asym.value = h->root.root.u.c.size;
6226   else if (h->root.root.type == bfd_link_hash_defined
6227            || h->root.root.type == bfd_link_hash_defweak)
6228     {
6229       if (h->esym.asym.sc == scCommon)
6230         h->esym.asym.sc = scBss;
6231       else if (h->esym.asym.sc == scSCommon)
6232         h->esym.asym.sc = scSBss;
6233
6234       sec = h->root.root.u.def.section;
6235       output_section = sec->output_section;
6236       if (output_section != NULL)
6237         h->esym.asym.value = (h->root.root.u.def.value
6238                               + sec->output_offset
6239                               + output_section->vma);
6240       else
6241         h->esym.asym.value = 0;
6242     }
6243   else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6244     {
6245       struct mips_elf64_link_hash_entry *hd = h;
6246       boolean no_fn_stub = h->no_fn_stub;
6247
6248       while (hd->root.root.type == bfd_link_hash_indirect)
6249         {
6250           hd = (struct mips_elf64_link_hash_entry *)h->root.root.u.i.link;
6251           no_fn_stub = no_fn_stub || hd->no_fn_stub;
6252         }
6253
6254       if (!no_fn_stub)
6255         {
6256           /* Set type and value for a symbol with a function stub.  */
6257           h->esym.asym.st = stProc;
6258           sec = hd->root.root.u.def.section;
6259           if (sec == NULL)
6260             h->esym.asym.value = 0;
6261           else
6262             {
6263               output_section = sec->output_section;
6264               if (output_section != NULL)
6265                 h->esym.asym.value = (hd->root.plt.offset
6266                                       + sec->output_offset
6267                                       + output_section->vma);
6268               else
6269                 h->esym.asym.value = 0;
6270             }
6271 #if 0 /* FIXME?  */
6272           h->esym.ifd = 0;
6273 #endif
6274         }
6275     }
6276
6277   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
6278                                       h->root.root.root.string,
6279                                       &h->esym))
6280     {
6281       einfo->failed = true;
6282       return false;
6283     }
6284
6285   return true;
6286 }
6287
6288 /* Swap an entry in a .gptab section.  Note that these routines rely
6289    on the equivalence of the two elements of the union.  */
6290
6291 static void
6292 mips_elf64_swap_gptab_in (abfd, ex, in)
6293      bfd *abfd;
6294      const Elf32_External_gptab *ex;
6295      Elf32_gptab *in;
6296 {
6297   in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
6298   in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
6299 }
6300
6301 static void
6302 mips_elf64_swap_gptab_out (abfd, in, ex)
6303      bfd *abfd;
6304      const Elf32_gptab *in;
6305      Elf32_External_gptab *ex;
6306 {
6307   H_PUT_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
6308                 ex->gt_entry.gt_g_value);
6309   H_PUT_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
6310                 ex->gt_entry.gt_bytes);
6311 }
6312
6313 /* A comparison routine used to sort .gptab entries.  */
6314
6315 static int
6316 gptab_compare (p1, p2)
6317      const PTR p1;
6318      const PTR p2;
6319 {
6320   const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
6321   const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
6322
6323   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
6324 }
6325
6326 /* We need to use a special link routine to handle the .mdebug section.
6327    We need to merge all instances of this section together, not write
6328    them all out sequentially.  */
6329
6330 boolean
6331 mips_elf64_final_link (abfd, info)
6332      bfd *abfd;
6333      struct bfd_link_info *info;
6334 {
6335   asection **secpp;
6336   asection *o;
6337   struct bfd_link_order *p;
6338   asection *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
6339   struct ecoff_debug_info debug;
6340   const struct ecoff_debug_swap *swap
6341     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
6342   HDRR *symhdr = &debug.symbolic_header;
6343   PTR mdebug_handle = NULL;
6344   asection *s;
6345   EXTR esym;
6346   unsigned int i;
6347   static const char * const secname[] =
6348       { ".text", ".init", ".fini", ".data",
6349           ".rodata", ".sdata", ".sbss", ".bss" };
6350   static const int sc[] = { scText, scInit, scFini, scData,
6351                           scRData, scSData, scSBss, scBss };
6352
6353   /* If all the things we linked together were PIC, but we're
6354      producing an executable (rather than a shared object), then the
6355      resulting file is CPIC (i.e., it calls PIC code.)  */
6356   if (!info->shared
6357       && !info->relocateable
6358       && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
6359     {
6360       elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
6361       elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
6362     }
6363
6364   /* We'd carefully arranged the dynamic symbol indices, and then the
6365      generic size_dynamic_sections renumbered them out from under us.
6366      Rather than trying somehow to prevent the renumbering, just do
6367      the sort again.  */
6368   if (elf_hash_table (info)->dynamic_sections_created)
6369     {
6370       bfd *dynobj;
6371       asection *got;
6372       struct mips_elf64_got_info *g;
6373
6374       /* When we resort, we must tell mips_elf64_sort_hash_table what
6375          the lowest index it may use is.  That's the number of section
6376          symbols we're going to add.  The generic ELF linker only
6377          adds these symbols when building a shared object.  Note that
6378          we count the sections after (possibly) removing the .options
6379          section above.  */
6380       if (!mips_elf64_sort_hash_table (info, (info->shared 
6381                                             ? bfd_count_sections (abfd) + 1
6382                                             : 1)))
6383         return false;
6384
6385       /* Make sure we didn't grow the global .got region.  */
6386       dynobj = elf_hash_table (info)->dynobj;
6387       got = bfd_get_section_by_name (dynobj, ".got");
6388       g = (struct mips_elf64_got_info *) elf_section_data (got)->tdata;
6389
6390       if (g->global_gotsym != NULL)
6391         BFD_ASSERT ((elf_hash_table (info)->dynsymcount
6392                      - g->global_gotsym->dynindx)
6393                     <= g->global_gotno);
6394     }
6395
6396   /* We include .MIPS.options, even though we don't process it quite right.
6397      (Some entries are supposed to be merged.)  At IRIX6 empirically we seem
6398      to be better off including it than not.  */
6399   for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
6400     {
6401       if (strcmp ((*secpp)->name, ".MIPS.options") == 0)
6402         {
6403           for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
6404             if (p->type == bfd_indirect_link_order)
6405               p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
6406           (*secpp)->link_order_head = NULL;
6407           bfd_section_list_remove (abfd, secpp);
6408           --abfd->section_count;
6409             
6410           break;
6411         }
6412     }
6413
6414   /* Get a value for the GP register.  */
6415   if (elf_gp (abfd) == 0)
6416     {
6417       struct bfd_link_hash_entry *h;
6418
6419       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
6420       if (h != (struct bfd_link_hash_entry *) NULL
6421           && h->type == bfd_link_hash_defined)
6422         elf_gp (abfd) = (h->u.def.value
6423                          + h->u.def.section->output_section->vma
6424                          + h->u.def.section->output_offset);
6425       else if (info->relocateable)
6426         {
6427           bfd_vma lo = MINUS_ONE;
6428
6429           /* Find the GP-relative section with the lowest offset.  */
6430           for (o = abfd->sections; o != NULL; o = o->next)
6431             if (o->vma < lo 
6432                 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
6433               lo = o->vma;
6434
6435           /* And calculate GP relative to that.  */
6436           elf_gp (abfd) = (lo + 0x7ff0);
6437         }
6438       else
6439         {
6440           /* If the relocate_section function needs to do a reloc
6441              involving the GP value, it should make a reloc_dangerous
6442              callback to warn that GP is not defined.  */
6443         }
6444     }
6445
6446   /* Go through the sections and collect the .mdebug information.  */
6447   mdebug_sec = NULL;
6448   gptab_data_sec = NULL;
6449   gptab_bss_sec = NULL;
6450   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
6451     {
6452       if (strcmp (o->name, ".mdebug") == 0)
6453         {
6454           struct extsym_info einfo;
6455           bfd_vma last;
6456
6457           /* We have found the .mdebug section in the output file.
6458              Look through all the link_orders comprising it and merge
6459              the information together.  */
6460           symhdr->magic = swap->sym_magic;
6461           /* FIXME: What should the version stamp be?  */
6462           symhdr->vstamp = 0;
6463           symhdr->ilineMax = 0;
6464           symhdr->cbLine = 0;
6465           symhdr->idnMax = 0;
6466           symhdr->ipdMax = 0;
6467           symhdr->isymMax = 0;
6468           symhdr->ioptMax = 0;
6469           symhdr->iauxMax = 0;
6470           symhdr->issMax = 0;
6471           symhdr->issExtMax = 0;
6472           symhdr->ifdMax = 0;
6473           symhdr->crfd = 0;
6474           symhdr->iextMax = 0;
6475
6476           /* We accumulate the debugging information itself in the
6477              debug_info structure.  */
6478           debug.line = NULL;
6479           debug.external_dnr = NULL;
6480           debug.external_pdr = NULL;
6481           debug.external_sym = NULL;
6482           debug.external_opt = NULL;
6483           debug.external_aux = NULL;
6484           debug.ss = NULL;
6485           debug.ssext = debug.ssext_end = NULL;
6486           debug.external_fdr = NULL;
6487           debug.external_rfd = NULL;
6488           debug.external_ext = debug.external_ext_end = NULL;
6489
6490           mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
6491           if (mdebug_handle == (PTR) NULL)
6492             return false;
6493
6494           esym.jmptbl = 0;
6495           esym.cobol_main = 0;
6496           esym.weakext = 0;
6497           esym.reserved = 0;
6498           esym.ifd = ifdNil;
6499           esym.asym.iss = issNil;
6500           esym.asym.st = stLocal;
6501           esym.asym.reserved = 0;
6502           esym.asym.index = indexNil;
6503           last = 0;
6504           for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
6505             {
6506               esym.asym.sc = sc[i];
6507               s = bfd_get_section_by_name (abfd, secname[i]);
6508               if (s != NULL)
6509                 {
6510                   esym.asym.value = s->vma;
6511                   last = s->vma + s->_raw_size;
6512                 }
6513               else
6514                 esym.asym.value = last;
6515               if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
6516                                                  secname[i], &esym))
6517                 return false;
6518             }
6519
6520           for (p = o->link_order_head;
6521                p != (struct bfd_link_order *) NULL;
6522                p = p->next)
6523             {
6524               asection *input_section;
6525               bfd *input_bfd;
6526               const struct ecoff_debug_swap *input_swap;
6527               struct ecoff_debug_info input_debug;
6528               char *eraw_src;
6529               char *eraw_end;
6530
6531               if (p->type != bfd_indirect_link_order)
6532                 {
6533                   if (p->type == bfd_fill_link_order)
6534                     continue;
6535                   abort ();
6536                 }
6537
6538               input_section = p->u.indirect.section;
6539               input_bfd = input_section->owner;
6540
6541               if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
6542                   || (get_elf_backend_data (input_bfd)
6543                       ->elf_backend_ecoff_debug_swap) == NULL)
6544                 {
6545                   /* I don't know what a non MIPS ELF bfd would be
6546                      doing with a .mdebug section, but I don't really
6547                      want to deal with it.  */
6548                   continue;
6549                 }
6550
6551               input_swap = (get_elf_backend_data (input_bfd)
6552                             ->elf_backend_ecoff_debug_swap);
6553
6554               BFD_ASSERT (p->size == input_section->_raw_size);
6555
6556               /* The ECOFF linking code expects that we have already
6557                  read in the debugging information and set up an
6558                  ecoff_debug_info structure, so we do that now.  */
6559               if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
6560                                                    &input_debug))
6561                 return false;
6562
6563               if (! (bfd_ecoff_debug_accumulate
6564                      (mdebug_handle, abfd, &debug, swap, input_bfd,
6565                       &input_debug, input_swap, info)))
6566                 return false;
6567
6568               /* Loop through the external symbols.  For each one with
6569                  interesting information, try to find the symbol in
6570                  the linker global hash table and save the information
6571                  for the output external symbols.  */
6572               eraw_src = input_debug.external_ext;
6573               eraw_end = (eraw_src
6574                           + (input_debug.symbolic_header.iextMax
6575                              * input_swap->external_ext_size));
6576               for (;
6577                    eraw_src < eraw_end;
6578                    eraw_src += input_swap->external_ext_size)
6579                 {
6580                   EXTR ext;
6581                   const char *name;
6582                   struct mips_elf64_link_hash_entry *h;
6583
6584                   (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
6585                   if (ext.asym.sc == scNil
6586                       || ext.asym.sc == scUndefined
6587                       || ext.asym.sc == scSUndefined)
6588                     continue;
6589
6590                   name = input_debug.ssext + ext.asym.iss;
6591                   h = mips_elf64_link_hash_lookup (mips_elf64_hash_table (info),
6592                                                  name, false, false, true);
6593                   if (h == NULL || h->esym.ifd != -2)
6594                     continue;
6595
6596                   if (ext.ifd != -1)
6597                     {
6598                       BFD_ASSERT (ext.ifd
6599                                   < input_debug.symbolic_header.ifdMax);
6600                       ext.ifd = input_debug.ifdmap[ext.ifd];
6601                     }
6602
6603                   h->esym = ext;
6604                 }
6605
6606               /* Free up the information we just read.  */
6607               free (input_debug.line);
6608               free (input_debug.external_dnr);
6609               free (input_debug.external_pdr);
6610               free (input_debug.external_sym);
6611               free (input_debug.external_opt);
6612               free (input_debug.external_aux);
6613               free (input_debug.ss);
6614               free (input_debug.ssext);
6615               free (input_debug.external_fdr);
6616               free (input_debug.external_rfd);
6617               free (input_debug.external_ext);
6618
6619               /* Hack: reset the SEC_HAS_CONTENTS flag so that
6620                  elf_link_input_bfd ignores this section.  */
6621               input_section->flags &=~ SEC_HAS_CONTENTS;
6622             }
6623
6624           /* Build the external symbol information.  */
6625           einfo.abfd = abfd;
6626           einfo.info = info;
6627           einfo.debug = &debug;
6628           einfo.swap = swap;
6629           einfo.failed = false;
6630           mips_elf64_link_hash_traverse (mips_elf64_hash_table (info),
6631                                          mips_elf64_output_extsym,
6632                                          (PTR) &einfo);
6633           if (einfo.failed)
6634             return false;
6635
6636           /* Set the size of the .mdebug section.  */
6637           o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
6638
6639           /* Skip this section later on (I don't think this currently
6640              matters, but someday it might).  */
6641           o->link_order_head = (struct bfd_link_order *) NULL;
6642
6643           mdebug_sec = o;
6644         }
6645
6646       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
6647         {
6648           const char *subname;
6649           unsigned int c;
6650           Elf32_gptab *tab;
6651           Elf32_External_gptab *ext_tab;
6652           unsigned int i;
6653
6654           /* The .gptab.sdata and .gptab.sbss sections hold
6655              information describing how the small data area would
6656              change depending upon the -G switch.  These sections
6657              not used in executables files.  */
6658           if (! info->relocateable)
6659             {
6660               asection **secpp;
6661
6662               for (p = o->link_order_head;
6663                    p != (struct bfd_link_order *) NULL;
6664                    p = p->next)
6665                 {
6666                   asection *input_section;
6667
6668                   if (p->type != bfd_indirect_link_order)
6669                     {
6670                       if (p->type == bfd_fill_link_order)
6671                         continue;
6672                       abort ();
6673                     }
6674
6675                   input_section = p->u.indirect.section;
6676
6677                   /* Hack: reset the SEC_HAS_CONTENTS flag so that
6678                      elf_link_input_bfd ignores this section.  */
6679                   input_section->flags &=~ SEC_HAS_CONTENTS;
6680                 }
6681
6682               /* Skip this section later on (I don't think this
6683                  currently matters, but someday it might).  */
6684               o->link_order_head = (struct bfd_link_order *) NULL;
6685
6686               /* Really remove the section.  */
6687               for (secpp = &abfd->sections;
6688                    *secpp != o;
6689                    secpp = &(*secpp)->next)
6690                 ;
6691               bfd_section_list_remove (abfd, secpp);
6692               --abfd->section_count;
6693
6694               continue;
6695             }
6696
6697           /* There is one gptab for initialized data, and one for
6698              uninitialized data.  */
6699           if (strcmp (o->name, ".gptab.sdata") == 0)
6700             gptab_data_sec = o;
6701           else if (strcmp (o->name, ".gptab.sbss") == 0)
6702             gptab_bss_sec = o;
6703           else
6704             {
6705               (*_bfd_error_handler)
6706                 (_("%s: illegal section name `%s'"),
6707                  bfd_archive_filename (abfd), o->name);
6708               bfd_set_error (bfd_error_nonrepresentable_section);
6709               return false;
6710             }
6711
6712           /* The linker script always combines .gptab.data and
6713              .gptab.sdata into .gptab.sdata, and likewise for
6714              .gptab.bss and .gptab.sbss.  It is possible that there is
6715              no .sdata or .sbss section in the output file, in which
6716              case we must change the name of the output section.  */
6717           subname = o->name + sizeof ".gptab" - 1;
6718           if (bfd_get_section_by_name (abfd, subname) == NULL)
6719             {
6720               if (o == gptab_data_sec)
6721                 o->name = ".gptab.data";
6722               else
6723                 o->name = ".gptab.bss";
6724               subname = o->name + sizeof ".gptab" - 1;
6725               BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
6726             }
6727
6728           /* Set up the first entry.  */
6729           c = 1;
6730           tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
6731           if (tab == NULL)
6732             return false;
6733           tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
6734           tab[0].gt_header.gt_unused = 0;
6735
6736           /* Combine the input sections.  */
6737           for (p = o->link_order_head;
6738                p != (struct bfd_link_order *) NULL;
6739                p = p->next)
6740             {
6741               asection *input_section;
6742               bfd *input_bfd;
6743               bfd_size_type size;
6744               unsigned long last;
6745               bfd_size_type gpentry;
6746
6747               if (p->type != bfd_indirect_link_order)
6748                 {
6749                   if (p->type == bfd_fill_link_order)
6750                     continue;
6751                   abort ();
6752                 }
6753
6754               input_section = p->u.indirect.section;
6755               input_bfd = input_section->owner;
6756
6757               /* Combine the gptab entries for this input section one
6758                  by one.  We know that the input gptab entries are
6759                  sorted by ascending -G value.  */
6760               size = bfd_section_size (input_bfd, input_section);
6761               last = 0;
6762               for (gpentry = sizeof (Elf32_External_gptab);
6763                    gpentry < size;
6764                    gpentry += sizeof (Elf32_External_gptab))
6765                 {
6766                   Elf32_External_gptab ext_gptab;
6767                   Elf32_gptab int_gptab;
6768                   unsigned long val;
6769                   unsigned long add;
6770                   boolean exact;
6771                   unsigned int look;
6772
6773                   if (! (bfd_get_section_contents
6774                          (input_bfd, input_section, (PTR) &ext_gptab,
6775                           gpentry, sizeof (Elf32_External_gptab))))
6776                     {
6777                       free (tab);
6778                       return false;
6779                     }
6780
6781                   mips_elf64_swap_gptab_in (input_bfd, &ext_gptab,
6782                                                 &int_gptab);
6783                   val = int_gptab.gt_entry.gt_g_value;
6784                   add = int_gptab.gt_entry.gt_bytes - last;
6785
6786                   exact = false;
6787                   for (look = 1; look < c; look++)
6788                     {
6789                       if (tab[look].gt_entry.gt_g_value >= val)
6790                         tab[look].gt_entry.gt_bytes += add;
6791
6792                       if (tab[look].gt_entry.gt_g_value == val)
6793                         exact = true;
6794                     }
6795
6796                   if (! exact)
6797                     {
6798                       Elf32_gptab *new_tab;
6799                       unsigned int max;
6800
6801                       /* We need a new table entry.  */
6802                       new_tab = ((Elf32_gptab *)
6803                                  bfd_realloc ((PTR) tab,
6804                                               (c + 1) * sizeof (Elf32_gptab)));
6805                       if (new_tab == NULL)
6806                         {
6807                           free (tab);
6808                           return false;
6809                         }
6810                       tab = new_tab;
6811                       tab[c].gt_entry.gt_g_value = val;
6812                       tab[c].gt_entry.gt_bytes = add;
6813
6814                       /* Merge in the size for the next smallest -G
6815                          value, since that will be implied by this new
6816                          value.  */
6817                       max = 0;
6818                       for (look = 1; look < c; look++)
6819                         {
6820                           if (tab[look].gt_entry.gt_g_value < val
6821                               && (max == 0
6822                                   || (tab[look].gt_entry.gt_g_value
6823                                       > tab[max].gt_entry.gt_g_value)))
6824                             max = look;
6825                         }
6826                       if (max != 0)
6827                         tab[c].gt_entry.gt_bytes +=
6828                           tab[max].gt_entry.gt_bytes;
6829
6830                       ++c;
6831                     }
6832
6833                   last = int_gptab.gt_entry.gt_bytes;
6834                 }
6835
6836               /* Hack: reset the SEC_HAS_CONTENTS flag so that
6837                  elf_link_input_bfd ignores this section.  */
6838               input_section->flags &=~ SEC_HAS_CONTENTS;
6839             }
6840
6841           /* The table must be sorted by -G value.  */
6842           if (c > 2)
6843             qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
6844
6845           /* Swap out the table.  */
6846           ext_tab = ((Elf32_External_gptab *)
6847                      bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
6848           if (ext_tab == NULL)
6849             {
6850               free (tab);
6851               return false;
6852             }
6853
6854           for (i = 0; i < c; i++)
6855             mips_elf64_swap_gptab_out (abfd, tab + i, ext_tab + i);
6856           free (tab);
6857
6858           o->_raw_size = c * sizeof (Elf32_External_gptab);
6859           o->contents = (bfd_byte *) ext_tab;
6860
6861           /* Skip this section later on (I don't think this currently
6862              matters, but someday it might).  */
6863           o->link_order_head = (struct bfd_link_order *) NULL;
6864         }
6865     }
6866
6867   /* Invoke the regular ELF backend linker to do all the work.  */
6868   if (!bfd_elf64_bfd_final_link (abfd, info))
6869      return false;
6870
6871   /* Now write out the computed sections.  */
6872   if (mdebug_sec != (asection *) NULL)
6873     {
6874       BFD_ASSERT (abfd->output_has_begun);
6875       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
6876                                                swap, info,
6877                                                mdebug_sec->filepos))
6878         return false;
6879
6880       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
6881     }
6882   if (gptab_data_sec != (asection *) NULL)
6883     {
6884       if (! bfd_set_section_contents (abfd, gptab_data_sec,
6885                                       gptab_data_sec->contents,
6886                                       (file_ptr) 0,
6887                                       gptab_data_sec->_raw_size))
6888         return false;
6889     }
6890
6891   if (gptab_bss_sec != (asection *) NULL)
6892     {
6893       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
6894                                       gptab_bss_sec->contents,
6895                                       (file_ptr) 0,
6896                                       gptab_bss_sec->_raw_size))
6897         return false;
6898     }
6899
6900   return true;
6901 }
6902 \f
6903 /* ECOFF swapping routines.  These are used when dealing with the
6904    .mdebug section, which is in the ECOFF debugging format.  */
6905 static const struct ecoff_debug_swap mips_elf64_ecoff_debug_swap =
6906 {
6907   /* Symbol table magic number.  */
6908   magicSym2,
6909   /* Alignment of debugging information.  E.g., 4.  */
6910   8,
6911   /* Sizes of external symbolic information.  */
6912   sizeof (struct hdr_ext),
6913   sizeof (struct dnr_ext),
6914   sizeof (struct pdr_ext),
6915   sizeof (struct sym_ext),
6916   sizeof (struct opt_ext),
6917   sizeof (struct fdr_ext),
6918   sizeof (struct rfd_ext),
6919   sizeof (struct ext_ext),
6920   /* Functions to swap in external symbolic data.  */
6921   ecoff_swap_hdr_in,
6922   ecoff_swap_dnr_in,
6923   ecoff_swap_pdr_in,
6924   ecoff_swap_sym_in,
6925   ecoff_swap_opt_in,
6926   ecoff_swap_fdr_in,
6927   ecoff_swap_rfd_in,
6928   ecoff_swap_ext_in,
6929   _bfd_ecoff_swap_tir_in,
6930   _bfd_ecoff_swap_rndx_in,
6931   /* Functions to swap out external symbolic data.  */
6932   ecoff_swap_hdr_out,
6933   ecoff_swap_dnr_out,
6934   ecoff_swap_pdr_out,
6935   ecoff_swap_sym_out,
6936   ecoff_swap_opt_out,
6937   ecoff_swap_fdr_out,
6938   ecoff_swap_rfd_out,
6939   ecoff_swap_ext_out,
6940   _bfd_ecoff_swap_tir_out,
6941   _bfd_ecoff_swap_rndx_out,
6942   /* Function to read in symbolic data.  */
6943   _bfd_mips_elf_read_ecoff_info
6944 };
6945 \f
6946 /* Relocations in the 64 bit MIPS ELF ABI are more complex than in
6947    standard ELF.  This structure is used to redirect the relocation
6948    handling routines.  */
6949
6950 const struct elf_size_info mips_elf64_size_info =
6951 {
6952   sizeof (Elf64_External_Ehdr),
6953   sizeof (Elf64_External_Phdr),
6954   sizeof (Elf64_External_Shdr),
6955   sizeof (Elf64_Mips_External_Rel),
6956   sizeof (Elf64_Mips_External_Rela),
6957   sizeof (Elf64_External_Sym),
6958   sizeof (Elf64_External_Dyn),
6959   sizeof (Elf_External_Note),
6960   4,            /* hash-table entry size */
6961   3,            /* internal relocations per external relocations */
6962   64,           /* arch_size */
6963   8,            /* file_align */
6964   ELFCLASS64,
6965   EV_CURRENT,
6966   bfd_elf64_write_out_phdrs,
6967   bfd_elf64_write_shdrs_and_ehdr,
6968   mips_elf64_write_relocs,
6969   bfd_elf64_swap_symbol_out,
6970   mips_elf64_slurp_reloc_table,
6971   bfd_elf64_slurp_symbol_table,
6972   bfd_elf64_swap_dyn_in,
6973   bfd_elf64_swap_dyn_out,
6974   mips_elf64_be_swap_reloc_in,
6975   mips_elf64_be_swap_reloc_out,
6976   mips_elf64_be_swap_reloca_in,
6977   mips_elf64_be_swap_reloca_out
6978 };
6979
6980 #define ELF_ARCH                        bfd_arch_mips
6981 #define ELF_MACHINE_CODE                EM_MIPS
6982
6983 #define ELF_MAXPAGESIZE                 0x1000
6984
6985 #define elf_backend_collect             true
6986 #define elf_backend_type_change_ok      true
6987 #define elf_backend_can_gc_sections     true
6988 #define elf_info_to_howto               mips_elf64_info_to_howto_rela
6989 #define elf_info_to_howto_rel           mips_elf64_info_to_howto_rel
6990 #define elf_backend_object_p            _bfd_mips_elf_object_p
6991 #define elf_backend_symbol_processing   _bfd_mips_elf_symbol_processing
6992 #define elf_backend_section_processing  _bfd_mips_elf_section_processing
6993 #define elf_backend_section_from_shdr   _bfd_mips_elf_section_from_shdr
6994 #define elf_backend_fake_sections       _bfd_mips_elf_fake_sections
6995 #define elf_backend_section_from_bfd_section \
6996                                         _bfd_mips_elf_section_from_bfd_section
6997 #define elf_backend_add_symbol_hook     _bfd_mips_elf_add_symbol_hook
6998 #define elf_backend_link_output_symbol_hook \
6999                                         _bfd_mips_elf_link_output_symbol_hook
7000 #define elf_backend_create_dynamic_sections \
7001                                         mips_elf64_create_dynamic_sections
7002 #define elf_backend_check_relocs        mips_elf64_check_relocs
7003 #define elf_backend_adjust_dynamic_symbol \
7004                                         mips_elf64_adjust_dynamic_symbol
7005 #define elf_backend_always_size_sections \
7006                                         mips_elf64_always_size_sections
7007 #define elf_backend_size_dynamic_sections \
7008                                         mips_elf64_size_dynamic_sections
7009 #define elf_backend_relocate_section    mips_elf64_relocate_section
7010 #define elf_backend_finish_dynamic_symbol \
7011                                         mips_elf64_finish_dynamic_symbol
7012 #define elf_backend_finish_dynamic_sections \
7013                                         mips_elf64_finish_dynamic_sections
7014 #define elf_backend_final_write_processing \
7015                                         _bfd_mips_elf_final_write_processing
7016 #define elf_backend_additional_program_headers \
7017                                         mips_elf64_additional_program_headers
7018 #define elf_backend_modify_segment_map  _bfd_mips_elf_modify_segment_map
7019 #define elf_backend_gc_mark_hook        mips_elf64_gc_mark_hook
7020 #define elf_backend_gc_sweep_hook       mips_elf64_gc_sweep_hook
7021 #define elf_backend_ecoff_debug_swap    &mips_elf64_ecoff_debug_swap
7022 #define elf_backend_size_info           mips_elf64_size_info
7023
7024 #define elf_backend_got_header_size     (4 * MIPS_RESERVED_GOTNO)
7025 #define elf_backend_plt_header_size     0
7026
7027 /* MIPS ELF64 can use a mixture of REL and RELA, but some Relocations
7028  * work better/work only in RELA, so we default to this.  */
7029 #define elf_backend_may_use_rel_p       1
7030 #define elf_backend_may_use_rela_p      1
7031 #define elf_backend_default_use_rela_p  1
7032
7033 /* We don't set bfd_elf64_bfd_is_local_label_name because the 32-bit
7034    MIPS-specific function only applies to IRIX5, which had no 64-bit
7035    ABI.  */
7036 #define bfd_elf64_find_nearest_line     _bfd_mips_elf_find_nearest_line
7037 #define bfd_elf64_set_section_contents  _bfd_mips_elf_set_section_contents
7038 #define bfd_elf64_bfd_link_hash_table_create \
7039                                         mips_elf64_link_hash_table_create
7040 #define bfd_elf64_bfd_final_link        mips_elf64_final_link
7041 #define bfd_elf64_bfd_merge_private_bfd_data \
7042                                         _bfd_mips_elf_merge_private_bfd_data
7043 #define bfd_elf64_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7044 #define bfd_elf64_bfd_print_private_bfd_data \
7045                                         _bfd_mips_elf_print_private_bfd_data
7046
7047 #define bfd_elf64_get_reloc_upper_bound mips_elf64_get_reloc_upper_bound
7048 #define bfd_elf64_bfd_reloc_type_lookup mips_elf64_reloc_type_lookup
7049 #define bfd_elf64_archive_functions
7050 extern boolean bfd_elf64_archive_slurp_armap
7051   PARAMS((bfd *));
7052 extern boolean bfd_elf64_archive_write_armap
7053   PARAMS((bfd *, unsigned int, struct orl *, unsigned int, int));
7054 #define bfd_elf64_archive_slurp_extended_name_table \
7055                                 _bfd_archive_coff_slurp_extended_name_table
7056 #define bfd_elf64_archive_construct_extended_name_table \
7057                                 _bfd_archive_coff_construct_extended_name_table
7058 #define bfd_elf64_archive_truncate_arname \
7059                                         _bfd_archive_coff_truncate_arname
7060 #define bfd_elf64_archive_read_ar_hdr   _bfd_archive_coff_read_ar_hdr
7061 #define bfd_elf64_archive_openr_next_archived_file \
7062                                 _bfd_archive_coff_openr_next_archived_file
7063 #define bfd_elf64_archive_get_elt_at_index \
7064                                         _bfd_archive_coff_get_elt_at_index
7065 #define bfd_elf64_archive_generic_stat_arch_elt \
7066                                         _bfd_archive_coff_generic_stat_arch_elt
7067 #define bfd_elf64_archive_update_armap_timestamp \
7068                                 _bfd_archive_coff_update_armap_timestamp
7069
7070 /* The SGI style (n)64 NewABI.  */
7071 #define TARGET_LITTLE_SYM               bfd_elf64_littlemips_vec
7072 #define TARGET_LITTLE_NAME              "elf64-littlemips"
7073 #define TARGET_BIG_SYM                  bfd_elf64_bigmips_vec
7074 #define TARGET_BIG_NAME                 "elf64-bigmips"
7075
7076 #include "elf64-target.h"
7077
7078 #define INCLUDED_TARGET_FILE            /* More a type of flag.  */
7079
7080 /* The SYSV-style 'traditional' (n)64 NewABI.  */
7081 #undef TARGET_LITTLE_SYM
7082 #undef TARGET_LITTLE_NAME
7083 #undef TARGET_BIG_SYM
7084 #undef TARGET_BIG_NAME
7085
7086 #define TARGET_LITTLE_SYM               bfd_elf64_tradlittlemips_vec
7087 #define TARGET_LITTLE_NAME              "elf64-tradlittlemips"
7088 #define TARGET_BIG_SYM                  bfd_elf64_tradbigmips_vec
7089 #define TARGET_BIG_NAME                 "elf64-tradbigmips"
7090
7091 /* Include the target file again for this target.  */
7092 #include "elf64-target.h"