Tue Apr 9 15:13:10 2002 J"orn Rennecke <joern.rennecke@superh.com>
[binutils.git] / bfd / elf32-mips.c
blob95eabc9a09544534707adbdcb1b2d7e130b6636c
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
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 mips_elf_hi16_reloc
51 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
52 static bfd_reloc_status_type mips_elf_lo16_reloc
53 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
54 static bfd_reloc_status_type mips_elf_got16_reloc
55 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
56 static bfd_reloc_status_type mips_elf_gprel32_reloc
57 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
58 static bfd_reloc_status_type mips32_64bit_reloc
59 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
60 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
61 PARAMS ((bfd *, bfd_reloc_code_real_type));
62 static reloc_howto_type *mips_elf32_rtype_to_howto
63 PARAMS ((unsigned int, boolean));
64 static void mips_info_to_howto_rel
65 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
66 static void mips_info_to_howto_rela
67 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
68 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
69 static boolean mips_elf32_object_p PARAMS ((bfd *));
70 static boolean mips_elf_is_local_label_name
71 PARAMS ((bfd *, const char *));
72 static bfd_reloc_status_type mips16_jump_reloc
73 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
74 static bfd_reloc_status_type mips16_gprel_reloc
75 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
76 static bfd_reloc_status_type mips_elf_final_gp
77 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
78 static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
79 static boolean elf32_mips_grok_prstatus
80 PARAMS ((bfd *, Elf_Internal_Note *));
81 static boolean elf32_mips_grok_psinfo
82 PARAMS ((bfd *, Elf_Internal_Note *));
83 static boolean elf32_mips_discard_info
84 PARAMS ((bfd *, struct elf_reloc_cookie *, struct bfd_link_info *));
85 static boolean elf32_mips_ignore_discarded_relocs
86 PARAMS ((asection *));
87 static boolean elf32_mips_write_section
88 PARAMS ((bfd *, asection *, bfd_byte *));
89 static irix_compat_t elf32_mips_irix_compat
90 PARAMS ((bfd *));
92 extern const bfd_target bfd_elf32_tradbigmips_vec;
93 extern const bfd_target bfd_elf32_tradlittlemips_vec;
95 /* Nonzero if ABFD is using the N32 ABI. */
97 #define ABI_N32_P(abfd) \
98 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
100 /* Nonzero if ABFD is using the 64-bit ABI. */
101 #define ABI_64_P(abfd) \
102 ((elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) != 0)
104 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
106 /* Whether we are trying to be compatible with IRIX at all. */
107 #define SGI_COMPAT(abfd) \
108 (elf32_mips_irix_compat (abfd) != ict_none)
110 /* The size of an external REL relocation. */
111 #define MIPS_ELF_REL_SIZE(abfd) \
112 (get_elf_backend_data (abfd)->s->sizeof_rel)
114 /* The number of local .got entries we reserve. */
115 #define MIPS_RESERVED_GOTNO (2)
117 #if 0
118 /* We no longer try to identify particular sections for the .dynsym
119 section. When we do, we wind up crashing if there are other random
120 sections with relocations. */
122 /* Names of sections which appear in the .dynsym section in an Irix 5
123 executable. */
125 static const char * const mips_elf_dynsym_sec_names[] =
127 ".text",
128 ".init",
129 ".fini",
130 ".data",
131 ".rodata",
132 ".sdata",
133 ".sbss",
134 ".bss",
135 NULL
138 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
139 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
141 /* The number of entries in mips_elf_dynsym_sec_names which go in the
142 text segment. */
144 #define MIPS_TEXT_DYNSYM_SECNO (3)
146 #endif /* 0 */
148 /* The names of the runtime procedure table symbols used on Irix 5. */
150 static const char * const mips_elf_dynsym_rtproc_names[] =
152 "_procedure_table",
153 "_procedure_string_table",
154 "_procedure_table_size",
155 NULL
158 /* These structures are used to generate the .compact_rel section on
159 Irix 5. */
161 typedef struct
163 unsigned long id1; /* Always one? */
164 unsigned long num; /* Number of compact relocation entries. */
165 unsigned long id2; /* Always two? */
166 unsigned long offset; /* The file offset of the first relocation. */
167 unsigned long reserved0; /* Zero? */
168 unsigned long reserved1; /* Zero? */
169 } Elf32_compact_rel;
171 typedef struct
173 bfd_byte id1[4];
174 bfd_byte num[4];
175 bfd_byte id2[4];
176 bfd_byte offset[4];
177 bfd_byte reserved0[4];
178 bfd_byte reserved1[4];
179 } Elf32_External_compact_rel;
181 typedef struct
183 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
184 unsigned int rtype : 4; /* Relocation types. See below. */
185 unsigned int dist2to : 8;
186 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
187 unsigned long konst; /* KONST field. See below. */
188 unsigned long vaddr; /* VADDR to be relocated. */
189 } Elf32_crinfo;
191 typedef struct
193 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
194 unsigned int rtype : 4; /* Relocation types. See below. */
195 unsigned int dist2to : 8;
196 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
197 unsigned long konst; /* KONST field. See below. */
198 } Elf32_crinfo2;
200 typedef struct
202 bfd_byte info[4];
203 bfd_byte konst[4];
204 bfd_byte vaddr[4];
205 } Elf32_External_crinfo;
207 typedef struct
209 bfd_byte info[4];
210 bfd_byte konst[4];
211 } Elf32_External_crinfo2;
213 /* These are the constants used to swap the bitfields in a crinfo. */
215 #define CRINFO_CTYPE (0x1)
216 #define CRINFO_CTYPE_SH (31)
217 #define CRINFO_RTYPE (0xf)
218 #define CRINFO_RTYPE_SH (27)
219 #define CRINFO_DIST2TO (0xff)
220 #define CRINFO_DIST2TO_SH (19)
221 #define CRINFO_RELVADDR (0x7ffff)
222 #define CRINFO_RELVADDR_SH (0)
224 /* A compact relocation info has long (3 words) or short (2 words)
225 formats. A short format doesn't have VADDR field and relvaddr
226 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
227 #define CRF_MIPS_LONG 1
228 #define CRF_MIPS_SHORT 0
230 /* There are 4 types of compact relocation at least. The value KONST
231 has different meaning for each type:
233 (type) (konst)
234 CT_MIPS_REL32 Address in data
235 CT_MIPS_WORD Address in word (XXX)
236 CT_MIPS_GPHI_LO GP - vaddr
237 CT_MIPS_JMPAD Address to jump
240 #define CRT_MIPS_REL32 0xa
241 #define CRT_MIPS_WORD 0xb
242 #define CRT_MIPS_GPHI_LO 0xc
243 #define CRT_MIPS_JMPAD 0xd
245 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
246 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
247 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
248 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
250 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
251 from smaller values. Start with zero, widen, *then* decrement. */
252 #define MINUS_ONE (((bfd_vma)0) - 1)
254 /* The relocation table used for SHT_REL sections. */
256 static reloc_howto_type elf_mips_howto_table_rel[] =
258 /* No relocation. */
259 HOWTO (R_MIPS_NONE, /* type */
260 0, /* rightshift */
261 0, /* size (0 = byte, 1 = short, 2 = long) */
262 0, /* bitsize */
263 false, /* pc_relative */
264 0, /* bitpos */
265 complain_overflow_dont, /* complain_on_overflow */
266 bfd_elf_generic_reloc, /* special_function */
267 "R_MIPS_NONE", /* name */
268 false, /* partial_inplace */
269 0, /* src_mask */
270 0, /* dst_mask */
271 false), /* pcrel_offset */
273 /* 16 bit relocation. */
274 HOWTO (R_MIPS_16, /* type */
275 0, /* rightshift */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
277 16, /* bitsize */
278 false, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_signed, /* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_MIPS_16", /* name */
283 true, /* partial_inplace */
284 0x0000ffff, /* src_mask */
285 0x0000ffff, /* dst_mask */
286 false), /* pcrel_offset */
288 /* 32 bit relocation. */
289 HOWTO (R_MIPS_32, /* type */
290 0, /* rightshift */
291 2, /* size (0 = byte, 1 = short, 2 = long) */
292 32, /* bitsize */
293 false, /* pc_relative */
294 0, /* bitpos */
295 complain_overflow_dont, /* complain_on_overflow */
296 bfd_elf_generic_reloc, /* special_function */
297 "R_MIPS_32", /* name */
298 true, /* partial_inplace */
299 0xffffffff, /* src_mask */
300 0xffffffff, /* dst_mask */
301 false), /* pcrel_offset */
303 /* 32 bit symbol relative relocation. */
304 HOWTO (R_MIPS_REL32, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 32, /* bitsize */
308 false, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_dont, /* complain_on_overflow */
311 bfd_elf_generic_reloc, /* special_function */
312 "R_MIPS_REL32", /* name */
313 true, /* partial_inplace */
314 0xffffffff, /* src_mask */
315 0xffffffff, /* dst_mask */
316 false), /* pcrel_offset */
318 /* 26 bit jump address. */
319 HOWTO (R_MIPS_26, /* type */
320 2, /* rightshift */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
322 26, /* bitsize */
323 false, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_dont, /* complain_on_overflow */
326 /* This needs complex overflow
327 detection, because the upper four
328 bits must match the PC + 4. */
329 bfd_elf_generic_reloc, /* special_function */
330 "R_MIPS_26", /* name */
331 true, /* partial_inplace */
332 0x03ffffff, /* src_mask */
333 0x03ffffff, /* dst_mask */
334 false), /* pcrel_offset */
336 /* High 16 bits of symbol value. */
337 HOWTO (R_MIPS_HI16, /* type */
338 0, /* rightshift */
339 2, /* size (0 = byte, 1 = short, 2 = long) */
340 16, /* bitsize */
341 false, /* pc_relative */
342 0, /* bitpos */
343 complain_overflow_dont, /* complain_on_overflow */
344 mips_elf_hi16_reloc, /* special_function */
345 "R_MIPS_HI16", /* name */
346 true, /* partial_inplace */
347 0x0000ffff, /* src_mask */
348 0x0000ffff, /* dst_mask */
349 false), /* pcrel_offset */
351 /* Low 16 bits of symbol value. */
352 HOWTO (R_MIPS_LO16, /* type */
353 0, /* rightshift */
354 2, /* size (0 = byte, 1 = short, 2 = long) */
355 16, /* bitsize */
356 false, /* pc_relative */
357 0, /* bitpos */
358 complain_overflow_dont, /* complain_on_overflow */
359 mips_elf_lo16_reloc, /* special_function */
360 "R_MIPS_LO16", /* name */
361 true, /* partial_inplace */
362 0x0000ffff, /* src_mask */
363 0x0000ffff, /* dst_mask */
364 false), /* pcrel_offset */
366 /* GP relative reference. */
367 HOWTO (R_MIPS_GPREL16, /* type */
368 0, /* rightshift */
369 2, /* size (0 = byte, 1 = short, 2 = long) */
370 16, /* bitsize */
371 false, /* pc_relative */
372 0, /* bitpos */
373 complain_overflow_signed, /* complain_on_overflow */
374 _bfd_mips_elf32_gprel16_reloc, /* special_function */
375 "R_MIPS_GPREL16", /* name */
376 true, /* partial_inplace */
377 0x0000ffff, /* src_mask */
378 0x0000ffff, /* dst_mask */
379 false), /* pcrel_offset */
381 /* Reference to literal section. */
382 HOWTO (R_MIPS_LITERAL, /* type */
383 0, /* rightshift */
384 2, /* size (0 = byte, 1 = short, 2 = long) */
385 16, /* bitsize */
386 false, /* pc_relative */
387 0, /* bitpos */
388 complain_overflow_signed, /* complain_on_overflow */
389 _bfd_mips_elf32_gprel16_reloc, /* special_function */
390 "R_MIPS_LITERAL", /* name */
391 true, /* partial_inplace */
392 0x0000ffff, /* src_mask */
393 0x0000ffff, /* dst_mask */
394 false), /* pcrel_offset */
396 /* Reference to global offset table. */
397 HOWTO (R_MIPS_GOT16, /* type */
398 0, /* rightshift */
399 2, /* size (0 = byte, 1 = short, 2 = long) */
400 16, /* bitsize */
401 false, /* pc_relative */
402 0, /* bitpos */
403 complain_overflow_signed, /* complain_on_overflow */
404 mips_elf_got16_reloc, /* special_function */
405 "R_MIPS_GOT16", /* name */
406 true, /* partial_inplace */
407 0x0000ffff, /* src_mask */
408 0x0000ffff, /* dst_mask */
409 false), /* pcrel_offset */
411 /* 16 bit PC relative reference. */
412 HOWTO (R_MIPS_PC16, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 true, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_signed, /* complain_on_overflow */
419 bfd_elf_generic_reloc, /* special_function */
420 "R_MIPS_PC16", /* name */
421 true, /* partial_inplace */
422 0x0000ffff, /* src_mask */
423 0x0000ffff, /* dst_mask */
424 true), /* pcrel_offset */
426 /* 16 bit call through global offset table. */
427 HOWTO (R_MIPS_CALL16, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 16, /* bitsize */
431 false, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
434 bfd_elf_generic_reloc, /* special_function */
435 "R_MIPS_CALL16", /* name */
436 true, /* partial_inplace */
437 0x0000ffff, /* src_mask */
438 0x0000ffff, /* dst_mask */
439 false), /* pcrel_offset */
441 /* 32 bit GP relative reference. */
442 HOWTO (R_MIPS_GPREL32, /* type */
443 0, /* rightshift */
444 2, /* size (0 = byte, 1 = short, 2 = long) */
445 32, /* bitsize */
446 false, /* pc_relative */
447 0, /* bitpos */
448 complain_overflow_dont, /* complain_on_overflow */
449 mips_elf_gprel32_reloc, /* special_function */
450 "R_MIPS_GPREL32", /* name */
451 true, /* partial_inplace */
452 0xffffffff, /* src_mask */
453 0xffffffff, /* dst_mask */
454 false), /* pcrel_offset */
456 /* The remaining relocs are defined on Irix 5, although they are
457 not defined by the ABI. */
458 EMPTY_HOWTO (13),
459 EMPTY_HOWTO (14),
460 EMPTY_HOWTO (15),
462 /* A 5 bit shift field. */
463 HOWTO (R_MIPS_SHIFT5, /* type */
464 0, /* rightshift */
465 2, /* size (0 = byte, 1 = short, 2 = long) */
466 5, /* bitsize */
467 false, /* pc_relative */
468 6, /* bitpos */
469 complain_overflow_bitfield, /* complain_on_overflow */
470 bfd_elf_generic_reloc, /* special_function */
471 "R_MIPS_SHIFT5", /* name */
472 true, /* partial_inplace */
473 0x000007c0, /* src_mask */
474 0x000007c0, /* dst_mask */
475 false), /* pcrel_offset */
477 /* A 6 bit shift field. */
478 /* FIXME: This is not handled correctly; a special function is
479 needed to put the most significant bit in the right place. */
480 HOWTO (R_MIPS_SHIFT6, /* type */
481 0, /* rightshift */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
483 6, /* bitsize */
484 false, /* pc_relative */
485 6, /* bitpos */
486 complain_overflow_bitfield, /* complain_on_overflow */
487 bfd_elf_generic_reloc, /* special_function */
488 "R_MIPS_SHIFT6", /* name */
489 true, /* partial_inplace */
490 0x000007c4, /* src_mask */
491 0x000007c4, /* dst_mask */
492 false), /* pcrel_offset */
494 /* A 64 bit relocation. */
495 HOWTO (R_MIPS_64, /* type */
496 0, /* rightshift */
497 4, /* size (0 = byte, 1 = short, 2 = long) */
498 64, /* bitsize */
499 false, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_dont, /* complain_on_overflow */
502 mips32_64bit_reloc, /* special_function */
503 "R_MIPS_64", /* name */
504 true, /* partial_inplace */
505 MINUS_ONE, /* src_mask */
506 MINUS_ONE, /* dst_mask */
507 false), /* pcrel_offset */
509 /* Displacement in the global offset table. */
510 HOWTO (R_MIPS_GOT_DISP, /* type */
511 0, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 16, /* bitsize */
514 false, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_signed, /* complain_on_overflow */
517 bfd_elf_generic_reloc, /* special_function */
518 "R_MIPS_GOT_DISP", /* name */
519 true, /* partial_inplace */
520 0x0000ffff, /* src_mask */
521 0x0000ffff, /* dst_mask */
522 false), /* pcrel_offset */
524 /* Displacement to page pointer in the global offset table. */
525 HOWTO (R_MIPS_GOT_PAGE, /* type */
526 0, /* rightshift */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
528 16, /* bitsize */
529 false, /* pc_relative */
530 0, /* bitpos */
531 complain_overflow_signed, /* complain_on_overflow */
532 bfd_elf_generic_reloc, /* special_function */
533 "R_MIPS_GOT_PAGE", /* name */
534 true, /* partial_inplace */
535 0x0000ffff, /* src_mask */
536 0x0000ffff, /* dst_mask */
537 false), /* pcrel_offset */
539 /* Offset from page pointer in the global offset table. */
540 HOWTO (R_MIPS_GOT_OFST, /* type */
541 0, /* rightshift */
542 2, /* size (0 = byte, 1 = short, 2 = long) */
543 16, /* bitsize */
544 false, /* pc_relative */
545 0, /* bitpos */
546 complain_overflow_signed, /* complain_on_overflow */
547 bfd_elf_generic_reloc, /* special_function */
548 "R_MIPS_GOT_OFST", /* name */
549 true, /* partial_inplace */
550 0x0000ffff, /* src_mask */
551 0x0000ffff, /* dst_mask */
552 false), /* pcrel_offset */
554 /* High 16 bits of displacement in global offset table. */
555 HOWTO (R_MIPS_GOT_HI16, /* type */
556 0, /* rightshift */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
558 16, /* bitsize */
559 false, /* pc_relative */
560 0, /* bitpos */
561 complain_overflow_dont, /* complain_on_overflow */
562 bfd_elf_generic_reloc, /* special_function */
563 "R_MIPS_GOT_HI16", /* name */
564 true, /* partial_inplace */
565 0x0000ffff, /* src_mask */
566 0x0000ffff, /* dst_mask */
567 false), /* pcrel_offset */
569 /* Low 16 bits of displacement in global offset table. */
570 HOWTO (R_MIPS_GOT_LO16, /* type */
571 0, /* rightshift */
572 2, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
574 false, /* pc_relative */
575 0, /* bitpos */
576 complain_overflow_dont, /* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_MIPS_GOT_LO16", /* name */
579 true, /* partial_inplace */
580 0x0000ffff, /* src_mask */
581 0x0000ffff, /* dst_mask */
582 false), /* pcrel_offset */
584 /* 64 bit subtraction. Used in the N32 ABI. */
585 HOWTO (R_MIPS_SUB, /* type */
586 0, /* rightshift */
587 4, /* size (0 = byte, 1 = short, 2 = long) */
588 64, /* bitsize */
589 false, /* pc_relative */
590 0, /* bitpos */
591 complain_overflow_dont, /* complain_on_overflow */
592 bfd_elf_generic_reloc, /* special_function */
593 "R_MIPS_SUB", /* name */
594 true, /* partial_inplace */
595 MINUS_ONE, /* src_mask */
596 MINUS_ONE, /* dst_mask */
597 false), /* pcrel_offset */
599 /* Used to cause the linker to insert and delete instructions? */
600 EMPTY_HOWTO (R_MIPS_INSERT_A),
601 EMPTY_HOWTO (R_MIPS_INSERT_B),
602 EMPTY_HOWTO (R_MIPS_DELETE),
604 /* Get the higher value of a 64 bit addend. */
605 HOWTO (R_MIPS_HIGHER, /* type */
606 0, /* rightshift */
607 2, /* size (0 = byte, 1 = short, 2 = long) */
608 16, /* bitsize */
609 false, /* pc_relative */
610 0, /* bitpos */
611 complain_overflow_dont, /* complain_on_overflow */
612 bfd_elf_generic_reloc, /* special_function */
613 "R_MIPS_HIGHER", /* name */
614 true, /* partial_inplace */
615 0x0000ffff, /* src_mask */
616 0x0000ffff, /* dst_mask */
617 false), /* pcrel_offset */
619 /* Get the highest value of a 64 bit addend. */
620 HOWTO (R_MIPS_HIGHEST, /* type */
621 0, /* rightshift */
622 2, /* size (0 = byte, 1 = short, 2 = long) */
623 16, /* bitsize */
624 false, /* pc_relative */
625 0, /* bitpos */
626 complain_overflow_dont, /* complain_on_overflow */
627 bfd_elf_generic_reloc, /* special_function */
628 "R_MIPS_HIGHEST", /* name */
629 true, /* partial_inplace */
630 0x0000ffff, /* src_mask */
631 0x0000ffff, /* dst_mask */
632 false), /* pcrel_offset */
634 /* High 16 bits of displacement in global offset table. */
635 HOWTO (R_MIPS_CALL_HI16, /* type */
636 0, /* rightshift */
637 2, /* size (0 = byte, 1 = short, 2 = long) */
638 16, /* bitsize */
639 false, /* pc_relative */
640 0, /* bitpos */
641 complain_overflow_dont, /* complain_on_overflow */
642 bfd_elf_generic_reloc, /* special_function */
643 "R_MIPS_CALL_HI16", /* name */
644 true, /* partial_inplace */
645 0x0000ffff, /* src_mask */
646 0x0000ffff, /* dst_mask */
647 false), /* pcrel_offset */
649 /* Low 16 bits of displacement in global offset table. */
650 HOWTO (R_MIPS_CALL_LO16, /* type */
651 0, /* rightshift */
652 2, /* size (0 = byte, 1 = short, 2 = long) */
653 16, /* bitsize */
654 false, /* pc_relative */
655 0, /* bitpos */
656 complain_overflow_dont, /* complain_on_overflow */
657 bfd_elf_generic_reloc, /* special_function */
658 "R_MIPS_CALL_LO16", /* name */
659 true, /* partial_inplace */
660 0x0000ffff, /* src_mask */
661 0x0000ffff, /* dst_mask */
662 false), /* pcrel_offset */
664 /* Section displacement. */
665 HOWTO (R_MIPS_SCN_DISP, /* type */
666 0, /* rightshift */
667 2, /* size (0 = byte, 1 = short, 2 = long) */
668 32, /* bitsize */
669 false, /* pc_relative */
670 0, /* bitpos */
671 complain_overflow_dont, /* complain_on_overflow */
672 bfd_elf_generic_reloc, /* special_function */
673 "R_MIPS_SCN_DISP", /* name */
674 true, /* partial_inplace */
675 0xffffffff, /* src_mask */
676 0xffffffff, /* dst_mask */
677 false), /* pcrel_offset */
679 EMPTY_HOWTO (R_MIPS_REL16),
680 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
681 EMPTY_HOWTO (R_MIPS_PJUMP),
682 EMPTY_HOWTO (R_MIPS_RELGOT),
684 /* Protected jump conversion. This is an optimization hint. No
685 relocation is required for correctness. */
686 HOWTO (R_MIPS_JALR, /* type */
687 0, /* rightshift */
688 2, /* size (0 = byte, 1 = short, 2 = long) */
689 32, /* bitsize */
690 false, /* pc_relative */
691 0, /* bitpos */
692 complain_overflow_dont, /* complain_on_overflow */
693 bfd_elf_generic_reloc, /* special_function */
694 "R_MIPS_JALR", /* name */
695 false, /* partial_inplace */
696 0x00000000, /* src_mask */
697 0x00000000, /* dst_mask */
698 false), /* pcrel_offset */
701 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
702 is a hack to make the linker think that we need 64 bit values. */
703 static reloc_howto_type elf_mips_ctor64_howto =
704 HOWTO (R_MIPS_64, /* type */
705 0, /* rightshift */
706 4, /* size (0 = byte, 1 = short, 2 = long) */
707 32, /* bitsize */
708 false, /* pc_relative */
709 0, /* bitpos */
710 complain_overflow_signed, /* complain_on_overflow */
711 mips32_64bit_reloc, /* special_function */
712 "R_MIPS_64", /* name */
713 true, /* partial_inplace */
714 0xffffffff, /* src_mask */
715 0xffffffff, /* dst_mask */
716 false); /* pcrel_offset */
718 /* The reloc used for the mips16 jump instruction. */
719 static reloc_howto_type elf_mips16_jump_howto =
720 HOWTO (R_MIPS16_26, /* type */
721 2, /* rightshift */
722 2, /* size (0 = byte, 1 = short, 2 = long) */
723 26, /* bitsize */
724 false, /* pc_relative */
725 0, /* bitpos */
726 complain_overflow_dont, /* complain_on_overflow */
727 /* This needs complex overflow
728 detection, because the upper four
729 bits must match the PC. */
730 mips16_jump_reloc, /* special_function */
731 "R_MIPS16_26", /* name */
732 true, /* partial_inplace */
733 0x3ffffff, /* src_mask */
734 0x3ffffff, /* dst_mask */
735 false); /* pcrel_offset */
737 /* The reloc used for the mips16 gprel instruction. */
738 static reloc_howto_type elf_mips16_gprel_howto =
739 HOWTO (R_MIPS16_GPREL, /* type */
740 0, /* rightshift */
741 2, /* size (0 = byte, 1 = short, 2 = long) */
742 16, /* bitsize */
743 false, /* pc_relative */
744 0, /* bitpos */
745 complain_overflow_signed, /* complain_on_overflow */
746 mips16_gprel_reloc, /* special_function */
747 "R_MIPS16_GPREL", /* name */
748 true, /* partial_inplace */
749 0x07ff001f, /* src_mask */
750 0x07ff001f, /* dst_mask */
751 false); /* pcrel_offset */
753 /* GNU extensions for embedded-pic. */
754 /* High 16 bits of symbol value, pc-relative. */
755 static reloc_howto_type elf_mips_gnu_rel_hi16 =
756 HOWTO (R_MIPS_GNU_REL_HI16, /* type */
757 0, /* rightshift */
758 2, /* size (0 = byte, 1 = short, 2 = long) */
759 16, /* bitsize */
760 true, /* pc_relative */
761 0, /* bitpos */
762 complain_overflow_dont, /* complain_on_overflow */
763 mips_elf_hi16_reloc, /* special_function */
764 "R_MIPS_GNU_REL_HI16", /* name */
765 true, /* partial_inplace */
766 0xffff, /* src_mask */
767 0xffff, /* dst_mask */
768 true); /* pcrel_offset */
770 /* Low 16 bits of symbol value, pc-relative. */
771 static reloc_howto_type elf_mips_gnu_rel_lo16 =
772 HOWTO (R_MIPS_GNU_REL_LO16, /* type */
773 0, /* rightshift */
774 2, /* size (0 = byte, 1 = short, 2 = long) */
775 16, /* bitsize */
776 true, /* pc_relative */
777 0, /* bitpos */
778 complain_overflow_dont, /* complain_on_overflow */
779 mips_elf_lo16_reloc, /* special_function */
780 "R_MIPS_GNU_REL_LO16", /* name */
781 true, /* partial_inplace */
782 0xffff, /* src_mask */
783 0xffff, /* dst_mask */
784 true); /* pcrel_offset */
786 /* 16 bit offset for pc-relative branches. */
787 static reloc_howto_type elf_mips_gnu_rel16_s2 =
788 HOWTO (R_MIPS_GNU_REL16_S2, /* type */
789 2, /* rightshift */
790 2, /* size (0 = byte, 1 = short, 2 = long) */
791 16, /* bitsize */
792 true, /* pc_relative */
793 0, /* bitpos */
794 complain_overflow_signed, /* complain_on_overflow */
795 bfd_elf_generic_reloc, /* special_function */
796 "R_MIPS_GNU_REL16_S2", /* name */
797 true, /* partial_inplace */
798 0xffff, /* src_mask */
799 0xffff, /* dst_mask */
800 true); /* pcrel_offset */
802 /* 64 bit pc-relative. */
803 static reloc_howto_type elf_mips_gnu_pcrel64 =
804 HOWTO (R_MIPS_PC64, /* type */
805 0, /* rightshift */
806 4, /* size (0 = byte, 1 = short, 2 = long) */
807 64, /* bitsize */
808 true, /* pc_relative */
809 0, /* bitpos */
810 complain_overflow_signed, /* complain_on_overflow */
811 bfd_elf_generic_reloc, /* special_function */
812 "R_MIPS_PC64", /* name */
813 true, /* partial_inplace */
814 MINUS_ONE, /* src_mask */
815 MINUS_ONE, /* dst_mask */
816 true); /* pcrel_offset */
818 /* 32 bit pc-relative. */
819 static reloc_howto_type elf_mips_gnu_pcrel32 =
820 HOWTO (R_MIPS_PC32, /* type */
821 0, /* rightshift */
822 2, /* size (0 = byte, 1 = short, 2 = long) */
823 32, /* bitsize */
824 true, /* pc_relative */
825 0, /* bitpos */
826 complain_overflow_signed, /* complain_on_overflow */
827 bfd_elf_generic_reloc, /* special_function */
828 "R_MIPS_PC32", /* name */
829 true, /* partial_inplace */
830 0xffffffff, /* src_mask */
831 0xffffffff, /* dst_mask */
832 true); /* pcrel_offset */
834 /* GNU extension to record C++ vtable hierarchy */
835 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
836 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
837 0, /* rightshift */
838 2, /* size (0 = byte, 1 = short, 2 = long) */
839 0, /* bitsize */
840 false, /* pc_relative */
841 0, /* bitpos */
842 complain_overflow_dont, /* complain_on_overflow */
843 NULL, /* special_function */
844 "R_MIPS_GNU_VTINHERIT", /* name */
845 false, /* partial_inplace */
846 0, /* src_mask */
847 0, /* dst_mask */
848 false); /* pcrel_offset */
850 /* GNU extension to record C++ vtable member usage */
851 static reloc_howto_type elf_mips_gnu_vtentry_howto =
852 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
853 0, /* rightshift */
854 2, /* size (0 = byte, 1 = short, 2 = long) */
855 0, /* bitsize */
856 false, /* pc_relative */
857 0, /* bitpos */
858 complain_overflow_dont, /* complain_on_overflow */
859 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
860 "R_MIPS_GNU_VTENTRY", /* name */
861 false, /* partial_inplace */
862 0, /* src_mask */
863 0, /* dst_mask */
864 false); /* pcrel_offset */
866 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
867 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
868 the HI16. Here we just save the information we need; we do the
869 actual relocation when we see the LO16.
871 MIPS ELF requires that the LO16 immediately follow the HI16. As a
872 GNU extension, for non-pc-relative relocations, we permit an
873 arbitrary number of HI16 relocs to be associated with a single LO16
874 reloc. This extension permits gcc to output the HI and LO relocs
875 itself.
877 This cannot be done for PC-relative relocations because both the HI16
878 and LO16 parts of the relocations must be done relative to the LO16
879 part, and there can be carry to or borrow from the HI16 part. */
881 struct mips_hi16
883 struct mips_hi16 *next;
884 bfd_byte *addr;
885 bfd_vma addend;
888 /* FIXME: This should not be a static variable. */
890 static struct mips_hi16 *mips_hi16_list;
892 static bfd_reloc_status_type
893 mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data, input_section,
894 output_bfd, error_message)
895 bfd *abfd ATTRIBUTE_UNUSED;
896 arelent *reloc_entry;
897 asymbol *symbol;
898 PTR data;
899 asection *input_section;
900 bfd *output_bfd;
901 char **error_message;
903 bfd_reloc_status_type ret;
904 bfd_vma relocation;
905 struct mips_hi16 *n;
907 /* If we're relocating, and this an external symbol, we don't want
908 to change anything. */
909 if (output_bfd != (bfd *) NULL
910 && (symbol->flags & BSF_SECTION_SYM) == 0
911 && reloc_entry->addend == 0)
913 reloc_entry->address += input_section->output_offset;
914 return bfd_reloc_ok;
917 ret = bfd_reloc_ok;
919 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
921 boolean relocateable;
922 bfd_vma gp;
924 if (ret == bfd_reloc_undefined)
925 abort ();
927 if (output_bfd != NULL)
928 relocateable = true;
929 else
931 relocateable = false;
932 output_bfd = symbol->section->output_section->owner;
935 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
936 error_message, &gp);
937 if (ret != bfd_reloc_ok)
938 return ret;
940 relocation = gp - reloc_entry->address;
942 else
944 if (bfd_is_und_section (symbol->section)
945 && output_bfd == (bfd *) NULL)
946 ret = bfd_reloc_undefined;
948 if (bfd_is_com_section (symbol->section))
949 relocation = 0;
950 else
951 relocation = symbol->value;
954 relocation += symbol->section->output_section->vma;
955 relocation += symbol->section->output_offset;
956 relocation += reloc_entry->addend;
958 if (reloc_entry->address > input_section->_cooked_size)
959 return bfd_reloc_outofrange;
961 /* Save the information, and let LO16 do the actual relocation. */
962 n = (struct mips_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
963 if (n == NULL)
964 return bfd_reloc_outofrange;
965 n->addr = (bfd_byte *) data + reloc_entry->address;
966 n->addend = relocation;
967 n->next = mips_hi16_list;
968 mips_hi16_list = n;
970 if (output_bfd != (bfd *) NULL)
971 reloc_entry->address += input_section->output_offset;
973 return ret;
976 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
977 inplace relocation; this function exists in order to do the
978 R_MIPS_HI16 relocation described above. */
980 static bfd_reloc_status_type
981 mips_elf_lo16_reloc (abfd, reloc_entry, symbol, data, input_section,
982 output_bfd, error_message)
983 bfd *abfd;
984 arelent *reloc_entry;
985 asymbol *symbol;
986 PTR data;
987 asection *input_section;
988 bfd *output_bfd;
989 char **error_message;
991 arelent gp_disp_relent;
993 if (mips_hi16_list != NULL)
995 struct mips_hi16 *l;
997 l = mips_hi16_list;
998 while (l != NULL)
1000 unsigned long insn;
1001 unsigned long val;
1002 unsigned long vallo;
1003 struct mips_hi16 *next;
1005 /* Do the HI16 relocation. Note that we actually don't need
1006 to know anything about the LO16 itself, except where to
1007 find the low 16 bits of the addend needed by the LO16. */
1008 insn = bfd_get_32 (abfd, l->addr);
1009 vallo = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1011 /* The low order 16 bits are always treated as a signed
1012 value. */
1013 vallo = ((vallo & 0xffff) ^ 0x8000) - 0x8000;
1014 val = ((insn & 0xffff) << 16) + vallo;
1015 val += l->addend;
1017 /* If PC-relative, we need to subtract out the address of the LO
1018 half of the HI/LO. (The actual relocation is relative
1019 to that instruction.) */
1020 if (reloc_entry->howto->pc_relative)
1021 val -= reloc_entry->address;
1023 /* At this point, "val" has the value of the combined HI/LO
1024 pair. If the low order 16 bits (which will be used for
1025 the LO16 insn) are negative, then we will need an
1026 adjustment for the high order 16 bits. */
1027 val += 0x8000;
1028 val = (val >> 16) & 0xffff;
1030 insn &= ~ (bfd_vma) 0xffff;
1031 insn |= val;
1032 bfd_put_32 (abfd, (bfd_vma) insn, l->addr);
1034 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1036 gp_disp_relent = *reloc_entry;
1037 reloc_entry = &gp_disp_relent;
1038 reloc_entry->addend = l->addend;
1041 next = l->next;
1042 free (l);
1043 l = next;
1046 mips_hi16_list = NULL;
1048 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
1050 bfd_reloc_status_type ret;
1051 bfd_vma gp, relocation;
1053 /* FIXME: Does this case ever occur? */
1055 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
1056 if (ret != bfd_reloc_ok)
1057 return ret;
1059 relocation = gp - reloc_entry->address;
1060 relocation += symbol->section->output_section->vma;
1061 relocation += symbol->section->output_offset;
1062 relocation += reloc_entry->addend;
1064 if (reloc_entry->address > input_section->_cooked_size)
1065 return bfd_reloc_outofrange;
1067 gp_disp_relent = *reloc_entry;
1068 reloc_entry = &gp_disp_relent;
1069 reloc_entry->addend = relocation - 4;
1072 /* Now do the LO16 reloc in the usual way. */
1073 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1074 input_section, output_bfd, error_message);
1077 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1078 table used for PIC code. If the symbol is an external symbol, the
1079 instruction is modified to contain the offset of the appropriate
1080 entry in the global offset table. If the symbol is a section
1081 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1082 addends are combined to form the real addend against the section
1083 symbol; the GOT16 is modified to contain the offset of an entry in
1084 the global offset table, and the LO16 is modified to offset it
1085 appropriately. Thus an offset larger than 16 bits requires a
1086 modified value in the global offset table.
1088 This implementation suffices for the assembler, but the linker does
1089 not yet know how to create global offset tables. */
1091 static bfd_reloc_status_type
1092 mips_elf_got16_reloc (abfd, reloc_entry, symbol, data, input_section,
1093 output_bfd, error_message)
1094 bfd *abfd;
1095 arelent *reloc_entry;
1096 asymbol *symbol;
1097 PTR data;
1098 asection *input_section;
1099 bfd *output_bfd;
1100 char **error_message;
1102 /* If we're relocating, and this an external symbol, we don't want
1103 to change anything. */
1104 if (output_bfd != (bfd *) NULL
1105 && (symbol->flags & BSF_SECTION_SYM) == 0
1106 && reloc_entry->addend == 0)
1108 reloc_entry->address += input_section->output_offset;
1109 return bfd_reloc_ok;
1112 /* If we're relocating, and this is a local symbol, we can handle it
1113 just like HI16. */
1114 if (output_bfd != (bfd *) NULL
1115 && (symbol->flags & BSF_SECTION_SYM) != 0)
1116 return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1117 input_section, output_bfd, error_message);
1119 abort ();
1122 /* Set the GP value for OUTPUT_BFD. Returns false if this is a
1123 dangerous relocation. */
1125 static boolean
1126 mips_elf_assign_gp (output_bfd, pgp)
1127 bfd *output_bfd;
1128 bfd_vma *pgp;
1130 unsigned int count;
1131 asymbol **sym;
1132 unsigned int i;
1134 /* If we've already figured out what GP will be, just return it. */
1135 *pgp = _bfd_get_gp_value (output_bfd);
1136 if (*pgp)
1137 return true;
1139 count = bfd_get_symcount (output_bfd);
1140 sym = bfd_get_outsymbols (output_bfd);
1142 /* The linker script will have created a symbol named `_gp' with the
1143 appropriate value. */
1144 if (sym == (asymbol **) NULL)
1145 i = count;
1146 else
1148 for (i = 0; i < count; i++, sym++)
1150 register const char *name;
1152 name = bfd_asymbol_name (*sym);
1153 if (*name == '_' && strcmp (name, "_gp") == 0)
1155 *pgp = bfd_asymbol_value (*sym);
1156 _bfd_set_gp_value (output_bfd, *pgp);
1157 break;
1162 if (i >= count)
1164 /* Only get the error once. */
1165 *pgp = 4;
1166 _bfd_set_gp_value (output_bfd, *pgp);
1167 return false;
1170 return true;
1173 /* We have to figure out the gp value, so that we can adjust the
1174 symbol value correctly. We look up the symbol _gp in the output
1175 BFD. If we can't find it, we're stuck. We cache it in the ELF
1176 target data. We don't need to adjust the symbol value for an
1177 external symbol if we are producing relocateable output. */
1179 static bfd_reloc_status_type
1180 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1181 bfd *output_bfd;
1182 asymbol *symbol;
1183 boolean relocateable;
1184 char **error_message;
1185 bfd_vma *pgp;
1187 if (bfd_is_und_section (symbol->section)
1188 && ! relocateable)
1190 *pgp = 0;
1191 return bfd_reloc_undefined;
1194 *pgp = _bfd_get_gp_value (output_bfd);
1195 if (*pgp == 0
1196 && (! relocateable
1197 || (symbol->flags & BSF_SECTION_SYM) != 0))
1199 if (relocateable)
1201 /* Make up a value. */
1202 *pgp = symbol->section->output_section->vma + 0x4000;
1203 _bfd_set_gp_value (output_bfd, *pgp);
1205 else if (!mips_elf_assign_gp (output_bfd, pgp))
1207 *error_message =
1208 (char *) _("GP relative relocation when _gp not defined");
1209 return bfd_reloc_dangerous;
1213 return bfd_reloc_ok;
1216 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1217 become the offset from the gp register. This function also handles
1218 R_MIPS_LITERAL relocations, although those can be handled more
1219 cleverly because the entries in the .lit8 and .lit4 sections can be
1220 merged. */
1222 bfd_reloc_status_type
1223 _bfd_mips_elf32_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1224 output_bfd, error_message)
1225 bfd *abfd;
1226 arelent *reloc_entry;
1227 asymbol *symbol;
1228 PTR data;
1229 asection *input_section;
1230 bfd *output_bfd;
1231 char **error_message;
1233 boolean relocateable;
1234 bfd_reloc_status_type ret;
1235 bfd_vma gp;
1237 /* If we're relocating, and this is an external symbol with no
1238 addend, we don't want to change anything. We will only have an
1239 addend if this is a newly created reloc, not read from an ELF
1240 file. */
1241 if (output_bfd != (bfd *) NULL
1242 && (symbol->flags & BSF_SECTION_SYM) == 0
1243 && reloc_entry->addend == 0)
1245 reloc_entry->address += input_section->output_offset;
1246 return bfd_reloc_ok;
1249 if (output_bfd != (bfd *) NULL)
1250 relocateable = true;
1251 else
1253 relocateable = false;
1254 output_bfd = symbol->section->output_section->owner;
1257 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1258 &gp);
1259 if (ret != bfd_reloc_ok)
1260 return ret;
1262 return _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
1263 input_section, relocateable,
1264 data, gp);
1267 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1268 from the gp register? XXX */
1270 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1271 arelent *, asection *,
1272 boolean, PTR, bfd_vma));
1274 static bfd_reloc_status_type
1275 mips_elf_gprel32_reloc (abfd, reloc_entry, symbol, data, input_section,
1276 output_bfd, error_message)
1277 bfd *abfd;
1278 arelent *reloc_entry;
1279 asymbol *symbol;
1280 PTR data;
1281 asection *input_section;
1282 bfd *output_bfd;
1283 char **error_message;
1285 boolean relocateable;
1286 bfd_reloc_status_type ret;
1287 bfd_vma gp;
1289 /* If we're relocating, and this is an external symbol with no
1290 addend, we don't want to change anything. We will only have an
1291 addend if this is a newly created reloc, not read from an ELF
1292 file. */
1293 if (output_bfd != (bfd *) NULL
1294 && (symbol->flags & BSF_SECTION_SYM) == 0
1295 && reloc_entry->addend == 0)
1297 *error_message = (char *)
1298 _("32bits gp relative relocation occurs for an external symbol");
1299 return bfd_reloc_outofrange;
1302 if (output_bfd != (bfd *) NULL)
1304 relocateable = true;
1305 gp = _bfd_get_gp_value (output_bfd);
1307 else
1309 relocateable = false;
1310 output_bfd = symbol->section->output_section->owner;
1312 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1313 error_message, &gp);
1314 if (ret != bfd_reloc_ok)
1315 return ret;
1318 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1319 relocateable, data, gp);
1322 static bfd_reloc_status_type
1323 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1325 bfd *abfd;
1326 asymbol *symbol;
1327 arelent *reloc_entry;
1328 asection *input_section;
1329 boolean relocateable;
1330 PTR data;
1331 bfd_vma gp;
1333 bfd_vma relocation;
1334 unsigned long val;
1336 if (bfd_is_com_section (symbol->section))
1337 relocation = 0;
1338 else
1339 relocation = symbol->value;
1341 relocation += symbol->section->output_section->vma;
1342 relocation += symbol->section->output_offset;
1344 if (reloc_entry->address > input_section->_cooked_size)
1345 return bfd_reloc_outofrange;
1347 if (reloc_entry->howto->src_mask == 0)
1349 /* This case arises with the 64-bit MIPS ELF ABI. */
1350 val = 0;
1352 else
1353 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1355 /* Set val to the offset into the section or symbol. */
1356 val += reloc_entry->addend;
1358 /* Adjust val for the final section location and GP value. If we
1359 are producing relocateable output, we don't want to do this for
1360 an external symbol. */
1361 if (! relocateable
1362 || (symbol->flags & BSF_SECTION_SYM) != 0)
1363 val += relocation - gp;
1365 bfd_put_32 (abfd, (bfd_vma) val, (bfd_byte *) data + reloc_entry->address);
1367 if (relocateable)
1368 reloc_entry->address += input_section->output_offset;
1370 return bfd_reloc_ok;
1373 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1374 generated when addresses are 64 bits. The upper 32 bits are a simple
1375 sign extension. */
1377 static bfd_reloc_status_type
1378 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1379 output_bfd, error_message)
1380 bfd *abfd;
1381 arelent *reloc_entry;
1382 asymbol *symbol;
1383 PTR data;
1384 asection *input_section;
1385 bfd *output_bfd;
1386 char **error_message;
1388 bfd_reloc_status_type r;
1389 arelent reloc32;
1390 unsigned long val;
1391 bfd_size_type addr;
1393 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1394 input_section, output_bfd, error_message);
1395 if (r != bfd_reloc_continue)
1396 return r;
1398 /* Do a normal 32 bit relocation on the lower 32 bits. */
1399 reloc32 = *reloc_entry;
1400 if (bfd_big_endian (abfd))
1401 reloc32.address += 4;
1402 reloc32.howto = &elf_mips_howto_table_rel[R_MIPS_32];
1403 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1404 output_bfd, error_message);
1406 /* Sign extend into the upper 32 bits. */
1407 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1408 if ((val & 0x80000000) != 0)
1409 val = 0xffffffff;
1410 else
1411 val = 0;
1412 addr = reloc_entry->address;
1413 if (bfd_little_endian (abfd))
1414 addr += 4;
1415 bfd_put_32 (abfd, (bfd_vma) val, (bfd_byte *) data + addr);
1417 return r;
1420 /* Handle a mips16 jump. */
1422 static bfd_reloc_status_type
1423 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1424 output_bfd, error_message)
1425 bfd *abfd ATTRIBUTE_UNUSED;
1426 arelent *reloc_entry;
1427 asymbol *symbol;
1428 PTR data ATTRIBUTE_UNUSED;
1429 asection *input_section;
1430 bfd *output_bfd;
1431 char **error_message ATTRIBUTE_UNUSED;
1433 if (output_bfd != (bfd *) NULL
1434 && (symbol->flags & BSF_SECTION_SYM) == 0
1435 && reloc_entry->addend == 0)
1437 reloc_entry->address += input_section->output_offset;
1438 return bfd_reloc_ok;
1441 /* FIXME. */
1443 static boolean warned;
1445 if (! warned)
1446 (*_bfd_error_handler)
1447 (_("Linking mips16 objects into %s format is not supported"),
1448 bfd_get_target (input_section->output_section->owner));
1449 warned = true;
1452 return bfd_reloc_undefined;
1455 /* Handle a mips16 GP relative reloc. */
1457 static bfd_reloc_status_type
1458 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1459 output_bfd, error_message)
1460 bfd *abfd;
1461 arelent *reloc_entry;
1462 asymbol *symbol;
1463 PTR data;
1464 asection *input_section;
1465 bfd *output_bfd;
1466 char **error_message;
1468 boolean relocateable;
1469 bfd_reloc_status_type ret;
1470 bfd_vma gp;
1471 unsigned short extend, insn;
1472 unsigned long final;
1474 /* If we're relocating, and this is an external symbol with no
1475 addend, we don't want to change anything. We will only have an
1476 addend if this is a newly created reloc, not read from an ELF
1477 file. */
1478 if (output_bfd != NULL
1479 && (symbol->flags & BSF_SECTION_SYM) == 0
1480 && reloc_entry->addend == 0)
1482 reloc_entry->address += input_section->output_offset;
1483 return bfd_reloc_ok;
1486 if (output_bfd != NULL)
1487 relocateable = true;
1488 else
1490 relocateable = false;
1491 output_bfd = symbol->section->output_section->owner;
1494 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1495 &gp);
1496 if (ret != bfd_reloc_ok)
1497 return ret;
1499 if (reloc_entry->address > input_section->_cooked_size)
1500 return bfd_reloc_outofrange;
1502 /* Pick up the mips16 extend instruction and the real instruction. */
1503 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1504 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1506 /* Stuff the current addend back as a 32 bit value, do the usual
1507 relocation, and then clean up. */
1508 bfd_put_32 (abfd,
1509 (bfd_vma) (((extend & 0x1f) << 11)
1510 | (extend & 0x7e0)
1511 | (insn & 0x1f)),
1512 (bfd_byte *) data + reloc_entry->address);
1514 ret = _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
1515 input_section, relocateable, data, gp);
1517 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1518 bfd_put_16 (abfd,
1519 (bfd_vma) ((extend & 0xf800)
1520 | ((final >> 11) & 0x1f)
1521 | (final & 0x7e0)),
1522 (bfd_byte *) data + reloc_entry->address);
1523 bfd_put_16 (abfd,
1524 (bfd_vma) ((insn & 0xffe0)
1525 | (final & 0x1f)),
1526 (bfd_byte *) data + reloc_entry->address + 2);
1528 return ret;
1531 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1533 struct elf_reloc_map {
1534 bfd_reloc_code_real_type bfd_reloc_val;
1535 enum elf_mips_reloc_type elf_reloc_val;
1538 static const struct elf_reloc_map mips_reloc_map[] =
1540 { BFD_RELOC_NONE, R_MIPS_NONE, },
1541 { BFD_RELOC_16, R_MIPS_16 },
1542 { BFD_RELOC_32, R_MIPS_32 },
1543 { BFD_RELOC_64, R_MIPS_64 },
1544 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1545 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1546 { BFD_RELOC_LO16, R_MIPS_LO16 },
1547 { BFD_RELOC_GPREL16, R_MIPS_GPREL16 },
1548 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1549 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1550 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1551 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1552 { BFD_RELOC_GPREL32, R_MIPS_GPREL32 },
1553 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1554 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1555 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1556 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1557 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1558 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1559 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1560 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1563 /* Given a BFD reloc type, return a howto structure. */
1565 static reloc_howto_type *
1566 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1567 bfd *abfd;
1568 bfd_reloc_code_real_type code;
1570 unsigned int i;
1572 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1574 if (mips_reloc_map[i].bfd_reloc_val == code)
1575 return &elf_mips_howto_table_rel[(int) mips_reloc_map[i].elf_reloc_val];
1578 switch (code)
1580 default:
1581 bfd_set_error (bfd_error_bad_value);
1582 return NULL;
1584 case BFD_RELOC_CTOR:
1585 /* We need to handle BFD_RELOC_CTOR specially.
1586 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1587 size of addresses on this architecture. */
1588 if (bfd_arch_bits_per_address (abfd) == 32)
1589 return &elf_mips_howto_table_rel[(int) R_MIPS_32];
1590 else
1591 return &elf_mips_ctor64_howto;
1593 case BFD_RELOC_MIPS16_JMP:
1594 return &elf_mips16_jump_howto;
1595 case BFD_RELOC_MIPS16_GPREL:
1596 return &elf_mips16_gprel_howto;
1597 case BFD_RELOC_VTABLE_INHERIT:
1598 return &elf_mips_gnu_vtinherit_howto;
1599 case BFD_RELOC_VTABLE_ENTRY:
1600 return &elf_mips_gnu_vtentry_howto;
1601 case BFD_RELOC_PCREL_HI16_S:
1602 return &elf_mips_gnu_rel_hi16;
1603 case BFD_RELOC_PCREL_LO16:
1604 return &elf_mips_gnu_rel_lo16;
1605 case BFD_RELOC_16_PCREL_S2:
1606 return &elf_mips_gnu_rel16_s2;
1607 case BFD_RELOC_64_PCREL:
1608 return &elf_mips_gnu_pcrel64;
1609 case BFD_RELOC_32_PCREL:
1610 return &elf_mips_gnu_pcrel32;
1614 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1616 static reloc_howto_type *
1617 mips_elf32_rtype_to_howto (r_type, rela_p)
1618 unsigned int r_type;
1619 boolean rela_p ATTRIBUTE_UNUSED;
1621 switch (r_type)
1623 case R_MIPS16_26:
1624 return &elf_mips16_jump_howto;
1625 break;
1626 case R_MIPS16_GPREL:
1627 return &elf_mips16_gprel_howto;
1628 break;
1629 case R_MIPS_GNU_VTINHERIT:
1630 return &elf_mips_gnu_vtinherit_howto;
1631 break;
1632 case R_MIPS_GNU_VTENTRY:
1633 return &elf_mips_gnu_vtentry_howto;
1634 break;
1635 case R_MIPS_GNU_REL_HI16:
1636 return &elf_mips_gnu_rel_hi16;
1637 break;
1638 case R_MIPS_GNU_REL_LO16:
1639 return &elf_mips_gnu_rel_lo16;
1640 break;
1641 case R_MIPS_GNU_REL16_S2:
1642 return &elf_mips_gnu_rel16_s2;
1643 break;
1644 case R_MIPS_PC64:
1645 return &elf_mips_gnu_pcrel64;
1646 break;
1647 case R_MIPS_PC32:
1648 return &elf_mips_gnu_pcrel32;
1649 break;
1651 default:
1652 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1653 return &elf_mips_howto_table_rel[r_type];
1657 /* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
1659 static void
1660 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1661 bfd *abfd;
1662 arelent *cache_ptr;
1663 Elf32_Internal_Rel *dst;
1665 unsigned int r_type;
1667 r_type = ELF32_R_TYPE (dst->r_info);
1668 cache_ptr->howto = mips_elf32_rtype_to_howto (r_type, false);
1670 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1671 value for the object file. We get the addend now, rather than
1672 when we do the relocation, because the symbol manipulations done
1673 by the linker may cause us to lose track of the input BFD. */
1674 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1675 && (r_type == (unsigned int) R_MIPS_GPREL16
1676 || r_type == (unsigned int) R_MIPS_LITERAL))
1677 cache_ptr->addend = elf_gp (abfd);
1680 /* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
1682 static void
1683 mips_info_to_howto_rela (abfd, cache_ptr, dst)
1684 bfd *abfd;
1685 arelent *cache_ptr;
1686 Elf32_Internal_Rela *dst;
1688 /* Since an Elf32_Internal_Rel is an initial prefix of an
1689 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1690 above. */
1691 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
1693 /* If we ever need to do any extra processing with dst->r_addend
1694 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1697 /* Determine whether a symbol is global for the purposes of splitting
1698 the symbol table into global symbols and local symbols. At least
1699 on Irix 5, this split must be between section symbols and all other
1700 symbols. On most ELF targets the split is between static symbols
1701 and externally visible symbols. */
1703 static boolean
1704 mips_elf_sym_is_global (abfd, sym)
1705 bfd *abfd ATTRIBUTE_UNUSED;
1706 asymbol *sym;
1708 if (SGI_COMPAT (abfd))
1709 return (sym->flags & BSF_SECTION_SYM) == 0;
1710 else
1711 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1712 || bfd_is_und_section (bfd_get_section (sym))
1713 || bfd_is_com_section (bfd_get_section (sym)));
1716 /* Set the right machine number for a MIPS ELF file. */
1718 static boolean
1719 mips_elf32_object_p (abfd)
1720 bfd *abfd;
1722 unsigned long mach;
1724 /* Irix 5 and 6 are broken. Object file symbol tables are not always
1725 sorted correctly such that local symbols precede global symbols,
1726 and the sh_info field in the symbol table is not always right. */
1727 if (SGI_COMPAT (abfd))
1728 elf_bad_symtab (abfd) = true;
1730 mach = _bfd_elf_mips_mach (elf_elfheader (abfd)->e_flags);
1731 bfd_default_set_arch_mach (abfd, bfd_arch_mips, mach);
1733 if (ABI_N32_P (abfd))
1734 return false;
1736 return true;
1739 /* MIPS ELF local labels start with '$', not 'L'. */
1741 static boolean
1742 mips_elf_is_local_label_name (abfd, name)
1743 bfd *abfd;
1744 const char *name;
1746 if (name[0] == '$')
1747 return true;
1749 /* On Irix 6, the labels go back to starting with '.', so we accept
1750 the generic ELF local label syntax as well. */
1751 return _bfd_elf_is_local_label_name (abfd, name);
1754 /* Support for core dump NOTE sections. */
1755 static boolean
1756 elf32_mips_grok_prstatus (abfd, note)
1757 bfd *abfd;
1758 Elf_Internal_Note *note;
1760 int offset;
1761 unsigned int raw_size;
1763 switch (note->descsz)
1765 default:
1766 return false;
1768 case 256: /* Linux/MIPS */
1769 /* pr_cursig */
1770 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1772 /* pr_pid */
1773 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1775 /* pr_reg */
1776 offset = 72;
1777 raw_size = 180;
1779 break;
1782 /* Make a ".reg/999" section. */
1783 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1784 raw_size, note->descpos + offset);
1787 static boolean
1788 elf32_mips_grok_psinfo (abfd, note)
1789 bfd *abfd;
1790 Elf_Internal_Note *note;
1792 switch (note->descsz)
1794 default:
1795 return false;
1797 case 128: /* Linux/MIPS elf_prpsinfo */
1798 elf_tdata (abfd)->core_program
1799 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1800 elf_tdata (abfd)->core_command
1801 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1804 /* Note that for some reason, a spurious space is tacked
1805 onto the end of the args in some (at least one anyway)
1806 implementations, so strip it off if it exists. */
1809 char *command = elf_tdata (abfd)->core_command;
1810 int n = strlen (command);
1812 if (0 < n && command[n - 1] == ' ')
1813 command[n - 1] = '\0';
1816 return true;
1819 #define PDR_SIZE 32
1821 static boolean
1822 elf32_mips_discard_info (abfd, cookie, info)
1823 bfd *abfd;
1824 struct elf_reloc_cookie *cookie;
1825 struct bfd_link_info *info;
1827 asection *o;
1828 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1829 boolean ret = false;
1830 unsigned char *tdata;
1831 size_t i, skip;
1833 o = bfd_get_section_by_name (abfd, ".pdr");
1834 if (! o)
1835 return false;
1836 if (o->_raw_size == 0)
1837 return false;
1838 if (o->_raw_size % PDR_SIZE != 0)
1839 return false;
1840 if (o->output_section != NULL
1841 && bfd_is_abs_section (o->output_section))
1842 return false;
1844 tdata = bfd_zmalloc (o->_raw_size / PDR_SIZE);
1845 if (! tdata)
1846 return false;
1848 cookie->rels = _bfd_elf32_link_read_relocs (abfd, o, (PTR) NULL,
1849 (Elf_Internal_Rela *) NULL,
1850 info->keep_memory);
1851 if (!cookie->rels)
1853 free (tdata);
1854 return false;
1857 cookie->rel = cookie->rels;
1858 cookie->relend =
1859 cookie->rels + o->reloc_count * bed->s->int_rels_per_ext_rel;
1861 for (i = 0, skip = 0; i < o->_raw_size; i ++)
1863 if (_bfd_elf32_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
1865 tdata[i] = 1;
1866 skip ++;
1870 if (skip != 0)
1872 elf_section_data (o)->tdata = tdata;
1873 o->_cooked_size = o->_raw_size - skip * PDR_SIZE;
1874 ret = true;
1876 else
1877 free (tdata);
1879 if (! info->keep_memory)
1880 free (cookie->rels);
1882 return ret;
1885 static boolean
1886 elf32_mips_ignore_discarded_relocs (sec)
1887 asection *sec;
1889 if (strcmp (sec->name, ".pdr") == 0)
1890 return true;
1891 return false;
1894 static boolean
1895 elf32_mips_write_section (output_bfd, sec, contents)
1896 bfd *output_bfd;
1897 asection *sec;
1898 bfd_byte *contents;
1900 bfd_byte *to, *from, *end;
1901 int i;
1903 if (strcmp (sec->name, ".pdr") != 0)
1904 return false;
1906 if (elf_section_data (sec)->tdata == NULL)
1907 return false;
1909 to = contents;
1910 end = contents + sec->_raw_size;
1911 for (from = contents, i = 0;
1912 from < end;
1913 from += PDR_SIZE, i++)
1915 if (((unsigned char *)elf_section_data (sec)->tdata)[i] == 1)
1916 continue;
1917 if (to != from)
1918 memcpy (to, from, PDR_SIZE);
1919 to += PDR_SIZE;
1921 bfd_set_section_contents (output_bfd, sec->output_section, contents,
1922 (file_ptr) sec->output_offset,
1923 sec->_cooked_size);
1924 return true;
1927 /* Depending on the target vector we generate some version of Irix
1928 executables or "normal" MIPS ELF ABI executables. */
1929 static irix_compat_t
1930 elf32_mips_irix_compat (abfd)
1931 bfd *abfd;
1933 if ((abfd->xvec == &bfd_elf32_tradbigmips_vec)
1934 || (abfd->xvec == &bfd_elf32_tradlittlemips_vec))
1935 return ict_none;
1936 else
1937 return ict_irix5;
1940 /* Given a data section and an in-memory embedded reloc section, store
1941 relocation information into the embedded reloc section which can be
1942 used at runtime to relocate the data section. This is called by the
1943 linker when the --embedded-relocs switch is used. This is called
1944 after the add_symbols entry point has been called for all the
1945 objects, and before the final_link entry point is called. */
1947 boolean
1948 bfd_mips_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
1949 bfd *abfd;
1950 struct bfd_link_info *info;
1951 asection *datasec;
1952 asection *relsec;
1953 char **errmsg;
1955 Elf_Internal_Shdr *symtab_hdr;
1956 Elf_Internal_Shdr *shndx_hdr;
1957 Elf32_External_Sym *extsyms;
1958 Elf32_External_Sym *free_extsyms = NULL;
1959 Elf_External_Sym_Shndx *shndx_buf = NULL;
1960 Elf_Internal_Rela *internal_relocs;
1961 Elf_Internal_Rela *free_relocs = NULL;
1962 Elf_Internal_Rela *irel, *irelend;
1963 bfd_byte *p;
1964 bfd_size_type amt;
1966 BFD_ASSERT (! info->relocateable);
1968 *errmsg = NULL;
1970 if (datasec->reloc_count == 0)
1971 return true;
1973 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1974 /* Read this BFD's symbols if we haven't done so already, or get the cached
1975 copy if it exists. */
1976 if (symtab_hdr->contents != NULL)
1977 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1978 else
1980 /* Go get them off disk. */
1981 if (info->keep_memory)
1982 extsyms = ((Elf32_External_Sym *)
1983 bfd_alloc (abfd, symtab_hdr->sh_size));
1984 else
1985 extsyms = ((Elf32_External_Sym *)
1986 bfd_malloc (symtab_hdr->sh_size));
1987 if (extsyms == NULL)
1988 goto error_return;
1989 if (! info->keep_memory)
1990 free_extsyms = extsyms;
1991 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1992 || (bfd_bread (extsyms, symtab_hdr->sh_size, abfd)
1993 != symtab_hdr->sh_size))
1994 goto error_return;
1995 if (info->keep_memory)
1996 symtab_hdr->contents = (unsigned char *) extsyms;
1999 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2000 if (shndx_hdr->sh_size != 0)
2002 amt = symtab_hdr->sh_info * sizeof (Elf_External_Sym_Shndx);
2003 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2004 if (shndx_buf == NULL)
2005 goto error_return;
2006 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2007 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
2008 goto error_return;
2011 /* Get a copy of the native relocations. */
2012 internal_relocs = (_bfd_elf32_link_read_relocs
2013 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2014 info->keep_memory));
2015 if (internal_relocs == NULL)
2016 goto error_return;
2017 if (! info->keep_memory)
2018 free_relocs = internal_relocs;
2020 relsec->contents = (bfd_byte *) bfd_alloc (abfd, datasec->reloc_count * 12);
2021 if (relsec->contents == NULL)
2022 goto error_return;
2024 p = relsec->contents;
2026 irelend = internal_relocs + datasec->reloc_count;
2028 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2030 asection *targetsec;
2032 /* We are going to write a four byte longword into the runtime
2033 reloc section. The longword will be the address in the data
2034 section which must be relocated. It is followed by the name
2035 of the target section NUL-padded or truncated to 8
2036 characters. */
2038 /* We can only relocate absolute longword relocs at run time. */
2039 if ((ELF32_R_TYPE (irel->r_info) != (int) R_MIPS_32) &&
2040 (ELF32_R_TYPE (irel->r_info) != (int) R_MIPS_64))
2042 *errmsg = _("unsupported reloc type");
2043 bfd_set_error (bfd_error_bad_value);
2044 goto error_return;
2046 /* Get the target section referred to by the reloc. */
2047 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2049 Elf32_External_Sym *esym;
2050 Elf_External_Sym_Shndx *shndx;
2051 Elf_Internal_Sym isym;
2053 /* A local symbol. */
2054 esym = extsyms + ELF32_R_SYM (irel->r_info);
2055 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (irel->r_info) : 0);
2056 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
2058 targetsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
2060 else
2062 unsigned long indx;
2063 struct elf_link_hash_entry *h;
2065 /* An external symbol. */
2066 indx = ELF32_R_SYM (irel->r_info);
2067 h = elf_sym_hashes (abfd)[indx];
2068 targetsec = NULL;
2070 For some reason, in certain programs, the symbol will
2071 not be in the hash table. It seems to happen when you
2072 declare a static table of pointers to const external structures.
2073 In this case, the relocs are relative to data, not
2074 text, so just treating it like an undefined link
2075 should be sufficient. */
2076 BFD_ASSERT(h != NULL);
2077 if (h->root.type == bfd_link_hash_defined
2078 || h->root.type == bfd_link_hash_defweak)
2079 targetsec = h->root.u.def.section;
2084 Set the low bit of the relocation offset if it's a MIPS64 reloc.
2085 Relocations will always be on (at least) 32-bit boundaries. */
2087 bfd_put_32 (abfd, ((irel->r_offset + datasec->output_offset) +
2088 ((ELF32_R_TYPE (irel->r_info) == (int) R_MIPS_64) ? 1 : 0)),
2090 memset (p + 4, 0, 8);
2091 if (targetsec != NULL)
2092 strncpy (p + 4, targetsec->output_section->name, 8);
2095 if (shndx_buf != NULL)
2096 free (shndx_buf);
2097 if (free_extsyms != NULL)
2098 free (free_extsyms);
2099 if (free_relocs != NULL)
2100 free (free_relocs);
2101 return true;
2103 error_return:
2104 if (shndx_buf != NULL)
2105 free (shndx_buf);
2106 if (free_extsyms != NULL)
2107 free (free_extsyms);
2108 if (free_relocs != NULL)
2109 free (free_relocs);
2110 return false;
2113 /* ECOFF swapping routines. These are used when dealing with the
2114 .mdebug section, which is in the ECOFF debugging format. */
2115 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
2116 /* Symbol table magic number. */
2117 magicSym,
2118 /* Alignment of debugging information. E.g., 4. */
2120 /* Sizes of external symbolic information. */
2121 sizeof (struct hdr_ext),
2122 sizeof (struct dnr_ext),
2123 sizeof (struct pdr_ext),
2124 sizeof (struct sym_ext),
2125 sizeof (struct opt_ext),
2126 sizeof (struct fdr_ext),
2127 sizeof (struct rfd_ext),
2128 sizeof (struct ext_ext),
2129 /* Functions to swap in external symbolic data. */
2130 ecoff_swap_hdr_in,
2131 ecoff_swap_dnr_in,
2132 ecoff_swap_pdr_in,
2133 ecoff_swap_sym_in,
2134 ecoff_swap_opt_in,
2135 ecoff_swap_fdr_in,
2136 ecoff_swap_rfd_in,
2137 ecoff_swap_ext_in,
2138 _bfd_ecoff_swap_tir_in,
2139 _bfd_ecoff_swap_rndx_in,
2140 /* Functions to swap out external symbolic data. */
2141 ecoff_swap_hdr_out,
2142 ecoff_swap_dnr_out,
2143 ecoff_swap_pdr_out,
2144 ecoff_swap_sym_out,
2145 ecoff_swap_opt_out,
2146 ecoff_swap_fdr_out,
2147 ecoff_swap_rfd_out,
2148 ecoff_swap_ext_out,
2149 _bfd_ecoff_swap_tir_out,
2150 _bfd_ecoff_swap_rndx_out,
2151 /* Function to read in symbolic data. */
2152 _bfd_mips_elf_read_ecoff_info
2155 #define ELF_ARCH bfd_arch_mips
2156 #define ELF_MACHINE_CODE EM_MIPS
2158 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
2159 a value of 0x1000, and we are compatible. */
2160 #define ELF_MAXPAGESIZE 0x1000
2162 #define elf_backend_collect true
2163 #define elf_backend_type_change_ok true
2164 #define elf_backend_can_gc_sections true
2165 #define elf_info_to_howto mips_info_to_howto_rela
2166 #define elf_info_to_howto_rel mips_info_to_howto_rel
2167 #define elf_backend_sym_is_global mips_elf_sym_is_global
2168 #define elf_backend_object_p mips_elf32_object_p
2169 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
2170 #define elf_backend_section_processing _bfd_mips_elf_section_processing
2171 #define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
2172 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
2173 #define elf_backend_section_from_bfd_section \
2174 _bfd_mips_elf_section_from_bfd_section
2175 #define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
2176 #define elf_backend_link_output_symbol_hook \
2177 _bfd_mips_elf_link_output_symbol_hook
2178 #define elf_backend_create_dynamic_sections \
2179 _bfd_mips_elf_create_dynamic_sections
2180 #define elf_backend_check_relocs _bfd_mips_elf_check_relocs
2181 #define elf_backend_adjust_dynamic_symbol \
2182 _bfd_mips_elf_adjust_dynamic_symbol
2183 #define elf_backend_always_size_sections \
2184 _bfd_mips_elf_always_size_sections
2185 #define elf_backend_size_dynamic_sections \
2186 _bfd_mips_elf_size_dynamic_sections
2187 #define elf_backend_relocate_section _bfd_mips_elf_relocate_section
2188 #define elf_backend_finish_dynamic_symbol \
2189 _bfd_mips_elf_finish_dynamic_symbol
2190 #define elf_backend_finish_dynamic_sections \
2191 _bfd_mips_elf_finish_dynamic_sections
2192 #define elf_backend_final_write_processing \
2193 _bfd_mips_elf_final_write_processing
2194 #define elf_backend_additional_program_headers \
2195 _bfd_mips_elf_additional_program_headers
2196 #define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
2197 #define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
2198 #define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
2199 #define elf_backend_copy_indirect_symbol \
2200 _bfd_mips_elf_copy_indirect_symbol
2201 #define elf_backend_hide_symbol _bfd_mips_elf_hide_symbol
2202 #define elf_backend_grok_prstatus elf32_mips_grok_prstatus
2203 #define elf_backend_grok_psinfo elf32_mips_grok_psinfo
2204 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
2206 #define elf_backend_got_header_size (4 * MIPS_RESERVED_GOTNO)
2207 #define elf_backend_plt_header_size 0
2208 #define elf_backend_may_use_rel_p 1
2209 #define elf_backend_may_use_rela_p 0
2210 #define elf_backend_default_use_rela_p 0
2211 #define elf_backend_sign_extend_vma true
2213 #define elf_backend_discard_info elf32_mips_discard_info
2214 #define elf_backend_ignore_discarded_relocs \
2215 elf32_mips_ignore_discarded_relocs
2216 #define elf_backend_write_section elf32_mips_write_section
2217 #define elf_backend_mips_irix_compat elf32_mips_irix_compat
2218 #define elf_backend_mips_rtype_to_howto mips_elf32_rtype_to_howto
2219 #define bfd_elf32_bfd_is_local_label_name \
2220 mips_elf_is_local_label_name
2221 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
2222 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
2223 #define bfd_elf32_bfd_get_relocated_section_contents \
2224 _bfd_elf_mips_get_relocated_section_contents
2225 #define bfd_elf32_bfd_link_hash_table_create \
2226 _bfd_mips_elf_link_hash_table_create
2227 #define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
2228 #define bfd_elf32_bfd_merge_private_bfd_data \
2229 _bfd_mips_elf_merge_private_bfd_data
2230 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
2231 #define bfd_elf32_bfd_print_private_bfd_data \
2232 _bfd_mips_elf_print_private_bfd_data
2234 /* Support for SGI-ish mips targets. */
2235 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
2236 #define TARGET_LITTLE_NAME "elf32-littlemips"
2237 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
2238 #define TARGET_BIG_NAME "elf32-bigmips"
2240 #include "elf32-target.h"
2242 /* Support for traditional mips targets. */
2243 #define INCLUDED_TARGET_FILE /* More a type of flag. */
2245 #undef TARGET_LITTLE_SYM
2246 #undef TARGET_LITTLE_NAME
2247 #undef TARGET_BIG_SYM
2248 #undef TARGET_BIG_NAME
2250 #define TARGET_LITTLE_SYM bfd_elf32_tradlittlemips_vec
2251 #define TARGET_LITTLE_NAME "elf32-tradlittlemips"
2252 #define TARGET_BIG_SYM bfd_elf32_tradbigmips_vec
2253 #define TARGET_BIG_NAME "elf32-tradbigmips"
2255 /* Include the target file again for this target. */
2256 #include "elf32-target.h"