merge from gcc
[binutils.git] / bfd / elf32-mips.c
blobe83b2165fb3b314ee9389e890deb427953dba6e2
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005 Free Software Foundation, Inc.
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
12 This file is part of BFD, the Binary File Descriptor library.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
29 different MIPS ELF from other targets. This matters when linking.
30 This file supports both, switching at runtime. */
32 #include "bfd.h"
33 #include "sysdep.h"
34 #include "libbfd.h"
35 #include "bfdlink.h"
36 #include "genlink.h"
37 #include "elf-bfd.h"
38 #include "elfxx-mips.h"
39 #include "elf/mips.h"
41 /* Get the ECOFF swapping routines. */
42 #include "coff/sym.h"
43 #include "coff/symconst.h"
44 #include "coff/internal.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
47 #define ECOFF_SIGNED_32
48 #include "ecoffswap.h"
50 static bfd_reloc_status_type gprel32_with_gp
51 (bfd *, asymbol *, arelent *, asection *, bfd_boolean, void *, bfd_vma);
52 static bfd_reloc_status_type mips_elf_gprel32_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type mips32_64bit_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
57 (bfd *, bfd_reloc_code_real_type);
58 static reloc_howto_type *mips_elf32_rtype_to_howto
59 (unsigned int, bfd_boolean);
60 static void mips_info_to_howto_rel
61 (bfd *, arelent *, Elf_Internal_Rela *);
62 static void mips_info_to_howto_rela
63 (bfd *, arelent *, Elf_Internal_Rela *);
64 static bfd_boolean mips_elf_sym_is_global
65 (bfd *, asymbol *);
66 static bfd_boolean mips_elf32_object_p
67 (bfd *);
68 static bfd_boolean mips_elf_is_local_label_name
69 (bfd *, const char *);
70 static bfd_reloc_status_type mips16_jump_reloc
71 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
72 static bfd_reloc_status_type mips16_gprel_reloc
73 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
74 static bfd_reloc_status_type mips_elf_final_gp
75 (bfd *, asymbol *, bfd_boolean, char **, bfd_vma *);
76 static bfd_boolean mips_elf_assign_gp
77 (bfd *, bfd_vma *);
78 static bfd_boolean elf32_mips_grok_prstatus
79 (bfd *, Elf_Internal_Note *);
80 static bfd_boolean elf32_mips_grok_psinfo
81 (bfd *, Elf_Internal_Note *);
82 static irix_compat_t elf32_mips_irix_compat
83 (bfd *);
85 extern const bfd_target bfd_elf32_bigmips_vec;
86 extern const bfd_target bfd_elf32_littlemips_vec;
88 /* Nonzero if ABFD is using the N32 ABI. */
89 #define ABI_N32_P(abfd) \
90 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
92 /* Whether we are trying to be compatible with IRIX at all. */
93 #define SGI_COMPAT(abfd) \
94 (elf32_mips_irix_compat (abfd) != ict_none)
96 /* The number of local .got entries we reserve. */
97 #define MIPS_RESERVED_GOTNO (2)
99 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
100 from smaller values. Start with zero, widen, *then* decrement. */
101 #define MINUS_ONE (((bfd_vma)0) - 1)
103 /* The relocation table used for SHT_REL sections. */
105 static reloc_howto_type elf_mips_howto_table_rel[] =
107 /* No relocation. */
108 HOWTO (R_MIPS_NONE, /* type */
109 0, /* rightshift */
110 0, /* size (0 = byte, 1 = short, 2 = long) */
111 0, /* bitsize */
112 FALSE, /* pc_relative */
113 0, /* bitpos */
114 complain_overflow_dont, /* complain_on_overflow */
115 _bfd_mips_elf_generic_reloc, /* special_function */
116 "R_MIPS_NONE", /* name */
117 FALSE, /* partial_inplace */
118 0, /* src_mask */
119 0, /* dst_mask */
120 FALSE), /* pcrel_offset */
122 /* 16 bit relocation. */
123 HOWTO (R_MIPS_16, /* type */
124 0, /* rightshift */
125 2, /* size (0 = byte, 1 = short, 2 = long) */
126 16, /* bitsize */
127 FALSE, /* pc_relative */
128 0, /* bitpos */
129 complain_overflow_signed, /* complain_on_overflow */
130 _bfd_mips_elf_generic_reloc, /* special_function */
131 "R_MIPS_16", /* name */
132 TRUE, /* partial_inplace */
133 0x0000ffff, /* src_mask */
134 0x0000ffff, /* dst_mask */
135 FALSE), /* pcrel_offset */
137 /* 32 bit relocation. */
138 HOWTO (R_MIPS_32, /* type */
139 0, /* rightshift */
140 2, /* size (0 = byte, 1 = short, 2 = long) */
141 32, /* bitsize */
142 FALSE, /* pc_relative */
143 0, /* bitpos */
144 complain_overflow_dont, /* complain_on_overflow */
145 _bfd_mips_elf_generic_reloc, /* special_function */
146 "R_MIPS_32", /* name */
147 TRUE, /* partial_inplace */
148 0xffffffff, /* src_mask */
149 0xffffffff, /* dst_mask */
150 FALSE), /* pcrel_offset */
152 /* 32 bit symbol relative relocation. */
153 HOWTO (R_MIPS_REL32, /* type */
154 0, /* rightshift */
155 2, /* size (0 = byte, 1 = short, 2 = long) */
156 32, /* bitsize */
157 FALSE, /* pc_relative */
158 0, /* bitpos */
159 complain_overflow_dont, /* complain_on_overflow */
160 _bfd_mips_elf_generic_reloc, /* special_function */
161 "R_MIPS_REL32", /* name */
162 TRUE, /* partial_inplace */
163 0xffffffff, /* src_mask */
164 0xffffffff, /* dst_mask */
165 FALSE), /* pcrel_offset */
167 /* 26 bit jump address. */
168 HOWTO (R_MIPS_26, /* type */
169 2, /* rightshift */
170 2, /* size (0 = byte, 1 = short, 2 = long) */
171 26, /* bitsize */
172 FALSE, /* pc_relative */
173 0, /* bitpos */
174 complain_overflow_dont, /* complain_on_overflow */
175 /* This needs complex overflow
176 detection, because the upper four
177 bits must match the PC + 4. */
178 _bfd_mips_elf_generic_reloc, /* special_function */
179 "R_MIPS_26", /* name */
180 TRUE, /* partial_inplace */
181 0x03ffffff, /* src_mask */
182 0x03ffffff, /* dst_mask */
183 FALSE), /* pcrel_offset */
185 /* High 16 bits of symbol value. */
186 HOWTO (R_MIPS_HI16, /* type */
187 16, /* rightshift */
188 2, /* size (0 = byte, 1 = short, 2 = long) */
189 16, /* bitsize */
190 FALSE, /* pc_relative */
191 0, /* bitpos */
192 complain_overflow_dont, /* complain_on_overflow */
193 _bfd_mips_elf_hi16_reloc, /* special_function */
194 "R_MIPS_HI16", /* name */
195 TRUE, /* partial_inplace */
196 0x0000ffff, /* src_mask */
197 0x0000ffff, /* dst_mask */
198 FALSE), /* pcrel_offset */
200 /* Low 16 bits of symbol value. */
201 HOWTO (R_MIPS_LO16, /* type */
202 0, /* rightshift */
203 2, /* size (0 = byte, 1 = short, 2 = long) */
204 16, /* bitsize */
205 FALSE, /* pc_relative */
206 0, /* bitpos */
207 complain_overflow_dont, /* complain_on_overflow */
208 _bfd_mips_elf_lo16_reloc, /* special_function */
209 "R_MIPS_LO16", /* name */
210 TRUE, /* partial_inplace */
211 0x0000ffff, /* src_mask */
212 0x0000ffff, /* dst_mask */
213 FALSE), /* pcrel_offset */
215 /* GP relative reference. */
216 HOWTO (R_MIPS_GPREL16, /* type */
217 0, /* rightshift */
218 2, /* size (0 = byte, 1 = short, 2 = long) */
219 16, /* bitsize */
220 FALSE, /* pc_relative */
221 0, /* bitpos */
222 complain_overflow_signed, /* complain_on_overflow */
223 _bfd_mips_elf32_gprel16_reloc, /* special_function */
224 "R_MIPS_GPREL16", /* name */
225 TRUE, /* partial_inplace */
226 0x0000ffff, /* src_mask */
227 0x0000ffff, /* dst_mask */
228 FALSE), /* pcrel_offset */
230 /* Reference to literal section. */
231 HOWTO (R_MIPS_LITERAL, /* type */
232 0, /* rightshift */
233 2, /* size (0 = byte, 1 = short, 2 = long) */
234 16, /* bitsize */
235 FALSE, /* pc_relative */
236 0, /* bitpos */
237 complain_overflow_signed, /* complain_on_overflow */
238 _bfd_mips_elf32_gprel16_reloc, /* special_function */
239 "R_MIPS_LITERAL", /* name */
240 TRUE, /* partial_inplace */
241 0x0000ffff, /* src_mask */
242 0x0000ffff, /* dst_mask */
243 FALSE), /* pcrel_offset */
245 /* Reference to global offset table. */
246 HOWTO (R_MIPS_GOT16, /* type */
247 0, /* rightshift */
248 2, /* size (0 = byte, 1 = short, 2 = long) */
249 16, /* bitsize */
250 FALSE, /* pc_relative */
251 0, /* bitpos */
252 complain_overflow_signed, /* complain_on_overflow */
253 _bfd_mips_elf_got16_reloc, /* special_function */
254 "R_MIPS_GOT16", /* name */
255 TRUE, /* partial_inplace */
256 0x0000ffff, /* src_mask */
257 0x0000ffff, /* dst_mask */
258 FALSE), /* pcrel_offset */
260 /* 16 bit PC relative reference. */
261 HOWTO (R_MIPS_PC16, /* type */
262 0, /* rightshift */
263 2, /* size (0 = byte, 1 = short, 2 = long) */
264 16, /* bitsize */
265 TRUE, /* pc_relative */
266 0, /* bitpos */
267 complain_overflow_signed, /* complain_on_overflow */
268 _bfd_mips_elf_generic_reloc, /* special_function */
269 "R_MIPS_PC16", /* name */
270 TRUE, /* partial_inplace */
271 0x0000ffff, /* src_mask */
272 0x0000ffff, /* dst_mask */
273 TRUE), /* pcrel_offset */
275 /* 16 bit call through global offset table. */
276 HOWTO (R_MIPS_CALL16, /* type */
277 0, /* rightshift */
278 2, /* size (0 = byte, 1 = short, 2 = long) */
279 16, /* bitsize */
280 FALSE, /* pc_relative */
281 0, /* bitpos */
282 complain_overflow_signed, /* complain_on_overflow */
283 _bfd_mips_elf_generic_reloc, /* special_function */
284 "R_MIPS_CALL16", /* name */
285 TRUE, /* partial_inplace */
286 0x0000ffff, /* src_mask */
287 0x0000ffff, /* dst_mask */
288 FALSE), /* pcrel_offset */
290 /* 32 bit GP relative reference. */
291 HOWTO (R_MIPS_GPREL32, /* type */
292 0, /* rightshift */
293 2, /* size (0 = byte, 1 = short, 2 = long) */
294 32, /* bitsize */
295 FALSE, /* pc_relative */
296 0, /* bitpos */
297 complain_overflow_dont, /* complain_on_overflow */
298 mips_elf_gprel32_reloc, /* special_function */
299 "R_MIPS_GPREL32", /* name */
300 TRUE, /* partial_inplace */
301 0xffffffff, /* src_mask */
302 0xffffffff, /* dst_mask */
303 FALSE), /* pcrel_offset */
305 /* The remaining relocs are defined on Irix 5, although they are
306 not defined by the ABI. */
307 EMPTY_HOWTO (13),
308 EMPTY_HOWTO (14),
309 EMPTY_HOWTO (15),
311 /* A 5 bit shift field. */
312 HOWTO (R_MIPS_SHIFT5, /* type */
313 0, /* rightshift */
314 2, /* size (0 = byte, 1 = short, 2 = long) */
315 5, /* bitsize */
316 FALSE, /* pc_relative */
317 6, /* bitpos */
318 complain_overflow_bitfield, /* complain_on_overflow */
319 _bfd_mips_elf_generic_reloc, /* special_function */
320 "R_MIPS_SHIFT5", /* name */
321 TRUE, /* partial_inplace */
322 0x000007c0, /* src_mask */
323 0x000007c0, /* dst_mask */
324 FALSE), /* pcrel_offset */
326 /* A 6 bit shift field. */
327 /* FIXME: This is not handled correctly; a special function is
328 needed to put the most significant bit in the right place. */
329 HOWTO (R_MIPS_SHIFT6, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 6, /* bitsize */
333 FALSE, /* pc_relative */
334 6, /* bitpos */
335 complain_overflow_bitfield, /* complain_on_overflow */
336 _bfd_mips_elf_generic_reloc, /* special_function */
337 "R_MIPS_SHIFT6", /* name */
338 TRUE, /* partial_inplace */
339 0x000007c4, /* src_mask */
340 0x000007c4, /* dst_mask */
341 FALSE), /* pcrel_offset */
343 /* A 64 bit relocation. */
344 HOWTO (R_MIPS_64, /* type */
345 0, /* rightshift */
346 4, /* size (0 = byte, 1 = short, 2 = long) */
347 64, /* bitsize */
348 FALSE, /* pc_relative */
349 0, /* bitpos */
350 complain_overflow_dont, /* complain_on_overflow */
351 mips32_64bit_reloc, /* special_function */
352 "R_MIPS_64", /* name */
353 TRUE, /* partial_inplace */
354 MINUS_ONE, /* src_mask */
355 MINUS_ONE, /* dst_mask */
356 FALSE), /* pcrel_offset */
358 /* Displacement in the global offset table. */
359 HOWTO (R_MIPS_GOT_DISP, /* type */
360 0, /* rightshift */
361 2, /* size (0 = byte, 1 = short, 2 = long) */
362 16, /* bitsize */
363 FALSE, /* pc_relative */
364 0, /* bitpos */
365 complain_overflow_signed, /* complain_on_overflow */
366 _bfd_mips_elf_generic_reloc, /* special_function */
367 "R_MIPS_GOT_DISP", /* name */
368 TRUE, /* partial_inplace */
369 0x0000ffff, /* src_mask */
370 0x0000ffff, /* dst_mask */
371 FALSE), /* pcrel_offset */
373 /* Displacement to page pointer in the global offset table. */
374 HOWTO (R_MIPS_GOT_PAGE, /* type */
375 0, /* rightshift */
376 2, /* size (0 = byte, 1 = short, 2 = long) */
377 16, /* bitsize */
378 FALSE, /* pc_relative */
379 0, /* bitpos */
380 complain_overflow_signed, /* complain_on_overflow */
381 _bfd_mips_elf_generic_reloc, /* special_function */
382 "R_MIPS_GOT_PAGE", /* name */
383 TRUE, /* partial_inplace */
384 0x0000ffff, /* src_mask */
385 0x0000ffff, /* dst_mask */
386 FALSE), /* pcrel_offset */
388 /* Offset from page pointer in the global offset table. */
389 HOWTO (R_MIPS_GOT_OFST, /* type */
390 0, /* rightshift */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
392 16, /* bitsize */
393 FALSE, /* pc_relative */
394 0, /* bitpos */
395 complain_overflow_signed, /* complain_on_overflow */
396 _bfd_mips_elf_generic_reloc, /* special_function */
397 "R_MIPS_GOT_OFST", /* name */
398 TRUE, /* partial_inplace */
399 0x0000ffff, /* src_mask */
400 0x0000ffff, /* dst_mask */
401 FALSE), /* pcrel_offset */
403 /* High 16 bits of displacement in global offset table. */
404 HOWTO (R_MIPS_GOT_HI16, /* type */
405 0, /* rightshift */
406 2, /* size (0 = byte, 1 = short, 2 = long) */
407 16, /* bitsize */
408 FALSE, /* pc_relative */
409 0, /* bitpos */
410 complain_overflow_dont, /* complain_on_overflow */
411 _bfd_mips_elf_generic_reloc, /* special_function */
412 "R_MIPS_GOT_HI16", /* name */
413 TRUE, /* partial_inplace */
414 0x0000ffff, /* src_mask */
415 0x0000ffff, /* dst_mask */
416 FALSE), /* pcrel_offset */
418 /* Low 16 bits of displacement in global offset table. */
419 HOWTO (R_MIPS_GOT_LO16, /* 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_dont, /* complain_on_overflow */
426 _bfd_mips_elf_generic_reloc, /* special_function */
427 "R_MIPS_GOT_LO16", /* name */
428 TRUE, /* partial_inplace */
429 0x0000ffff, /* src_mask */
430 0x0000ffff, /* dst_mask */
431 FALSE), /* pcrel_offset */
433 /* 64 bit subtraction. Used in the N32 ABI. */
434 HOWTO (R_MIPS_SUB, /* type */
435 0, /* rightshift */
436 4, /* size (0 = byte, 1 = short, 2 = long) */
437 64, /* bitsize */
438 FALSE, /* pc_relative */
439 0, /* bitpos */
440 complain_overflow_dont, /* complain_on_overflow */
441 _bfd_mips_elf_generic_reloc, /* special_function */
442 "R_MIPS_SUB", /* name */
443 TRUE, /* partial_inplace */
444 MINUS_ONE, /* src_mask */
445 MINUS_ONE, /* dst_mask */
446 FALSE), /* pcrel_offset */
448 /* Used to cause the linker to insert and delete instructions? */
449 EMPTY_HOWTO (R_MIPS_INSERT_A),
450 EMPTY_HOWTO (R_MIPS_INSERT_B),
451 EMPTY_HOWTO (R_MIPS_DELETE),
453 /* Get the higher value of a 64 bit addend. */
454 HOWTO (R_MIPS_HIGHER, /* type */
455 0, /* rightshift */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
457 16, /* bitsize */
458 FALSE, /* pc_relative */
459 0, /* bitpos */
460 complain_overflow_dont, /* complain_on_overflow */
461 _bfd_mips_elf_generic_reloc, /* special_function */
462 "R_MIPS_HIGHER", /* name */
463 TRUE, /* partial_inplace */
464 0x0000ffff, /* src_mask */
465 0x0000ffff, /* dst_mask */
466 FALSE), /* pcrel_offset */
468 /* Get the highest value of a 64 bit addend. */
469 HOWTO (R_MIPS_HIGHEST, /* type */
470 0, /* rightshift */
471 2, /* size (0 = byte, 1 = short, 2 = long) */
472 16, /* bitsize */
473 FALSE, /* pc_relative */
474 0, /* bitpos */
475 complain_overflow_dont, /* complain_on_overflow */
476 _bfd_mips_elf_generic_reloc, /* special_function */
477 "R_MIPS_HIGHEST", /* name */
478 TRUE, /* partial_inplace */
479 0x0000ffff, /* src_mask */
480 0x0000ffff, /* dst_mask */
481 FALSE), /* pcrel_offset */
483 /* High 16 bits of displacement in global offset table. */
484 HOWTO (R_MIPS_CALL_HI16, /* type */
485 0, /* rightshift */
486 2, /* size (0 = byte, 1 = short, 2 = long) */
487 16, /* bitsize */
488 FALSE, /* pc_relative */
489 0, /* bitpos */
490 complain_overflow_dont, /* complain_on_overflow */
491 _bfd_mips_elf_generic_reloc, /* special_function */
492 "R_MIPS_CALL_HI16", /* name */
493 TRUE, /* partial_inplace */
494 0x0000ffff, /* src_mask */
495 0x0000ffff, /* dst_mask */
496 FALSE), /* pcrel_offset */
498 /* Low 16 bits of displacement in global offset table. */
499 HOWTO (R_MIPS_CALL_LO16, /* 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_dont, /* complain_on_overflow */
506 _bfd_mips_elf_generic_reloc, /* special_function */
507 "R_MIPS_CALL_LO16", /* name */
508 TRUE, /* partial_inplace */
509 0x0000ffff, /* src_mask */
510 0x0000ffff, /* dst_mask */
511 FALSE), /* pcrel_offset */
513 /* Section displacement. */
514 HOWTO (R_MIPS_SCN_DISP, /* type */
515 0, /* rightshift */
516 2, /* size (0 = byte, 1 = short, 2 = long) */
517 32, /* bitsize */
518 FALSE, /* pc_relative */
519 0, /* bitpos */
520 complain_overflow_dont, /* complain_on_overflow */
521 _bfd_mips_elf_generic_reloc, /* special_function */
522 "R_MIPS_SCN_DISP", /* name */
523 TRUE, /* partial_inplace */
524 0xffffffff, /* src_mask */
525 0xffffffff, /* dst_mask */
526 FALSE), /* pcrel_offset */
528 EMPTY_HOWTO (R_MIPS_REL16),
529 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
530 EMPTY_HOWTO (R_MIPS_PJUMP),
531 EMPTY_HOWTO (R_MIPS_RELGOT),
533 /* Protected jump conversion. This is an optimization hint. No
534 relocation is required for correctness. */
535 HOWTO (R_MIPS_JALR, /* type */
536 0, /* rightshift */
537 2, /* size (0 = byte, 1 = short, 2 = long) */
538 32, /* bitsize */
539 FALSE, /* pc_relative */
540 0, /* bitpos */
541 complain_overflow_dont, /* complain_on_overflow */
542 _bfd_mips_elf_generic_reloc, /* special_function */
543 "R_MIPS_JALR", /* name */
544 FALSE, /* partial_inplace */
545 0x00000000, /* src_mask */
546 0x00000000, /* dst_mask */
547 FALSE), /* pcrel_offset */
549 /* TLS GD/LD dynamic relocations. */
550 HOWTO (R_MIPS_TLS_DTPMOD32, /* type */
551 0, /* rightshift */
552 2, /* size (0 = byte, 1 = short, 2 = long) */
553 32, /* bitsize */
554 FALSE, /* pc_relative */
555 0, /* bitpos */
556 complain_overflow_dont, /* complain_on_overflow */
557 _bfd_mips_elf_generic_reloc, /* special_function */
558 "R_MIPS_TLS_DTPMOD32", /* name */
559 TRUE, /* partial_inplace */
560 0xffffffff, /* src_mask */
561 0xffffffff, /* dst_mask */
562 FALSE), /* pcrel_offset */
564 HOWTO (R_MIPS_TLS_DTPREL32, /* type */
565 0, /* rightshift */
566 2, /* size (0 = byte, 1 = short, 2 = long) */
567 32, /* bitsize */
568 FALSE, /* pc_relative */
569 0, /* bitpos */
570 complain_overflow_dont, /* complain_on_overflow */
571 _bfd_mips_elf_generic_reloc, /* special_function */
572 "R_MIPS_TLS_DTPREL32", /* name */
573 TRUE, /* partial_inplace */
574 0xffffffff, /* src_mask */
575 0xffffffff, /* dst_mask */
576 FALSE), /* pcrel_offset */
578 EMPTY_HOWTO (R_MIPS_TLS_DTPMOD64),
579 EMPTY_HOWTO (R_MIPS_TLS_DTPREL64),
581 /* TLS general dynamic variable reference. */
582 HOWTO (R_MIPS_TLS_GD, /* type */
583 0, /* rightshift */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
585 16, /* bitsize */
586 FALSE, /* pc_relative */
587 0, /* bitpos */
588 complain_overflow_signed, /* complain_on_overflow */
589 _bfd_mips_elf_generic_reloc, /* special_function */
590 "R_MIPS_TLS_GD", /* name */
591 TRUE, /* partial_inplace */
592 0x0000ffff, /* src_mask */
593 0x0000ffff, /* dst_mask */
594 FALSE), /* pcrel_offset */
596 /* TLS local dynamic variable reference. */
597 HOWTO (R_MIPS_TLS_LDM, /* type */
598 0, /* rightshift */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
600 16, /* bitsize */
601 FALSE, /* pc_relative */
602 0, /* bitpos */
603 complain_overflow_signed, /* complain_on_overflow */
604 _bfd_mips_elf_generic_reloc, /* special_function */
605 "R_MIPS_TLS_LDM", /* name */
606 TRUE, /* partial_inplace */
607 0x0000ffff, /* src_mask */
608 0x0000ffff, /* dst_mask */
609 FALSE), /* pcrel_offset */
611 /* TLS local dynamic offset. */
612 HOWTO (R_MIPS_TLS_DTPREL_HI16, /* type */
613 0, /* rightshift */
614 2, /* size (0 = byte, 1 = short, 2 = long) */
615 16, /* bitsize */
616 FALSE, /* pc_relative */
617 0, /* bitpos */
618 complain_overflow_signed, /* complain_on_overflow */
619 _bfd_mips_elf_generic_reloc, /* special_function */
620 "R_MIPS_TLS_DTPREL_HI16", /* name */
621 TRUE, /* partial_inplace */
622 0x0000ffff, /* src_mask */
623 0x0000ffff, /* dst_mask */
624 FALSE), /* pcrel_offset */
626 /* TLS local dynamic offset. */
627 HOWTO (R_MIPS_TLS_DTPREL_LO16, /* type */
628 0, /* rightshift */
629 2, /* size (0 = byte, 1 = short, 2 = long) */
630 16, /* bitsize */
631 FALSE, /* pc_relative */
632 0, /* bitpos */
633 complain_overflow_signed, /* complain_on_overflow */
634 _bfd_mips_elf_generic_reloc, /* special_function */
635 "R_MIPS_TLS_DTPREL_LO16", /* name */
636 TRUE, /* partial_inplace */
637 0x0000ffff, /* src_mask */
638 0x0000ffff, /* dst_mask */
639 FALSE), /* pcrel_offset */
641 /* TLS thread pointer offset. */
642 HOWTO (R_MIPS_TLS_GOTTPREL, /* type */
643 0, /* rightshift */
644 2, /* size (0 = byte, 1 = short, 2 = long) */
645 16, /* bitsize */
646 FALSE, /* pc_relative */
647 0, /* bitpos */
648 complain_overflow_signed, /* complain_on_overflow */
649 _bfd_mips_elf_generic_reloc, /* special_function */
650 "R_MIPS_TLS_GOTTPREL", /* name */
651 TRUE, /* partial_inplace */
652 0x0000ffff, /* src_mask */
653 0x0000ffff, /* dst_mask */
654 FALSE), /* pcrel_offset */
656 /* TLS IE dynamic relocations. */
657 HOWTO (R_MIPS_TLS_TPREL32, /* type */
658 0, /* rightshift */
659 2, /* size (0 = byte, 1 = short, 2 = long) */
660 32, /* bitsize */
661 FALSE, /* pc_relative */
662 0, /* bitpos */
663 complain_overflow_dont, /* complain_on_overflow */
664 _bfd_mips_elf_generic_reloc, /* special_function */
665 "R_MIPS_TLS_TPREL32", /* name */
666 TRUE, /* partial_inplace */
667 0xffffffff, /* src_mask */
668 0xffffffff, /* dst_mask */
669 FALSE), /* pcrel_offset */
671 EMPTY_HOWTO (R_MIPS_TLS_TPREL64),
673 /* TLS thread pointer offset. */
674 HOWTO (R_MIPS_TLS_TPREL_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_signed, /* complain_on_overflow */
681 _bfd_mips_elf_generic_reloc, /* special_function */
682 "R_MIPS_TLS_TPREL_HI16", /* name */
683 TRUE, /* partial_inplace */
684 0x0000ffff, /* src_mask */
685 0x0000ffff, /* dst_mask */
686 FALSE), /* pcrel_offset */
688 /* TLS thread pointer offset. */
689 HOWTO (R_MIPS_TLS_TPREL_LO16, /* type */
690 0, /* rightshift */
691 2, /* size (0 = byte, 1 = short, 2 = long) */
692 16, /* bitsize */
693 FALSE, /* pc_relative */
694 0, /* bitpos */
695 complain_overflow_signed, /* complain_on_overflow */
696 _bfd_mips_elf_generic_reloc, /* special_function */
697 "R_MIPS_TLS_TPREL_LO16", /* name */
698 TRUE, /* partial_inplace */
699 0x0000ffff, /* src_mask */
700 0x0000ffff, /* dst_mask */
701 FALSE), /* pcrel_offset */
704 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
705 is a hack to make the linker think that we need 64 bit values. */
706 static reloc_howto_type elf_mips_ctor64_howto =
707 HOWTO (R_MIPS_64, /* type */
708 0, /* rightshift */
709 4, /* size (0 = byte, 1 = short, 2 = long) */
710 32, /* bitsize */
711 FALSE, /* pc_relative */
712 0, /* bitpos */
713 complain_overflow_signed, /* complain_on_overflow */
714 mips32_64bit_reloc, /* special_function */
715 "R_MIPS_64", /* name */
716 TRUE, /* partial_inplace */
717 0xffffffff, /* src_mask */
718 0xffffffff, /* dst_mask */
719 FALSE); /* pcrel_offset */
721 static reloc_howto_type elf_mips16_howto_table_rel[] =
723 /* The reloc used for the mips16 jump instruction. */
724 HOWTO (R_MIPS16_26, /* type */
725 2, /* rightshift */
726 2, /* size (0 = byte, 1 = short, 2 = long) */
727 26, /* bitsize */
728 FALSE, /* pc_relative */
729 0, /* bitpos */
730 complain_overflow_dont, /* complain_on_overflow */
731 /* This needs complex overflow
732 detection, because the upper four
733 bits must match the PC. */
734 mips16_jump_reloc, /* special_function */
735 "R_MIPS16_26", /* name */
736 TRUE, /* partial_inplace */
737 0x3ffffff, /* src_mask */
738 0x3ffffff, /* dst_mask */
739 FALSE), /* pcrel_offset */
741 /* The reloc used for the mips16 gprel instruction. */
742 HOWTO (R_MIPS16_GPREL, /* type */
743 0, /* rightshift */
744 2, /* size (0 = byte, 1 = short, 2 = long) */
745 16, /* bitsize */
746 FALSE, /* pc_relative */
747 0, /* bitpos */
748 complain_overflow_signed, /* complain_on_overflow */
749 mips16_gprel_reloc, /* special_function */
750 "R_MIPS16_GPREL", /* name */
751 TRUE, /* partial_inplace */
752 0x0000ffff, /* src_mask */
753 0x0000ffff, /* dst_mask */
754 FALSE), /* pcrel_offset */
756 /* A placeholder for MIPS16 reference to global offset table. */
757 EMPTY_HOWTO (R_MIPS16_GOT16),
759 /* A placeholder for MIPS16 16 bit call through global offset table. */
760 EMPTY_HOWTO (R_MIPS16_CALL16),
762 /* MIPS16 high 16 bits of symbol value. */
763 HOWTO (R_MIPS16_HI16, /* type */
764 16, /* rightshift */
765 2, /* size (0 = byte, 1 = short, 2 = long) */
766 16, /* bitsize */
767 FALSE, /* pc_relative */
768 0, /* bitpos */
769 complain_overflow_dont, /* complain_on_overflow */
770 _bfd_mips_elf_hi16_reloc, /* special_function */
771 "R_MIPS16_HI16", /* name */
772 TRUE, /* partial_inplace */
773 0x0000ffff, /* src_mask */
774 0x0000ffff, /* dst_mask */
775 FALSE), /* pcrel_offset */
777 /* MIPS16 low 16 bits of symbol value. */
778 HOWTO (R_MIPS16_LO16, /* type */
779 0, /* rightshift */
780 2, /* size (0 = byte, 1 = short, 2 = long) */
781 16, /* bitsize */
782 FALSE, /* pc_relative */
783 0, /* bitpos */
784 complain_overflow_dont, /* complain_on_overflow */
785 _bfd_mips_elf_lo16_reloc, /* special_function */
786 "R_MIPS16_LO16", /* name */
787 TRUE, /* partial_inplace */
788 0x0000ffff, /* src_mask */
789 0x0000ffff, /* dst_mask */
790 FALSE), /* pcrel_offset */
793 /* 16 bit offset for pc-relative branches. */
794 static reloc_howto_type elf_mips_gnu_rel16_s2 =
795 HOWTO (R_MIPS_GNU_REL16_S2, /* type */
796 2, /* rightshift */
797 2, /* size (0 = byte, 1 = short, 2 = long) */
798 16, /* bitsize */
799 TRUE, /* pc_relative */
800 0, /* bitpos */
801 complain_overflow_signed, /* complain_on_overflow */
802 _bfd_mips_elf_generic_reloc, /* special_function */
803 "R_MIPS_GNU_REL16_S2", /* name */
804 TRUE, /* partial_inplace */
805 0xffff, /* src_mask */
806 0xffff, /* dst_mask */
807 TRUE); /* pcrel_offset */
809 /* 32 bit pc-relative. This was a GNU extension used by embedded-PIC.
810 It was co-opted by mips-linux for exception-handling data. It is no
811 longer used, but should continue to be supported by the linker for
812 backward compatibility. (GCC stopped using it in May, 2004.) */
813 static reloc_howto_type elf_mips_gnu_pcrel32 =
814 HOWTO (R_MIPS_PC32, /* type */
815 0, /* rightshift */
816 2, /* size (0 = byte, 1 = short, 2 = long) */
817 32, /* bitsize */
818 TRUE, /* pc_relative */
819 0, /* bitpos */
820 complain_overflow_signed, /* complain_on_overflow */
821 _bfd_mips_elf_generic_reloc, /* special_function */
822 "R_MIPS_PC32", /* name */
823 TRUE, /* partial_inplace */
824 0xffffffff, /* src_mask */
825 0xffffffff, /* dst_mask */
826 TRUE); /* pcrel_offset */
828 /* GNU extension to record C++ vtable hierarchy */
829 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
830 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
831 0, /* rightshift */
832 2, /* size (0 = byte, 1 = short, 2 = long) */
833 0, /* bitsize */
834 FALSE, /* pc_relative */
835 0, /* bitpos */
836 complain_overflow_dont, /* complain_on_overflow */
837 NULL, /* special_function */
838 "R_MIPS_GNU_VTINHERIT", /* name */
839 FALSE, /* partial_inplace */
840 0, /* src_mask */
841 0, /* dst_mask */
842 FALSE); /* pcrel_offset */
844 /* GNU extension to record C++ vtable member usage */
845 static reloc_howto_type elf_mips_gnu_vtentry_howto =
846 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
847 0, /* rightshift */
848 2, /* size (0 = byte, 1 = short, 2 = long) */
849 0, /* bitsize */
850 FALSE, /* pc_relative */
851 0, /* bitpos */
852 complain_overflow_dont, /* complain_on_overflow */
853 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
854 "R_MIPS_GNU_VTENTRY", /* name */
855 FALSE, /* partial_inplace */
856 0, /* src_mask */
857 0, /* dst_mask */
858 FALSE); /* pcrel_offset */
860 /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a
861 dangerous relocation. */
863 static bfd_boolean
864 mips_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp)
866 unsigned int count;
867 asymbol **sym;
868 unsigned int i;
870 /* If we've already figured out what GP will be, just return it. */
871 *pgp = _bfd_get_gp_value (output_bfd);
872 if (*pgp)
873 return TRUE;
875 count = bfd_get_symcount (output_bfd);
876 sym = bfd_get_outsymbols (output_bfd);
878 /* The linker script will have created a symbol named `_gp' with the
879 appropriate value. */
880 if (sym == NULL)
881 i = count;
882 else
884 for (i = 0; i < count; i++, sym++)
886 register const char *name;
888 name = bfd_asymbol_name (*sym);
889 if (*name == '_' && strcmp (name, "_gp") == 0)
891 *pgp = bfd_asymbol_value (*sym);
892 _bfd_set_gp_value (output_bfd, *pgp);
893 break;
898 if (i >= count)
900 /* Only get the error once. */
901 *pgp = 4;
902 _bfd_set_gp_value (output_bfd, *pgp);
903 return FALSE;
906 return TRUE;
909 /* We have to figure out the gp value, so that we can adjust the
910 symbol value correctly. We look up the symbol _gp in the output
911 BFD. If we can't find it, we're stuck. We cache it in the ELF
912 target data. We don't need to adjust the symbol value for an
913 external symbol if we are producing relocatable output. */
915 static bfd_reloc_status_type
916 mips_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
917 char **error_message, bfd_vma *pgp)
919 if (bfd_is_und_section (symbol->section)
920 && ! relocatable)
922 *pgp = 0;
923 return bfd_reloc_undefined;
926 *pgp = _bfd_get_gp_value (output_bfd);
927 if (*pgp == 0
928 && (! relocatable
929 || (symbol->flags & BSF_SECTION_SYM) != 0))
931 if (relocatable)
933 /* Make up a value. */
934 *pgp = symbol->section->output_section->vma + 0x4000;
935 _bfd_set_gp_value (output_bfd, *pgp);
937 else if (!mips_elf_assign_gp (output_bfd, pgp))
939 *error_message =
940 (char *) _("GP relative relocation when _gp not defined");
941 return bfd_reloc_dangerous;
945 return bfd_reloc_ok;
948 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
949 become the offset from the gp register. This function also handles
950 R_MIPS_LITERAL relocations, although those can be handled more
951 cleverly because the entries in the .lit8 and .lit4 sections can be
952 merged. */
954 bfd_reloc_status_type
955 _bfd_mips_elf32_gprel16_reloc (bfd *abfd, arelent *reloc_entry,
956 asymbol *symbol, void *data,
957 asection *input_section, bfd *output_bfd,
958 char **error_message)
960 bfd_boolean relocatable;
961 bfd_reloc_status_type ret;
962 bfd_vma gp;
964 /* R_MIPS_LITERAL relocations are defined for local symbols only. */
965 if (reloc_entry->howto->type == R_MIPS_LITERAL
966 && output_bfd != NULL
967 && (symbol->flags & BSF_SECTION_SYM) == 0
968 && (symbol->flags & BSF_LOCAL) != 0)
970 *error_message = (char *)
971 _("literal relocation occurs for an external symbol");
972 return bfd_reloc_outofrange;
975 if (output_bfd != NULL)
976 relocatable = TRUE;
977 else
979 relocatable = FALSE;
980 output_bfd = symbol->section->output_section->owner;
983 ret = mips_elf_final_gp (output_bfd, symbol, relocatable, error_message,
984 &gp);
985 if (ret != bfd_reloc_ok)
986 return ret;
988 return _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
989 input_section, relocatable,
990 data, gp);
993 /* Do a R_MIPS_GPREL32 relocation. This is a 32 bit value which must
994 become the offset from the gp register. */
996 static bfd_reloc_status_type
997 mips_elf_gprel32_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
998 void *data, asection *input_section, bfd *output_bfd,
999 char **error_message)
1001 bfd_boolean relocatable;
1002 bfd_reloc_status_type ret;
1003 bfd_vma gp;
1005 /* R_MIPS_GPREL32 relocations are defined for local symbols only. */
1006 if (output_bfd != NULL
1007 && (symbol->flags & BSF_SECTION_SYM) == 0
1008 && (symbol->flags & BSF_LOCAL) != 0)
1010 *error_message = (char *)
1011 _("32bits gp relative relocation occurs for an external symbol");
1012 return bfd_reloc_outofrange;
1015 if (output_bfd != NULL)
1016 relocatable = TRUE;
1017 else
1019 relocatable = FALSE;
1020 output_bfd = symbol->section->output_section->owner;
1023 ret = mips_elf_final_gp (output_bfd, symbol, relocatable,
1024 error_message, &gp);
1025 if (ret != bfd_reloc_ok)
1026 return ret;
1028 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1029 relocatable, data, gp);
1032 static bfd_reloc_status_type
1033 gprel32_with_gp (bfd *abfd, asymbol *symbol, arelent *reloc_entry,
1034 asection *input_section, bfd_boolean relocatable,
1035 void *data, bfd_vma gp)
1037 bfd_vma relocation;
1038 bfd_vma val;
1040 if (bfd_is_com_section (symbol->section))
1041 relocation = 0;
1042 else
1043 relocation = symbol->value;
1045 relocation += symbol->section->output_section->vma;
1046 relocation += symbol->section->output_offset;
1048 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1049 return bfd_reloc_outofrange;
1051 /* Set val to the offset into the section or symbol. */
1052 val = reloc_entry->addend;
1054 if (reloc_entry->howto->partial_inplace)
1055 val += bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1057 /* Adjust val for the final section location and GP value. If we
1058 are producing relocatable output, we don't want to do this for
1059 an external symbol. */
1060 if (! relocatable
1061 || (symbol->flags & BSF_SECTION_SYM) != 0)
1062 val += relocation - gp;
1064 if (reloc_entry->howto->partial_inplace)
1065 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1066 else
1067 reloc_entry->addend = val;
1069 if (relocatable)
1070 reloc_entry->address += input_section->output_offset;
1072 return bfd_reloc_ok;
1075 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1076 generated when addresses are 64 bits. The upper 32 bits are a simple
1077 sign extension. */
1079 static bfd_reloc_status_type
1080 mips32_64bit_reloc (bfd *abfd, arelent *reloc_entry,
1081 asymbol *symbol ATTRIBUTE_UNUSED,
1082 void *data, asection *input_section,
1083 bfd *output_bfd, char **error_message)
1085 bfd_reloc_status_type r;
1086 arelent reloc32;
1087 unsigned long val;
1088 bfd_size_type addr;
1090 /* Do a normal 32 bit relocation on the lower 32 bits. */
1091 reloc32 = *reloc_entry;
1092 if (bfd_big_endian (abfd))
1093 reloc32.address += 4;
1094 reloc32.howto = &elf_mips_howto_table_rel[R_MIPS_32];
1095 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1096 output_bfd, error_message);
1098 /* Sign extend into the upper 32 bits. */
1099 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1100 if ((val & 0x80000000) != 0)
1101 val = 0xffffffff;
1102 else
1103 val = 0;
1104 addr = reloc_entry->address;
1105 if (bfd_little_endian (abfd))
1106 addr += 4;
1107 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1109 return r;
1112 /* Handle a mips16 jump. */
1114 static bfd_reloc_status_type
1115 mips16_jump_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1116 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1117 asection *input_section, bfd *output_bfd,
1118 char **error_message ATTRIBUTE_UNUSED)
1120 if (output_bfd != NULL
1121 && (symbol->flags & BSF_SECTION_SYM) == 0
1122 && reloc_entry->addend == 0)
1124 reloc_entry->address += input_section->output_offset;
1125 return bfd_reloc_ok;
1128 /* FIXME. */
1130 static bfd_boolean warned;
1132 if (! warned)
1133 (*_bfd_error_handler)
1134 (_("Linking mips16 objects into %s format is not supported"),
1135 bfd_get_target (input_section->output_section->owner));
1136 warned = TRUE;
1139 return bfd_reloc_undefined;
1142 /* Handle a mips16 GP relative reloc. */
1144 static bfd_reloc_status_type
1145 mips16_gprel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1146 void *data, asection *input_section, bfd *output_bfd,
1147 char **error_message)
1149 bfd_boolean relocatable;
1150 bfd_reloc_status_type ret;
1151 bfd_byte *location;
1152 bfd_vma gp;
1154 /* If we're relocating, and this is an external symbol, we don't want
1155 to change anything. */
1156 if (output_bfd != NULL
1157 && (symbol->flags & BSF_SECTION_SYM) == 0
1158 && (symbol->flags & BSF_LOCAL) != 0)
1160 reloc_entry->address += input_section->output_offset;
1161 return bfd_reloc_ok;
1164 if (output_bfd != NULL)
1165 relocatable = TRUE;
1166 else
1168 relocatable = FALSE;
1169 output_bfd = symbol->section->output_section->owner;
1172 ret = mips_elf_final_gp (output_bfd, symbol, relocatable, error_message,
1173 &gp);
1174 if (ret != bfd_reloc_ok)
1175 return ret;
1177 location = (bfd_byte *) data + reloc_entry->address;
1178 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1179 location);
1180 ret = _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
1181 input_section, relocatable,
1182 data, gp);
1183 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, !relocatable,
1184 location);
1186 return ret;
1189 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1191 struct elf_reloc_map {
1192 bfd_reloc_code_real_type bfd_val;
1193 enum elf_mips_reloc_type elf_val;
1196 static const struct elf_reloc_map mips_reloc_map[] =
1198 { BFD_RELOC_NONE, R_MIPS_NONE },
1199 { BFD_RELOC_16, R_MIPS_16 },
1200 { BFD_RELOC_32, R_MIPS_32 },
1201 /* There is no BFD reloc for R_MIPS_REL32. */
1202 { BFD_RELOC_64, R_MIPS_64 },
1203 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1204 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1205 { BFD_RELOC_LO16, R_MIPS_LO16 },
1206 { BFD_RELOC_GPREL16, R_MIPS_GPREL16 },
1207 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1208 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1209 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1210 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1211 { BFD_RELOC_GPREL32, R_MIPS_GPREL32 },
1212 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1213 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1214 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1215 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1216 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1217 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1218 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1219 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP },
1220 { BFD_RELOC_MIPS_TLS_DTPMOD32, R_MIPS_TLS_DTPMOD32 },
1221 { BFD_RELOC_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL32 },
1222 { BFD_RELOC_MIPS_TLS_DTPMOD64, R_MIPS_TLS_DTPMOD64 },
1223 { BFD_RELOC_MIPS_TLS_DTPREL64, R_MIPS_TLS_DTPREL64 },
1224 { BFD_RELOC_MIPS_TLS_GD, R_MIPS_TLS_GD },
1225 { BFD_RELOC_MIPS_TLS_LDM, R_MIPS_TLS_LDM },
1226 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, R_MIPS_TLS_DTPREL_HI16 },
1227 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, R_MIPS_TLS_DTPREL_LO16 },
1228 { BFD_RELOC_MIPS_TLS_GOTTPREL, R_MIPS_TLS_GOTTPREL },
1229 { BFD_RELOC_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL32 },
1230 { BFD_RELOC_MIPS_TLS_TPREL64, R_MIPS_TLS_TPREL64 },
1231 { BFD_RELOC_MIPS_TLS_TPREL_HI16, R_MIPS_TLS_TPREL_HI16 },
1232 { BFD_RELOC_MIPS_TLS_TPREL_LO16, R_MIPS_TLS_TPREL_LO16 }
1235 static const struct elf_reloc_map mips16_reloc_map[] =
1237 { BFD_RELOC_MIPS16_JMP, R_MIPS16_26 - R_MIPS16_min },
1238 { BFD_RELOC_MIPS16_GPREL, R_MIPS16_GPREL - R_MIPS16_min },
1239 { BFD_RELOC_MIPS16_HI16_S, R_MIPS16_HI16 - R_MIPS16_min },
1240 { BFD_RELOC_MIPS16_LO16, R_MIPS16_LO16 - R_MIPS16_min },
1243 /* Given a BFD reloc type, return a howto structure. */
1245 static reloc_howto_type *
1246 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1248 unsigned int i;
1249 reloc_howto_type *howto_table = elf_mips_howto_table_rel;
1250 reloc_howto_type *howto16_table = elf_mips16_howto_table_rel;
1252 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map);
1253 i++)
1255 if (mips_reloc_map[i].bfd_val == code)
1256 return &howto_table[(int) mips_reloc_map[i].elf_val];
1259 for (i = 0; i < sizeof (mips16_reloc_map) / sizeof (struct elf_reloc_map);
1260 i++)
1262 if (mips16_reloc_map[i].bfd_val == code)
1263 return &howto16_table[(int) mips16_reloc_map[i].elf_val];
1266 switch (code)
1268 default:
1269 bfd_set_error (bfd_error_bad_value);
1270 return NULL;
1272 case BFD_RELOC_CTOR:
1273 /* We need to handle BFD_RELOC_CTOR specially.
1274 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1275 size of addresses of the ABI. */
1276 if ((elf_elfheader (abfd)->e_flags & (E_MIPS_ABI_O64
1277 | E_MIPS_ABI_EABI64)) != 0)
1278 return &elf_mips_ctor64_howto;
1279 else
1280 return &howto_table[(int) R_MIPS_32];
1282 case BFD_RELOC_VTABLE_INHERIT:
1283 return &elf_mips_gnu_vtinherit_howto;
1284 case BFD_RELOC_VTABLE_ENTRY:
1285 return &elf_mips_gnu_vtentry_howto;
1286 case BFD_RELOC_16_PCREL_S2:
1287 return &elf_mips_gnu_rel16_s2;
1288 case BFD_RELOC_32_PCREL:
1289 return &elf_mips_gnu_pcrel32;
1293 /* Given a MIPS Elf_Internal_Rel, fill in an arelent structure. */
1295 static reloc_howto_type *
1296 mips_elf32_rtype_to_howto (unsigned int r_type,
1297 bfd_boolean rela_p ATTRIBUTE_UNUSED)
1299 switch (r_type)
1301 case R_MIPS_GNU_VTINHERIT:
1302 return &elf_mips_gnu_vtinherit_howto;
1303 case R_MIPS_GNU_VTENTRY:
1304 return &elf_mips_gnu_vtentry_howto;
1305 case R_MIPS_GNU_REL16_S2:
1306 return &elf_mips_gnu_rel16_s2;
1307 case R_MIPS_PC32:
1308 return &elf_mips_gnu_pcrel32;
1309 default:
1310 if (r_type >= R_MIPS16_min && r_type < R_MIPS16_max)
1311 return &elf_mips16_howto_table_rel[r_type - R_MIPS16_min];
1312 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1313 return &elf_mips_howto_table_rel[r_type];
1317 /* Given a MIPS Elf_Internal_Rel, fill in an arelent structure. */
1319 static void
1320 mips_info_to_howto_rel (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
1322 unsigned int r_type;
1324 r_type = ELF32_R_TYPE (dst->r_info);
1325 cache_ptr->howto = mips_elf32_rtype_to_howto (r_type, FALSE);
1327 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1328 value for the object file. We get the addend now, rather than
1329 when we do the relocation, because the symbol manipulations done
1330 by the linker may cause us to lose track of the input BFD. */
1331 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1332 && (r_type == (unsigned int) R_MIPS_GPREL16
1333 || r_type == (unsigned int) R_MIPS_LITERAL))
1334 cache_ptr->addend = elf_gp (abfd);
1337 /* Given a MIPS Elf_Internal_Rela, fill in an arelent structure. */
1339 static void
1340 mips_info_to_howto_rela (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
1342 mips_info_to_howto_rel (abfd, cache_ptr, dst);
1344 /* If we ever need to do any extra processing with dst->r_addend
1345 (the field omitted in an Elf_Internal_Rel) we can do it here. */
1348 /* Determine whether a symbol is global for the purposes of splitting
1349 the symbol table into global symbols and local symbols. At least
1350 on Irix 5, this split must be between section symbols and all other
1351 symbols. On most ELF targets the split is between static symbols
1352 and externally visible symbols. */
1354 static bfd_boolean
1355 mips_elf_sym_is_global (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
1357 if (SGI_COMPAT (abfd))
1358 return (sym->flags & BSF_SECTION_SYM) == 0;
1359 else
1360 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1361 || bfd_is_und_section (bfd_get_section (sym))
1362 || bfd_is_com_section (bfd_get_section (sym)));
1365 /* Set the right machine number for a MIPS ELF file. */
1367 static bfd_boolean
1368 mips_elf32_object_p (bfd *abfd)
1370 unsigned long mach;
1372 /* Irix 5 and 6 are broken. Object file symbol tables are not always
1373 sorted correctly such that local symbols precede global symbols,
1374 and the sh_info field in the symbol table is not always right. */
1375 if (SGI_COMPAT (abfd))
1376 elf_bad_symtab (abfd) = TRUE;
1378 if (ABI_N32_P (abfd))
1379 return FALSE;
1381 mach = _bfd_elf_mips_mach (elf_elfheader (abfd)->e_flags);
1382 bfd_default_set_arch_mach (abfd, bfd_arch_mips, mach);
1384 return TRUE;
1387 /* MIPS ELF local labels start with '$', not 'L'. */
1389 static bfd_boolean
1390 mips_elf_is_local_label_name (bfd *abfd, const char *name)
1392 if (name[0] == '$')
1393 return TRUE;
1395 /* On Irix 6, the labels go back to starting with '.', so we accept
1396 the generic ELF local label syntax as well. */
1397 return _bfd_elf_is_local_label_name (abfd, name);
1400 /* Support for core dump NOTE sections. */
1401 static bfd_boolean
1402 elf32_mips_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1404 int offset;
1405 unsigned int size;
1407 switch (note->descsz)
1409 default:
1410 return FALSE;
1412 case 256: /* Linux/MIPS */
1413 /* pr_cursig */
1414 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1416 /* pr_pid */
1417 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1419 /* pr_reg */
1420 offset = 72;
1421 size = 180;
1423 break;
1426 /* Make a ".reg/999" section. */
1427 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1428 size, note->descpos + offset);
1431 static bfd_boolean
1432 elf32_mips_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1434 switch (note->descsz)
1436 default:
1437 return FALSE;
1439 case 128: /* Linux/MIPS elf_prpsinfo */
1440 elf_tdata (abfd)->core_program
1441 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1442 elf_tdata (abfd)->core_command
1443 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1446 /* Note that for some reason, a spurious space is tacked
1447 onto the end of the args in some (at least one anyway)
1448 implementations, so strip it off if it exists. */
1451 char *command = elf_tdata (abfd)->core_command;
1452 int n = strlen (command);
1454 if (0 < n && command[n - 1] == ' ')
1455 command[n - 1] = '\0';
1458 return TRUE;
1461 /* Depending on the target vector we generate some version of Irix
1462 executables or "normal" MIPS ELF ABI executables. */
1463 static irix_compat_t
1464 elf32_mips_irix_compat (bfd *abfd)
1466 if ((abfd->xvec == &bfd_elf32_bigmips_vec)
1467 || (abfd->xvec == &bfd_elf32_littlemips_vec))
1468 return ict_irix5;
1469 else
1470 return ict_none;
1473 /* ECOFF swapping routines. These are used when dealing with the
1474 .mdebug section, which is in the ECOFF debugging format. */
1475 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
1476 /* Symbol table magic number. */
1477 magicSym,
1478 /* Alignment of debugging information. E.g., 4. */
1480 /* Sizes of external symbolic information. */
1481 sizeof (struct hdr_ext),
1482 sizeof (struct dnr_ext),
1483 sizeof (struct pdr_ext),
1484 sizeof (struct sym_ext),
1485 sizeof (struct opt_ext),
1486 sizeof (struct fdr_ext),
1487 sizeof (struct rfd_ext),
1488 sizeof (struct ext_ext),
1489 /* Functions to swap in external symbolic data. */
1490 ecoff_swap_hdr_in,
1491 ecoff_swap_dnr_in,
1492 ecoff_swap_pdr_in,
1493 ecoff_swap_sym_in,
1494 ecoff_swap_opt_in,
1495 ecoff_swap_fdr_in,
1496 ecoff_swap_rfd_in,
1497 ecoff_swap_ext_in,
1498 _bfd_ecoff_swap_tir_in,
1499 _bfd_ecoff_swap_rndx_in,
1500 /* Functions to swap out external symbolic data. */
1501 ecoff_swap_hdr_out,
1502 ecoff_swap_dnr_out,
1503 ecoff_swap_pdr_out,
1504 ecoff_swap_sym_out,
1505 ecoff_swap_opt_out,
1506 ecoff_swap_fdr_out,
1507 ecoff_swap_rfd_out,
1508 ecoff_swap_ext_out,
1509 _bfd_ecoff_swap_tir_out,
1510 _bfd_ecoff_swap_rndx_out,
1511 /* Function to read in symbolic data. */
1512 _bfd_mips_elf_read_ecoff_info
1515 #define ELF_ARCH bfd_arch_mips
1516 #define ELF_MACHINE_CODE EM_MIPS
1518 #define elf_backend_collect TRUE
1519 #define elf_backend_type_change_ok TRUE
1520 #define elf_backend_can_gc_sections TRUE
1521 #define elf_info_to_howto mips_info_to_howto_rela
1522 #define elf_info_to_howto_rel mips_info_to_howto_rel
1523 #define elf_backend_sym_is_global mips_elf_sym_is_global
1524 #define elf_backend_object_p mips_elf32_object_p
1525 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
1526 #define elf_backend_section_processing _bfd_mips_elf_section_processing
1527 #define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
1528 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
1529 #define elf_backend_section_from_bfd_section \
1530 _bfd_mips_elf_section_from_bfd_section
1531 #define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
1532 #define elf_backend_link_output_symbol_hook \
1533 _bfd_mips_elf_link_output_symbol_hook
1534 #define elf_backend_create_dynamic_sections \
1535 _bfd_mips_elf_create_dynamic_sections
1536 #define elf_backend_check_relocs _bfd_mips_elf_check_relocs
1537 #define elf_backend_adjust_dynamic_symbol \
1538 _bfd_mips_elf_adjust_dynamic_symbol
1539 #define elf_backend_always_size_sections \
1540 _bfd_mips_elf_always_size_sections
1541 #define elf_backend_size_dynamic_sections \
1542 _bfd_mips_elf_size_dynamic_sections
1543 #define elf_backend_relocate_section _bfd_mips_elf_relocate_section
1544 #define elf_backend_finish_dynamic_symbol \
1545 _bfd_mips_elf_finish_dynamic_symbol
1546 #define elf_backend_finish_dynamic_sections \
1547 _bfd_mips_elf_finish_dynamic_sections
1548 #define elf_backend_final_write_processing \
1549 _bfd_mips_elf_final_write_processing
1550 #define elf_backend_additional_program_headers \
1551 _bfd_mips_elf_additional_program_headers
1552 #define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
1553 #define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
1554 #define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
1555 #define elf_backend_copy_indirect_symbol \
1556 _bfd_mips_elf_copy_indirect_symbol
1557 #define elf_backend_hide_symbol _bfd_mips_elf_hide_symbol
1558 #define elf_backend_grok_prstatus elf32_mips_grok_prstatus
1559 #define elf_backend_grok_psinfo elf32_mips_grok_psinfo
1560 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
1562 #define elf_backend_got_header_size (4 * MIPS_RESERVED_GOTNO)
1563 #define elf_backend_may_use_rel_p 1
1564 #define elf_backend_may_use_rela_p 0
1565 #define elf_backend_default_use_rela_p 0
1566 #define elf_backend_sign_extend_vma TRUE
1568 #define elf_backend_discard_info _bfd_mips_elf_discard_info
1569 #define elf_backend_ignore_discarded_relocs \
1570 _bfd_mips_elf_ignore_discarded_relocs
1571 #define elf_backend_mips_irix_compat elf32_mips_irix_compat
1572 #define elf_backend_mips_rtype_to_howto mips_elf32_rtype_to_howto
1573 #define bfd_elf32_bfd_is_local_label_name \
1574 mips_elf_is_local_label_name
1575 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
1576 #define bfd_elf32_new_section_hook _bfd_mips_elf_new_section_hook
1577 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
1578 #define bfd_elf32_bfd_get_relocated_section_contents \
1579 _bfd_elf_mips_get_relocated_section_contents
1580 #define bfd_elf32_bfd_link_hash_table_create \
1581 _bfd_mips_elf_link_hash_table_create
1582 #define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
1583 #define bfd_elf32_bfd_merge_private_bfd_data \
1584 _bfd_mips_elf_merge_private_bfd_data
1585 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
1586 #define bfd_elf32_bfd_print_private_bfd_data \
1587 _bfd_mips_elf_print_private_bfd_data
1589 /* Support for SGI-ish mips targets. */
1590 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
1591 #define TARGET_LITTLE_NAME "elf32-littlemips"
1592 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
1593 #define TARGET_BIG_NAME "elf32-bigmips"
1595 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
1596 a value of 0x1000, and we are compatible. */
1597 #define ELF_MAXPAGESIZE 0x1000
1599 #include "elf32-target.h"
1601 /* Support for traditional mips targets. */
1602 #undef TARGET_LITTLE_SYM
1603 #undef TARGET_LITTLE_NAME
1604 #undef TARGET_BIG_SYM
1605 #undef TARGET_BIG_NAME
1607 #undef ELF_MAXPAGESIZE
1609 #define TARGET_LITTLE_SYM bfd_elf32_tradlittlemips_vec
1610 #define TARGET_LITTLE_NAME "elf32-tradlittlemips"
1611 #define TARGET_BIG_SYM bfd_elf32_tradbigmips_vec
1612 #define TARGET_BIG_NAME "elf32-tradbigmips"
1614 /* The SVR4 MIPS ABI says that this should be 0x10000, and Linux uses
1615 page sizes of up to that limit, so we need to respect it. */
1616 #define ELF_MAXPAGESIZE 0x10000
1617 #define elf32_bed elf32_tradbed
1619 /* Include the target file again for this target. */
1620 #include "elf32-target.h"