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