1 /* BFD back-end for MIPS Extended-Coff files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
5 Original version by Per Bothner.
6 Full support added by Ian Lance Taylor, ian@cygnus.com.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "coff/internal.h"
30 #include "coff/symconst.h"
31 #include "coff/ecoff.h"
32 #include "coff/mips.h"
36 /* Prototypes for static functions. */
38 static boolean mips_ecoff_bad_format_hook
PARAMS ((bfd
*abfd
, PTR filehdr
));
39 static void mips_ecoff_swap_reloc_in
PARAMS ((bfd
*, PTR
,
40 struct internal_reloc
*));
41 static void mips_ecoff_swap_reloc_out
PARAMS ((bfd
*,
42 const struct internal_reloc
*,
44 static void mips_adjust_reloc_in
PARAMS ((bfd
*,
45 const struct internal_reloc
*,
47 static void mips_adjust_reloc_out
PARAMS ((bfd
*, const arelent
*,
48 struct internal_reloc
*));
49 static bfd_reloc_status_type mips_generic_reloc
PARAMS ((bfd
*abfd
,
56 static bfd_reloc_status_type mips_refhi_reloc
PARAMS ((bfd
*abfd
,
63 static bfd_reloc_status_type mips_reflo_reloc
PARAMS ((bfd
*abfd
,
70 static bfd_reloc_status_type mips_gprel_reloc
PARAMS ((bfd
*abfd
,
77 static bfd_reloc_status_type mips_relhi_reloc
PARAMS ((bfd
*abfd
,
84 static bfd_reloc_status_type mips_rello_reloc
PARAMS ((bfd
*abfd
,
91 static bfd_reloc_status_type mips_switch_reloc
PARAMS ((bfd
*abfd
,
98 static void mips_relocate_hi
PARAMS ((struct internal_reloc
*refhi
,
99 struct internal_reloc
*reflo
,
101 asection
*input_section
,
106 static boolean mips_relocate_section
PARAMS ((bfd
*, struct bfd_link_info
*,
109 static boolean mips_read_relocs
PARAMS ((bfd
*, asection
*));
110 static boolean mips_relax_section
PARAMS ((bfd
*, asection
*,
111 struct bfd_link_info
*,
113 static boolean mips_relax_pcrel16
PARAMS ((struct bfd_link_info
*, bfd
*,
115 struct ecoff_link_hash_entry
*,
116 bfd_byte
*, bfd_vma
));
117 static reloc_howto_type
*mips_bfd_reloc_type_lookup
118 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
120 /* ECOFF has COFF sections, but the debugging information is stored in
121 a completely different format. ECOFF targets use some of the
122 swapping routines from coffswap.h, and some of the generic COFF
123 routines in coffgen.c, but, unlike the real COFF targets, do not
124 use coffcode.h itself.
126 Get the generic COFF swapping routines, except for the reloc,
127 symbol, and lineno ones. Give them ECOFF names. */
129 #define NO_COFF_RELOCS
130 #define NO_COFF_SYMBOLS
131 #define NO_COFF_LINENOS
132 #define coff_swap_filehdr_in mips_ecoff_swap_filehdr_in
133 #define coff_swap_filehdr_out mips_ecoff_swap_filehdr_out
134 #define coff_swap_aouthdr_in mips_ecoff_swap_aouthdr_in
135 #define coff_swap_aouthdr_out mips_ecoff_swap_aouthdr_out
136 #define coff_swap_scnhdr_in mips_ecoff_swap_scnhdr_in
137 #define coff_swap_scnhdr_out mips_ecoff_swap_scnhdr_out
138 #include "coffswap.h"
140 /* Get the ECOFF swapping routines. */
142 #include "ecoffswap.h"
144 /* How to process the various relocs types. */
146 static reloc_howto_type mips_howto_table
[] =
148 /* Reloc type 0 is ignored. The reloc reading code ensures that
149 this is a reference to the .abs section, which will cause
150 bfd_perform_relocation to do nothing. */
151 HOWTO (MIPS_R_IGNORE
, /* type */
153 0, /* size (0 = byte, 1 = short, 2 = long) */
155 false, /* pc_relative */
157 complain_overflow_dont
, /* complain_on_overflow */
158 0, /* special_function */
160 false, /* partial_inplace */
163 false), /* pcrel_offset */
165 /* A 16 bit reference to a symbol, normally from a data section. */
166 HOWTO (MIPS_R_REFHALF
, /* type */
168 1, /* size (0 = byte, 1 = short, 2 = long) */
170 false, /* pc_relative */
172 complain_overflow_bitfield
, /* complain_on_overflow */
173 mips_generic_reloc
, /* special_function */
174 "REFHALF", /* name */
175 true, /* partial_inplace */
176 0xffff, /* src_mask */
177 0xffff, /* dst_mask */
178 false), /* pcrel_offset */
180 /* A 32 bit reference to a symbol, normally from a data section. */
181 HOWTO (MIPS_R_REFWORD
, /* type */
183 2, /* size (0 = byte, 1 = short, 2 = long) */
185 false, /* pc_relative */
187 complain_overflow_bitfield
, /* complain_on_overflow */
188 mips_generic_reloc
, /* special_function */
189 "REFWORD", /* name */
190 true, /* partial_inplace */
191 0xffffffff, /* src_mask */
192 0xffffffff, /* dst_mask */
193 false), /* pcrel_offset */
195 /* A 26 bit absolute jump address. */
196 HOWTO (MIPS_R_JMPADDR
, /* type */
198 2, /* size (0 = byte, 1 = short, 2 = long) */
200 false, /* pc_relative */
202 complain_overflow_dont
, /* complain_on_overflow */
203 /* This needs complex overflow
204 detection, because the upper four
205 bits must match the PC. */
206 mips_generic_reloc
, /* special_function */
207 "JMPADDR", /* name */
208 true, /* partial_inplace */
209 0x3ffffff, /* src_mask */
210 0x3ffffff, /* dst_mask */
211 false), /* pcrel_offset */
213 /* The high 16 bits of a symbol value. Handled by the function
215 HOWTO (MIPS_R_REFHI
, /* type */
217 2, /* size (0 = byte, 1 = short, 2 = long) */
219 false, /* pc_relative */
221 complain_overflow_bitfield
, /* complain_on_overflow */
222 mips_refhi_reloc
, /* special_function */
224 true, /* partial_inplace */
225 0xffff, /* src_mask */
226 0xffff, /* dst_mask */
227 false), /* pcrel_offset */
229 /* The low 16 bits of a symbol value. */
230 HOWTO (MIPS_R_REFLO
, /* type */
232 2, /* size (0 = byte, 1 = short, 2 = long) */
234 false, /* pc_relative */
236 complain_overflow_dont
, /* complain_on_overflow */
237 mips_reflo_reloc
, /* special_function */
239 true, /* partial_inplace */
240 0xffff, /* src_mask */
241 0xffff, /* dst_mask */
242 false), /* pcrel_offset */
244 /* A reference to an offset from the gp register. Handled by the
245 function mips_gprel_reloc. */
246 HOWTO (MIPS_R_GPREL
, /* type */
248 2, /* size (0 = byte, 1 = short, 2 = long) */
250 false, /* pc_relative */
252 complain_overflow_signed
, /* complain_on_overflow */
253 mips_gprel_reloc
, /* special_function */
255 true, /* partial_inplace */
256 0xffff, /* src_mask */
257 0xffff, /* dst_mask */
258 false), /* pcrel_offset */
260 /* A reference to a literal using an offset from the gp register.
261 Handled by the function mips_gprel_reloc. */
262 HOWTO (MIPS_R_LITERAL
, /* type */
264 2, /* size (0 = byte, 1 = short, 2 = long) */
266 false, /* pc_relative */
268 complain_overflow_signed
, /* complain_on_overflow */
269 mips_gprel_reloc
, /* special_function */
270 "LITERAL", /* name */
271 true, /* partial_inplace */
272 0xffff, /* src_mask */
273 0xffff, /* dst_mask */
274 false), /* pcrel_offset */
281 /* This reloc is a Cygnus extension used when generating position
282 independent code for embedded systems. It represents a 16 bit PC
283 relative reloc rightshifted twice as used in the MIPS branch
285 HOWTO (MIPS_R_PCREL16
, /* type */
287 2, /* size (0 = byte, 1 = short, 2 = long) */
289 true, /* pc_relative */
291 complain_overflow_signed
, /* complain_on_overflow */
292 mips_generic_reloc
, /* special_function */
293 "PCREL16", /* name */
294 true, /* partial_inplace */
295 0xffff, /* src_mask */
296 0xffff, /* dst_mask */
297 true), /* pcrel_offset */
299 /* This reloc is a Cygnus extension used when generating position
300 independent code for embedded systems. It represents the high 16
301 bits of a PC relative reloc. The next reloc must be
302 MIPS_R_RELLO, and the addend is formed from the addends of the
303 two instructions, just as in MIPS_R_REFHI and MIPS_R_REFLO. The
304 final value is actually PC relative to the location of the
305 MIPS_R_RELLO reloc, not the MIPS_R_RELHI reloc. */
306 HOWTO (MIPS_R_RELHI
, /* type */
308 2, /* size (0 = byte, 1 = short, 2 = long) */
310 true, /* pc_relative */
312 complain_overflow_bitfield
, /* complain_on_overflow */
313 mips_relhi_reloc
, /* special_function */
315 true, /* partial_inplace */
316 0xffff, /* src_mask */
317 0xffff, /* dst_mask */
318 true), /* pcrel_offset */
320 /* This reloc is a Cygnus extension used when generating position
321 independent code for embedded systems. It represents the low 16
322 bits of a PC relative reloc. */
323 HOWTO (MIPS_R_RELLO
, /* type */
325 2, /* size (0 = byte, 1 = short, 2 = long) */
327 true, /* pc_relative */
329 complain_overflow_dont
, /* complain_on_overflow */
330 mips_rello_reloc
, /* special_function */
332 true, /* partial_inplace */
333 0xffff, /* src_mask */
334 0xffff, /* dst_mask */
335 true), /* pcrel_offset */
345 /* This reloc is a Cygnus extension used when generating position
346 independent code for embedded systems. It represents an entry in
347 a switch table, which is the difference between two symbols in
348 the .text section. The symndx is actually the offset from the
349 reloc address to the subtrahend. See include/coff/mips.h for
351 HOWTO (MIPS_R_SWITCH
, /* type */
353 2, /* size (0 = byte, 1 = short, 2 = long) */
355 true, /* pc_relative */
357 complain_overflow_dont
, /* complain_on_overflow */
358 mips_switch_reloc
, /* special_function */
360 true, /* partial_inplace */
361 0xffffffff, /* src_mask */
362 0xffffffff, /* dst_mask */
363 true) /* pcrel_offset */
366 #define MIPS_HOWTO_COUNT \
367 (sizeof mips_howto_table / sizeof mips_howto_table[0])
369 /* When the linker is doing relaxing, it may change an external PCREL16
370 reloc. This typically represents an instruction like
375 lui $at,%hi(foo - $L1)
377 addiu $at,%lo(foo - $L1)
380 PCREL16_EXPANSION_ADJUSTMENT is the number of bytes this changes the
383 #define PCREL16_EXPANSION_ADJUSTMENT (4 * 4)
385 /* See whether the magic number matches. */
388 mips_ecoff_bad_format_hook (abfd
, filehdr
)
392 struct internal_filehdr
*internal_f
= (struct internal_filehdr
*) filehdr
;
394 switch (internal_f
->f_magic
)
397 /* I don't know what endianness this implies. */
401 case MIPS_MAGIC_BIG2
:
402 case MIPS_MAGIC_BIG3
:
403 return bfd_big_endian (abfd
);
405 case MIPS_MAGIC_LITTLE
:
406 case MIPS_MAGIC_LITTLE2
:
407 case MIPS_MAGIC_LITTLE3
:
408 return bfd_little_endian (abfd
);
415 /* Reloc handling. MIPS ECOFF relocs are packed into 8 bytes in
416 external form. They use a bit which indicates whether the symbol
419 /* Swap a reloc in. */
422 mips_ecoff_swap_reloc_in (abfd
, ext_ptr
, intern
)
425 struct internal_reloc
*intern
;
427 const RELOC
*ext
= (RELOC
*) ext_ptr
;
429 intern
->r_vaddr
= H_GET_32 (abfd
, ext
->r_vaddr
);
430 if (bfd_header_big_endian (abfd
))
432 intern
->r_symndx
= (((int) ext
->r_bits
[0]
433 << RELOC_BITS0_SYMNDX_SH_LEFT_BIG
)
434 | ((int) ext
->r_bits
[1]
435 << RELOC_BITS1_SYMNDX_SH_LEFT_BIG
)
436 | ((int) ext
->r_bits
[2]
437 << RELOC_BITS2_SYMNDX_SH_LEFT_BIG
));
438 intern
->r_type
= ((ext
->r_bits
[3] & RELOC_BITS3_TYPE_BIG
)
439 >> RELOC_BITS3_TYPE_SH_BIG
);
440 intern
->r_extern
= (ext
->r_bits
[3] & RELOC_BITS3_EXTERN_BIG
) != 0;
444 intern
->r_symndx
= (((int) ext
->r_bits
[0]
445 << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE
)
446 | ((int) ext
->r_bits
[1]
447 << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE
)
448 | ((int) ext
->r_bits
[2]
449 << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE
));
450 intern
->r_type
= (((ext
->r_bits
[3] & RELOC_BITS3_TYPE_LITTLE
)
451 >> RELOC_BITS3_TYPE_SH_LITTLE
)
452 | ((ext
->r_bits
[3] & RELOC_BITS3_TYPEHI_LITTLE
)
453 << RELOC_BITS3_TYPEHI_SH_LITTLE
));
454 intern
->r_extern
= (ext
->r_bits
[3] & RELOC_BITS3_EXTERN_LITTLE
) != 0;
457 /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or
458 MIPS_R_RELLO reloc, r_symndx is actually the offset from the
459 reloc address to the base of the difference (see
460 include/coff/mips.h for more details). We copy symndx into the
461 r_offset field so as not to confuse ecoff_slurp_reloc_table in
462 ecoff.c. In adjust_reloc_in we then copy r_offset into the reloc
464 if (intern
->r_type
== MIPS_R_SWITCH
465 || (! intern
->r_extern
466 && (intern
->r_type
== MIPS_R_RELLO
467 || intern
->r_type
== MIPS_R_RELHI
)))
469 BFD_ASSERT (! intern
->r_extern
);
470 intern
->r_offset
= intern
->r_symndx
;
471 if (intern
->r_offset
& 0x800000)
472 intern
->r_offset
-= 0x1000000;
473 intern
->r_symndx
= RELOC_SECTION_TEXT
;
477 /* Swap a reloc out. */
480 mips_ecoff_swap_reloc_out (abfd
, intern
, dst
)
482 const struct internal_reloc
*intern
;
485 RELOC
*ext
= (RELOC
*) dst
;
488 BFD_ASSERT (intern
->r_extern
489 || (intern
->r_symndx
>= 0 && intern
->r_symndx
<= 12));
491 /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELLO or
492 MIPS_R_RELHI reloc, we actually want to write the contents of
493 r_offset out as the symbol index. This undoes the change made by
494 mips_ecoff_swap_reloc_in. */
495 if (intern
->r_type
!= MIPS_R_SWITCH
497 || (intern
->r_type
!= MIPS_R_RELHI
498 && intern
->r_type
!= MIPS_R_RELLO
)))
499 r_symndx
= intern
->r_symndx
;
502 BFD_ASSERT (intern
->r_symndx
== RELOC_SECTION_TEXT
);
503 r_symndx
= intern
->r_offset
& 0xffffff;
506 H_PUT_32 (abfd
, intern
->r_vaddr
, ext
->r_vaddr
);
507 if (bfd_header_big_endian (abfd
))
509 ext
->r_bits
[0] = r_symndx
>> RELOC_BITS0_SYMNDX_SH_LEFT_BIG
;
510 ext
->r_bits
[1] = r_symndx
>> RELOC_BITS1_SYMNDX_SH_LEFT_BIG
;
511 ext
->r_bits
[2] = r_symndx
>> RELOC_BITS2_SYMNDX_SH_LEFT_BIG
;
512 ext
->r_bits
[3] = (((intern
->r_type
<< RELOC_BITS3_TYPE_SH_BIG
)
513 & RELOC_BITS3_TYPE_BIG
)
514 | (intern
->r_extern
? RELOC_BITS3_EXTERN_BIG
: 0));
518 ext
->r_bits
[0] = r_symndx
>> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE
;
519 ext
->r_bits
[1] = r_symndx
>> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE
;
520 ext
->r_bits
[2] = r_symndx
>> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE
;
521 ext
->r_bits
[3] = (((intern
->r_type
<< RELOC_BITS3_TYPE_SH_LITTLE
)
522 & RELOC_BITS3_TYPE_LITTLE
)
523 | ((intern
->r_type
>> RELOC_BITS3_TYPEHI_SH_LITTLE
524 & RELOC_BITS3_TYPEHI_LITTLE
))
525 | (intern
->r_extern
? RELOC_BITS3_EXTERN_LITTLE
: 0));
529 /* Finish canonicalizing a reloc. Part of this is generic to all
530 ECOFF targets, and that part is in ecoff.c. The rest is done in
531 this backend routine. It must fill in the howto field. */
534 mips_adjust_reloc_in (abfd
, intern
, rptr
)
536 const struct internal_reloc
*intern
;
539 if (intern
->r_type
> MIPS_R_SWITCH
)
542 if (! intern
->r_extern
543 && (intern
->r_type
== MIPS_R_GPREL
544 || intern
->r_type
== MIPS_R_LITERAL
))
545 rptr
->addend
+= ecoff_data (abfd
)->gp
;
547 /* If the type is MIPS_R_IGNORE, make sure this is a reference to
548 the absolute section so that the reloc is ignored. */
549 if (intern
->r_type
== MIPS_R_IGNORE
)
550 rptr
->sym_ptr_ptr
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
552 /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or
553 MIPS_R_RELLO reloc, we want the addend field of the BFD relocto
554 hold the value which was originally in the symndx field of the
555 internal MIPS ECOFF reloc. This value was copied into
556 intern->r_offset by mips_swap_reloc_in, and here we copy it into
558 if (intern
->r_type
== MIPS_R_SWITCH
559 || (! intern
->r_extern
560 && (intern
->r_type
== MIPS_R_RELHI
561 || intern
->r_type
== MIPS_R_RELLO
)))
562 rptr
->addend
= intern
->r_offset
;
564 rptr
->howto
= &mips_howto_table
[intern
->r_type
];
567 /* Make any adjustments needed to a reloc before writing it out. None
568 are needed for MIPS. */
571 mips_adjust_reloc_out (abfd
, rel
, intern
)
572 bfd
*abfd ATTRIBUTE_UNUSED
;
574 struct internal_reloc
*intern
;
576 /* For a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or
577 MIPS_R_RELLO reloc, we must copy rel->addend into
578 intern->r_offset. This will then be written out as the symbol
579 index by mips_ecoff_swap_reloc_out. This operation parallels the
580 action of mips_adjust_reloc_in. */
581 if (intern
->r_type
== MIPS_R_SWITCH
582 || (! intern
->r_extern
583 && (intern
->r_type
== MIPS_R_RELHI
584 || intern
->r_type
== MIPS_R_RELLO
)))
585 intern
->r_offset
= rel
->addend
;
588 /* ECOFF relocs are either against external symbols, or against
589 sections. If we are producing relocateable output, and the reloc
590 is against an external symbol, and nothing has given us any
591 additional addend, the resulting reloc will also be against the
592 same symbol. In such a case, we don't want to change anything
593 about the way the reloc is handled, since it will all be done at
594 final link time. Rather than put special case code into
595 bfd_perform_relocation, all the reloc types use this howto
596 function. It just short circuits the reloc if producing
597 relocateable output against an external symbol. */
599 static bfd_reloc_status_type
600 mips_generic_reloc (abfd
,
607 bfd
*abfd ATTRIBUTE_UNUSED
;
608 arelent
*reloc_entry
;
610 PTR data ATTRIBUTE_UNUSED
;
611 asection
*input_section
;
613 char **error_message ATTRIBUTE_UNUSED
;
615 if (output_bfd
!= (bfd
*) NULL
616 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
617 && reloc_entry
->addend
== 0)
619 reloc_entry
->address
+= input_section
->output_offset
;
623 return bfd_reloc_continue
;
626 /* Do a REFHI relocation. This has to be done in combination with a
627 REFLO reloc, because there is a carry from the REFLO to the REFHI.
628 Here we just save the information we need; we do the actual
629 relocation when we see the REFLO. MIPS ECOFF requires that the
630 REFLO immediately follow the REFHI. As a GNU extension, we permit
631 an arbitrary number of HI relocs to be associated with a single LO
632 reloc. This extension permits gcc to output the HI and LO relocs
637 struct mips_hi
*next
;
642 /* FIXME: This should not be a static variable. */
644 static struct mips_hi
*mips_refhi_list
;
646 static bfd_reloc_status_type
647 mips_refhi_reloc (abfd
,
654 bfd
*abfd ATTRIBUTE_UNUSED
;
655 arelent
*reloc_entry
;
658 asection
*input_section
;
660 char **error_message ATTRIBUTE_UNUSED
;
662 bfd_reloc_status_type ret
;
666 /* If we're relocating, and this an external symbol, we don't want
667 to change anything. */
668 if (output_bfd
!= (bfd
*) NULL
669 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
670 && reloc_entry
->addend
== 0)
672 reloc_entry
->address
+= input_section
->output_offset
;
677 if (bfd_is_und_section (symbol
->section
)
678 && output_bfd
== (bfd
*) NULL
)
679 ret
= bfd_reloc_undefined
;
681 if (bfd_is_com_section (symbol
->section
))
684 relocation
= symbol
->value
;
686 relocation
+= symbol
->section
->output_section
->vma
;
687 relocation
+= symbol
->section
->output_offset
;
688 relocation
+= reloc_entry
->addend
;
690 if (reloc_entry
->address
> input_section
->_cooked_size
)
691 return bfd_reloc_outofrange
;
693 /* Save the information, and let REFLO do the actual relocation. */
694 n
= (struct mips_hi
*) bfd_malloc ((bfd_size_type
) sizeof *n
);
696 return bfd_reloc_outofrange
;
697 n
->addr
= (bfd_byte
*) data
+ reloc_entry
->address
;
698 n
->addend
= relocation
;
699 n
->next
= mips_refhi_list
;
702 if (output_bfd
!= (bfd
*) NULL
)
703 reloc_entry
->address
+= input_section
->output_offset
;
708 /* Do a REFLO relocation. This is a straightforward 16 bit inplace
709 relocation; this function exists in order to do the REFHI
710 relocation described above. */
712 static bfd_reloc_status_type
713 mips_reflo_reloc (abfd
,
721 arelent
*reloc_entry
;
724 asection
*input_section
;
726 char **error_message
;
728 if (mips_refhi_list
!= NULL
)
738 struct mips_hi
*next
;
740 /* Do the REFHI relocation. Note that we actually don't
741 need to know anything about the REFLO itself, except
742 where to find the low 16 bits of the addend needed by the
744 insn
= bfd_get_32 (abfd
, l
->addr
);
745 vallo
= (bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
)
747 val
= ((insn
& 0xffff) << 16) + vallo
;
750 /* The low order 16 bits are always treated as a signed
751 value. Therefore, a negative value in the low order bits
752 requires an adjustment in the high order bits. We need
753 to make this adjustment in two ways: once for the bits we
754 took from the data, and once for the bits we are putting
755 back in to the data. */
756 if ((vallo
& 0x8000) != 0)
758 if ((val
& 0x8000) != 0)
761 insn
= (insn
&~ (unsigned) 0xffff) | ((val
>> 16) & 0xffff);
762 bfd_put_32 (abfd
, (bfd_vma
) insn
, l
->addr
);
769 mips_refhi_list
= NULL
;
772 /* Now do the REFLO reloc in the usual way. */
773 return mips_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
774 input_section
, output_bfd
, error_message
);
777 /* Do a GPREL relocation. This is a 16 bit value which must become
778 the offset from the gp register. */
780 static bfd_reloc_status_type
781 mips_gprel_reloc (abfd
,
789 arelent
*reloc_entry
;
792 asection
*input_section
;
794 char **error_message
;
796 boolean relocateable
;
802 /* If we're relocating, and this is an external symbol with no
803 addend, we don't want to change anything. We will only have an
804 addend if this is a newly created reloc, not read from an ECOFF
806 if (output_bfd
!= (bfd
*) NULL
807 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
808 && reloc_entry
->addend
== 0)
810 reloc_entry
->address
+= input_section
->output_offset
;
814 if (output_bfd
!= (bfd
*) NULL
)
818 relocateable
= false;
819 output_bfd
= symbol
->section
->output_section
->owner
;
822 if (bfd_is_und_section (symbol
->section
)
823 && relocateable
== false)
824 return bfd_reloc_undefined
;
826 /* We have to figure out the gp value, so that we can adjust the
827 symbol value correctly. We look up the symbol _gp in the output
828 BFD. If we can't find it, we're stuck. We cache it in the ECOFF
829 target data. We don't need to adjust the symbol value for an
830 external symbol if we are producing relocateable output. */
831 gp
= _bfd_get_gp_value (output_bfd
);
833 && (relocateable
== false
834 || (symbol
->flags
& BSF_SECTION_SYM
) != 0))
836 if (relocateable
!= false)
838 /* Make up a value. */
839 gp
= symbol
->section
->output_section
->vma
+ 0x4000;
840 _bfd_set_gp_value (output_bfd
, gp
);
848 count
= bfd_get_symcount (output_bfd
);
849 sym
= bfd_get_outsymbols (output_bfd
);
851 if (sym
== (asymbol
**) NULL
)
855 for (i
= 0; i
< count
; i
++, sym
++)
857 register const char *name
;
859 name
= bfd_asymbol_name (*sym
);
860 if (*name
== '_' && strcmp (name
, "_gp") == 0)
862 gp
= bfd_asymbol_value (*sym
);
863 _bfd_set_gp_value (output_bfd
, gp
);
871 /* Only get the error once. */
873 _bfd_set_gp_value (output_bfd
, gp
);
875 (char *) _("GP relative relocation when _gp not defined");
876 return bfd_reloc_dangerous
;
881 if (bfd_is_com_section (symbol
->section
))
884 relocation
= symbol
->value
;
886 relocation
+= symbol
->section
->output_section
->vma
;
887 relocation
+= symbol
->section
->output_offset
;
889 if (reloc_entry
->address
> input_section
->_cooked_size
)
890 return bfd_reloc_outofrange
;
892 insn
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
);
894 /* Set val to the offset into the section or symbol. */
895 val
= ((insn
& 0xffff) + reloc_entry
->addend
) & 0xffff;
899 /* Adjust val for the final section location and GP value. If we
900 are producing relocateable output, we don't want to do this for
901 an external symbol. */
902 if (relocateable
== false
903 || (symbol
->flags
& BSF_SECTION_SYM
) != 0)
904 val
+= relocation
- gp
;
906 insn
= (insn
&~ (unsigned) 0xffff) | (val
& 0xffff);
907 bfd_put_32 (abfd
, (bfd_vma
) insn
, (bfd_byte
*) data
+ reloc_entry
->address
);
909 if (relocateable
!= false)
910 reloc_entry
->address
+= input_section
->output_offset
;
912 /* Make sure it fit in 16 bits. */
913 if ((long) val
>= 0x8000 || (long) val
< -0x8000)
914 return bfd_reloc_overflow
;
919 /* Do a RELHI relocation. We do this in conjunction with a RELLO
920 reloc, just as REFHI and REFLO are done together. RELHI and RELLO
921 are Cygnus extensions used when generating position independent
922 code for embedded systems. */
924 /* FIXME: This should not be a static variable. */
926 static struct mips_hi
*mips_relhi_list
;
928 static bfd_reloc_status_type
929 mips_relhi_reloc (abfd
,
936 bfd
*abfd ATTRIBUTE_UNUSED
;
937 arelent
*reloc_entry
;
940 asection
*input_section
;
942 char **error_message ATTRIBUTE_UNUSED
;
944 bfd_reloc_status_type ret
;
948 /* If this is a reloc against a section symbol, then it is correct
949 in the object file. The only time we want to change this case is
950 when we are relaxing, and that is handled entirely by
951 mips_relocate_section and never calls this function. */
952 if ((symbol
->flags
& BSF_SECTION_SYM
) != 0)
954 if (output_bfd
!= (bfd
*) NULL
)
955 reloc_entry
->address
+= input_section
->output_offset
;
959 /* This is an external symbol. If we're relocating, we don't want
960 to change anything. */
961 if (output_bfd
!= (bfd
*) NULL
)
963 reloc_entry
->address
+= input_section
->output_offset
;
968 if (bfd_is_und_section (symbol
->section
)
969 && output_bfd
== (bfd
*) NULL
)
970 ret
= bfd_reloc_undefined
;
972 if (bfd_is_com_section (symbol
->section
))
975 relocation
= symbol
->value
;
977 relocation
+= symbol
->section
->output_section
->vma
;
978 relocation
+= symbol
->section
->output_offset
;
979 relocation
+= reloc_entry
->addend
;
981 if (reloc_entry
->address
> input_section
->_cooked_size
)
982 return bfd_reloc_outofrange
;
984 /* Save the information, and let RELLO do the actual relocation. */
985 n
= (struct mips_hi
*) bfd_malloc ((bfd_size_type
) sizeof *n
);
987 return bfd_reloc_outofrange
;
988 n
->addr
= (bfd_byte
*) data
+ reloc_entry
->address
;
989 n
->addend
= relocation
;
990 n
->next
= mips_relhi_list
;
993 if (output_bfd
!= (bfd
*) NULL
)
994 reloc_entry
->address
+= input_section
->output_offset
;
999 /* Do a RELLO relocation. This is a straightforward 16 bit PC
1000 relative relocation; this function exists in order to do the RELHI
1001 relocation described above. */
1003 static bfd_reloc_status_type
1004 mips_rello_reloc (abfd
,
1012 arelent
*reloc_entry
;
1015 asection
*input_section
;
1017 char **error_message
;
1019 if (mips_relhi_list
!= NULL
)
1023 l
= mips_relhi_list
;
1028 unsigned long vallo
;
1029 struct mips_hi
*next
;
1031 /* Do the RELHI relocation. Note that we actually don't
1032 need to know anything about the RELLO itself, except
1033 where to find the low 16 bits of the addend needed by the
1035 insn
= bfd_get_32 (abfd
, l
->addr
);
1036 vallo
= (bfd_get_32 (abfd
, (bfd_byte
*) data
+ reloc_entry
->address
)
1038 val
= ((insn
& 0xffff) << 16) + vallo
;
1041 /* If the symbol is defined, make val PC relative. If the
1042 symbol is not defined we don't want to do this, because
1043 we don't want the value in the object file to incorporate
1044 the address of the reloc. */
1045 if (! bfd_is_und_section (bfd_get_section (symbol
))
1046 && ! bfd_is_com_section (bfd_get_section (symbol
)))
1047 val
-= (input_section
->output_section
->vma
1048 + input_section
->output_offset
1049 + reloc_entry
->address
);
1051 /* The low order 16 bits are always treated as a signed
1052 value. Therefore, a negative value in the low order bits
1053 requires an adjustment in the high order bits. We need
1054 to make this adjustment in two ways: once for the bits we
1055 took from the data, and once for the bits we are putting
1056 back in to the data. */
1057 if ((vallo
& 0x8000) != 0)
1059 if ((val
& 0x8000) != 0)
1062 insn
= (insn
&~ (unsigned) 0xffff) | ((val
>> 16) & 0xffff);
1063 bfd_put_32 (abfd
, (bfd_vma
) insn
, l
->addr
);
1070 mips_relhi_list
= NULL
;
1073 /* If this is a reloc against a section symbol, then it is correct
1074 in the object file. The only time we want to change this case is
1075 when we are relaxing, and that is handled entirely by
1076 mips_relocate_section and never calls this function. */
1077 if ((symbol
->flags
& BSF_SECTION_SYM
) != 0)
1079 if (output_bfd
!= (bfd
*) NULL
)
1080 reloc_entry
->address
+= input_section
->output_offset
;
1081 return bfd_reloc_ok
;
1084 /* bfd_perform_relocation does not handle pcrel_offset relocations
1085 correctly when generating a relocateable file, so handle them
1087 if (output_bfd
!= (bfd
*) NULL
)
1089 reloc_entry
->address
+= input_section
->output_offset
;
1090 return bfd_reloc_ok
;
1093 /* Now do the RELLO reloc in the usual way. */
1094 return mips_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
1095 input_section
, output_bfd
, error_message
);
1098 /* This is the special function for the MIPS_R_SWITCH reloc. This
1099 special reloc is normally correct in the object file, and only
1100 requires special handling when relaxing. We don't want
1101 bfd_perform_relocation to tamper with it at all. */
1103 static bfd_reloc_status_type
1104 mips_switch_reloc (abfd
,
1111 bfd
*abfd ATTRIBUTE_UNUSED
;
1112 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1113 asymbol
*symbol ATTRIBUTE_UNUSED
;
1114 PTR data ATTRIBUTE_UNUSED
;
1115 asection
*input_section ATTRIBUTE_UNUSED
;
1116 bfd
*output_bfd ATTRIBUTE_UNUSED
;
1117 char **error_message ATTRIBUTE_UNUSED
;
1119 return bfd_reloc_ok
;
1122 /* Get the howto structure for a generic reloc type. */
1124 static reloc_howto_type
*
1125 mips_bfd_reloc_type_lookup (abfd
, code
)
1126 bfd
*abfd ATTRIBUTE_UNUSED
;
1127 bfd_reloc_code_real_type code
;
1134 mips_type
= MIPS_R_REFHALF
;
1137 case BFD_RELOC_CTOR
:
1138 mips_type
= MIPS_R_REFWORD
;
1140 case BFD_RELOC_MIPS_JMP
:
1141 mips_type
= MIPS_R_JMPADDR
;
1143 case BFD_RELOC_HI16_S
:
1144 mips_type
= MIPS_R_REFHI
;
1146 case BFD_RELOC_LO16
:
1147 mips_type
= MIPS_R_REFLO
;
1149 case BFD_RELOC_GPREL16
:
1150 mips_type
= MIPS_R_GPREL
;
1152 case BFD_RELOC_MIPS_LITERAL
:
1153 mips_type
= MIPS_R_LITERAL
;
1155 case BFD_RELOC_16_PCREL_S2
:
1156 mips_type
= MIPS_R_PCREL16
;
1158 case BFD_RELOC_PCREL_HI16_S
:
1159 mips_type
= MIPS_R_RELHI
;
1161 case BFD_RELOC_PCREL_LO16
:
1162 mips_type
= MIPS_R_RELLO
;
1164 case BFD_RELOC_GPREL32
:
1165 mips_type
= MIPS_R_SWITCH
;
1168 return (reloc_howto_type
*) NULL
;
1171 return &mips_howto_table
[mips_type
];
1174 /* A helper routine for mips_relocate_section which handles the REFHI
1175 and RELHI relocations. The REFHI relocation must be followed by a
1176 REFLO relocation (and RELHI by a RELLO), and the addend used is
1177 formed from the addends of both instructions. */
1180 mips_relocate_hi (refhi
, reflo
, input_bfd
, input_section
, contents
, adjust
,
1182 struct internal_reloc
*refhi
;
1183 struct internal_reloc
*reflo
;
1185 asection
*input_section
;
1193 unsigned long vallo
;
1198 insn
= bfd_get_32 (input_bfd
,
1199 contents
+ adjust
+ refhi
->r_vaddr
- input_section
->vma
);
1203 vallo
= (bfd_get_32 (input_bfd
,
1204 contents
+ adjust
+ reflo
->r_vaddr
- input_section
->vma
)
1207 val
= ((insn
& 0xffff) << 16) + vallo
;
1210 /* The low order 16 bits are always treated as a signed value.
1211 Therefore, a negative value in the low order bits requires an
1212 adjustment in the high order bits. We need to make this
1213 adjustment in two ways: once for the bits we took from the data,
1214 and once for the bits we are putting back in to the data. */
1215 if ((vallo
& 0x8000) != 0)
1219 val
-= (input_section
->output_section
->vma
1220 + input_section
->output_offset
1221 + (reflo
->r_vaddr
- input_section
->vma
+ adjust
));
1223 if ((val
& 0x8000) != 0)
1226 insn
= (insn
&~ (unsigned) 0xffff) | ((val
>> 16) & 0xffff);
1227 bfd_put_32 (input_bfd
, (bfd_vma
) insn
,
1228 contents
+ adjust
+ refhi
->r_vaddr
- input_section
->vma
);
1231 /* Relocate a section while linking a MIPS ECOFF file. */
1234 mips_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1235 contents
, external_relocs
)
1237 struct bfd_link_info
*info
;
1239 asection
*input_section
;
1241 PTR external_relocs
;
1243 asection
**symndx_to_section
;
1244 struct ecoff_link_hash_entry
**sym_hashes
;
1246 boolean gp_undefined
;
1249 struct external_reloc
*ext_rel
;
1250 struct external_reloc
*ext_rel_end
;
1253 struct internal_reloc lo_int_rel
;
1256 BFD_ASSERT (input_bfd
->xvec
->byteorder
1257 == output_bfd
->xvec
->byteorder
);
1259 /* We keep a table mapping the symndx found in an internal reloc to
1260 the appropriate section. This is faster than looking up the
1261 section by name each time. */
1262 symndx_to_section
= ecoff_data (input_bfd
)->symndx_to_section
;
1263 if (symndx_to_section
== (asection
**) NULL
)
1265 amt
= NUM_RELOC_SECTIONS
* sizeof (asection
*);
1266 symndx_to_section
= (asection
**) bfd_alloc (input_bfd
, amt
);
1267 if (!symndx_to_section
)
1270 symndx_to_section
[RELOC_SECTION_NONE
] = NULL
;
1271 symndx_to_section
[RELOC_SECTION_TEXT
] =
1272 bfd_get_section_by_name (input_bfd
, ".text");
1273 symndx_to_section
[RELOC_SECTION_RDATA
] =
1274 bfd_get_section_by_name (input_bfd
, ".rdata");
1275 symndx_to_section
[RELOC_SECTION_DATA
] =
1276 bfd_get_section_by_name (input_bfd
, ".data");
1277 symndx_to_section
[RELOC_SECTION_SDATA
] =
1278 bfd_get_section_by_name (input_bfd
, ".sdata");
1279 symndx_to_section
[RELOC_SECTION_SBSS
] =
1280 bfd_get_section_by_name (input_bfd
, ".sbss");
1281 symndx_to_section
[RELOC_SECTION_BSS
] =
1282 bfd_get_section_by_name (input_bfd
, ".bss");
1283 symndx_to_section
[RELOC_SECTION_INIT
] =
1284 bfd_get_section_by_name (input_bfd
, ".init");
1285 symndx_to_section
[RELOC_SECTION_LIT8
] =
1286 bfd_get_section_by_name (input_bfd
, ".lit8");
1287 symndx_to_section
[RELOC_SECTION_LIT4
] =
1288 bfd_get_section_by_name (input_bfd
, ".lit4");
1289 symndx_to_section
[RELOC_SECTION_XDATA
] = NULL
;
1290 symndx_to_section
[RELOC_SECTION_PDATA
] = NULL
;
1291 symndx_to_section
[RELOC_SECTION_FINI
] =
1292 bfd_get_section_by_name (input_bfd
, ".fini");
1293 symndx_to_section
[RELOC_SECTION_LITA
] = NULL
;
1294 symndx_to_section
[RELOC_SECTION_ABS
] = NULL
;
1296 ecoff_data (input_bfd
)->symndx_to_section
= symndx_to_section
;
1299 sym_hashes
= ecoff_data (input_bfd
)->sym_hashes
;
1301 gp
= _bfd_get_gp_value (output_bfd
);
1303 gp_undefined
= true;
1305 gp_undefined
= false;
1311 if (ecoff_section_data (input_bfd
, input_section
) == NULL
)
1314 offsets
= ecoff_section_data (input_bfd
, input_section
)->offsets
;
1316 ext_rel
= (struct external_reloc
*) external_relocs
;
1317 ext_rel_end
= ext_rel
+ input_section
->reloc_count
;
1318 for (i
= 0; ext_rel
< ext_rel_end
; ext_rel
++, i
++)
1320 struct internal_reloc int_rel
;
1321 boolean use_lo
= false;
1323 reloc_howto_type
*howto
;
1324 struct ecoff_link_hash_entry
*h
= NULL
;
1327 bfd_reloc_status_type r
;
1330 mips_ecoff_swap_reloc_in (input_bfd
, (PTR
) ext_rel
, &int_rel
);
1333 int_rel
= lo_int_rel
;
1337 BFD_ASSERT (int_rel
.r_type
1338 < sizeof mips_howto_table
/ sizeof mips_howto_table
[0]);
1340 /* The REFHI and RELHI relocs requires special handling. they
1341 must be followed by a REFLO or RELLO reloc, respectively, and
1342 the addend is formed from both relocs. */
1343 if (int_rel
.r_type
== MIPS_R_REFHI
1344 || int_rel
.r_type
== MIPS_R_RELHI
)
1346 struct external_reloc
*lo_ext_rel
;
1348 /* As a GNU extension, permit an arbitrary number of REFHI
1349 or RELHI relocs before the REFLO or RELLO reloc. This
1350 permits gcc to emit the HI and LO relocs itself. */
1351 for (lo_ext_rel
= ext_rel
+ 1;
1352 lo_ext_rel
< ext_rel_end
;
1355 mips_ecoff_swap_reloc_in (input_bfd
, (PTR
) lo_ext_rel
,
1357 if (lo_int_rel
.r_type
!= int_rel
.r_type
)
1361 if (lo_ext_rel
< ext_rel_end
1362 && (lo_int_rel
.r_type
1363 == (int_rel
.r_type
== MIPS_R_REFHI
1366 && int_rel
.r_extern
== lo_int_rel
.r_extern
1367 && int_rel
.r_symndx
== lo_int_rel
.r_symndx
)
1370 if (lo_ext_rel
== ext_rel
+ 1)
1375 howto
= &mips_howto_table
[int_rel
.r_type
];
1377 /* The SWITCH reloc must be handled specially. This reloc is
1378 marks the location of a difference between two portions of an
1379 object file. The symbol index does not reference a symbol,
1380 but is actually the offset from the reloc to the subtrahend
1381 of the difference. This reloc is correct in the object file,
1382 and needs no further adjustment, unless we are relaxing. If
1383 we are relaxing, we may have to add in an offset. Since no
1384 symbols are involved in this reloc, we handle it completely
1386 if (int_rel
.r_type
== MIPS_R_SWITCH
)
1391 r
= _bfd_relocate_contents (howto
, input_bfd
,
1392 (bfd_vma
) offsets
[i
],
1396 - input_section
->vma
));
1397 BFD_ASSERT (r
== bfd_reloc_ok
);
1403 if (int_rel
.r_extern
)
1405 h
= sym_hashes
[int_rel
.r_symndx
];
1406 /* If h is NULL, that means that there is a reloc against an
1407 external symbol which we thought was just a debugging
1408 symbol. This should not happen. */
1409 if (h
== (struct ecoff_link_hash_entry
*) NULL
)
1414 if (int_rel
.r_symndx
< 0 || int_rel
.r_symndx
>= NUM_RELOC_SECTIONS
)
1417 s
= symndx_to_section
[int_rel
.r_symndx
];
1419 if (s
== (asection
*) NULL
)
1423 /* The GPREL reloc uses an addend: the difference in the GP
1425 if (int_rel
.r_type
!= MIPS_R_GPREL
1426 && int_rel
.r_type
!= MIPS_R_LITERAL
)
1432 if (! ((*info
->callbacks
->reloc_dangerous
)
1433 (info
, _("GP relative relocation used when GP not defined"),
1434 input_bfd
, input_section
,
1435 int_rel
.r_vaddr
- input_section
->vma
)))
1437 /* Only give the error once per link. */
1439 _bfd_set_gp_value (output_bfd
, gp
);
1440 gp_undefined
= false;
1442 if (! int_rel
.r_extern
)
1444 /* This is a relocation against a section. The current
1445 addend in the instruction is the difference between
1446 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
1447 must change this to be the difference between the
1448 final definition (which will end up in RELOCATION)
1449 and the GP value of OUTPUT_BFD (which is in GP). */
1450 addend
= ecoff_data (input_bfd
)->gp
- gp
;
1452 else if (! info
->relocateable
1453 || h
->root
.type
== bfd_link_hash_defined
1454 || h
->root
.type
== bfd_link_hash_defweak
)
1456 /* This is a relocation against a defined symbol. The
1457 current addend in the instruction is simply the
1458 desired offset into the symbol (normally zero). We
1459 are going to change this into a relocation against a
1460 defined symbol, so we want the instruction to hold
1461 the difference between the final definition of the
1462 symbol (which will end up in RELOCATION) and the GP
1463 value of OUTPUT_BFD (which is in GP). */
1468 /* This is a relocation against an undefined or common
1469 symbol. The current addend in the instruction is
1470 simply the desired offset into the symbol (normally
1471 zero). We are generating relocateable output, and we
1472 aren't going to define this symbol, so we just leave
1473 the instruction alone. */
1478 /* If we are relaxing, mips_relax_section may have set
1479 offsets[i] to some value. A value of 1 means we must expand
1480 a PC relative branch into a multi-instruction of sequence,
1481 and any other value is an addend. */
1485 BFD_ASSERT (! info
->relocateable
);
1486 BFD_ASSERT (int_rel
.r_type
== MIPS_R_PCREL16
1487 || int_rel
.r_type
== MIPS_R_RELHI
1488 || int_rel
.r_type
== MIPS_R_RELLO
);
1489 if (offsets
[i
] != 1)
1490 addend
+= offsets
[i
];
1495 BFD_ASSERT (int_rel
.r_extern
1496 && int_rel
.r_type
== MIPS_R_PCREL16
);
1498 /* Move the rest of the instructions up. */
1502 - input_section
->vma
);
1503 memmove (here
+ PCREL16_EXPANSION_ADJUSTMENT
, here
,
1504 (size_t) (input_section
->_raw_size
1505 - (int_rel
.r_vaddr
- input_section
->vma
)));
1507 /* Generate the new instructions. */
1508 if (! mips_relax_pcrel16 (info
, input_bfd
, input_section
,
1510 (input_section
->output_section
->vma
1511 + input_section
->output_offset
1513 - input_section
->vma
)
1517 /* We must adjust everything else up a notch. */
1518 adjust
+= PCREL16_EXPANSION_ADJUSTMENT
;
1520 /* mips_relax_pcrel16 handles all the details of this
1526 /* If we are relaxing, and this is a reloc against the .text
1527 segment, we may need to adjust it if some branches have been
1528 expanded. The reloc types which are likely to occur in the
1529 .text section are handled efficiently by mips_relax_section,
1530 and thus do not need to be handled here. */
1531 if (ecoff_data (input_bfd
)->debug_info
.adjust
!= NULL
1532 && ! int_rel
.r_extern
1533 && int_rel
.r_symndx
== RELOC_SECTION_TEXT
1534 && (strcmp (bfd_get_section_name (input_bfd
, input_section
),
1536 || (int_rel
.r_type
!= MIPS_R_PCREL16
1537 && int_rel
.r_type
!= MIPS_R_SWITCH
1538 && int_rel
.r_type
!= MIPS_R_RELHI
1539 && int_rel
.r_type
!= MIPS_R_RELLO
)))
1542 struct ecoff_value_adjust
*a
;
1544 /* We need to get the addend so that we know whether we need
1545 to adjust the address. */
1546 BFD_ASSERT (int_rel
.r_type
== MIPS_R_REFWORD
);
1548 adr
= bfd_get_32 (input_bfd
,
1552 - input_section
->vma
));
1554 for (a
= ecoff_data (input_bfd
)->debug_info
.adjust
;
1555 a
!= (struct ecoff_value_adjust
*) NULL
;
1558 if (adr
>= a
->start
&& adr
< a
->end
)
1559 addend
+= a
->adjust
;
1563 if (info
->relocateable
)
1565 /* We are generating relocateable output, and must convert
1566 the existing reloc. */
1567 if (int_rel
.r_extern
)
1569 if ((h
->root
.type
== bfd_link_hash_defined
1570 || h
->root
.type
== bfd_link_hash_defweak
)
1571 && ! bfd_is_abs_section (h
->root
.u
.def
.section
))
1575 /* This symbol is defined in the output. Convert
1576 the reloc from being against the symbol to being
1577 against the section. */
1579 /* Clear the r_extern bit. */
1580 int_rel
.r_extern
= 0;
1582 /* Compute a new r_symndx value. */
1583 s
= h
->root
.u
.def
.section
;
1584 name
= bfd_get_section_name (output_bfd
,
1587 int_rel
.r_symndx
= -1;
1591 if (strcmp (name
, ".bss") == 0)
1592 int_rel
.r_symndx
= RELOC_SECTION_BSS
;
1595 if (strcmp (name
, ".data") == 0)
1596 int_rel
.r_symndx
= RELOC_SECTION_DATA
;
1599 if (strcmp (name
, ".fini") == 0)
1600 int_rel
.r_symndx
= RELOC_SECTION_FINI
;
1603 if (strcmp (name
, ".init") == 0)
1604 int_rel
.r_symndx
= RELOC_SECTION_INIT
;
1607 if (strcmp (name
, ".lit8") == 0)
1608 int_rel
.r_symndx
= RELOC_SECTION_LIT8
;
1609 else if (strcmp (name
, ".lit4") == 0)
1610 int_rel
.r_symndx
= RELOC_SECTION_LIT4
;
1613 if (strcmp (name
, ".rdata") == 0)
1614 int_rel
.r_symndx
= RELOC_SECTION_RDATA
;
1617 if (strcmp (name
, ".sdata") == 0)
1618 int_rel
.r_symndx
= RELOC_SECTION_SDATA
;
1619 else if (strcmp (name
, ".sbss") == 0)
1620 int_rel
.r_symndx
= RELOC_SECTION_SBSS
;
1623 if (strcmp (name
, ".text") == 0)
1624 int_rel
.r_symndx
= RELOC_SECTION_TEXT
;
1628 if (int_rel
.r_symndx
== -1)
1631 /* Add the section VMA and the symbol value. */
1632 relocation
= (h
->root
.u
.def
.value
1633 + s
->output_section
->vma
1634 + s
->output_offset
);
1636 /* For a PC relative relocation, the object file
1637 currently holds just the addend. We must adjust
1638 by the address to get the right value. */
1639 if (howto
->pc_relative
)
1641 relocation
-= int_rel
.r_vaddr
- input_section
->vma
;
1643 /* If we are converting a RELHI or RELLO reloc
1644 from being against an external symbol to
1645 being against a section, we must put a
1646 special value into the r_offset field. This
1647 value is the old addend. The r_offset for
1648 both the RELHI and RELLO relocs are the same,
1649 and we set both when we see RELHI. */
1650 if (int_rel
.r_type
== MIPS_R_RELHI
)
1654 addhi
= bfd_get_32 (input_bfd
,
1658 - input_section
->vma
));
1668 addlo
= bfd_get_32 (input_bfd
,
1671 + lo_int_rel
.r_vaddr
1672 - input_section
->vma
));
1677 lo_int_rel
.r_offset
= addhi
+ addlo
;
1680 int_rel
.r_offset
= addhi
+ addlo
;
1688 /* Change the symndx value to the right one for the
1690 int_rel
.r_symndx
= h
->indx
;
1691 if (int_rel
.r_symndx
== -1)
1693 /* This symbol is not being written out. */
1694 if (! ((*info
->callbacks
->unattached_reloc
)
1695 (info
, h
->root
.root
.string
, input_bfd
,
1697 int_rel
.r_vaddr
- input_section
->vma
)))
1699 int_rel
.r_symndx
= 0;
1706 /* This is a relocation against a section. Adjust the
1707 value by the amount the section moved. */
1708 relocation
= (s
->output_section
->vma
1713 relocation
+= addend
;
1716 /* Adjust a PC relative relocation by removing the reference
1717 to the original address in the section and including the
1718 reference to the new address. However, external RELHI
1719 and RELLO relocs are PC relative, but don't include any
1720 reference to the address. The addend is merely an
1722 if (howto
->pc_relative
1723 && (! int_rel
.r_extern
1724 || (int_rel
.r_type
!= MIPS_R_RELHI
1725 && int_rel
.r_type
!= MIPS_R_RELLO
)))
1726 relocation
-= (input_section
->output_section
->vma
1727 + input_section
->output_offset
1728 - input_section
->vma
);
1730 /* Adjust the contents. */
1731 if (relocation
== 0)
1735 if (int_rel
.r_type
!= MIPS_R_REFHI
1736 && int_rel
.r_type
!= MIPS_R_RELHI
)
1737 r
= _bfd_relocate_contents (howto
, input_bfd
, relocation
,
1741 - input_section
->vma
));
1744 mips_relocate_hi (&int_rel
,
1745 use_lo
? &lo_int_rel
: NULL
,
1746 input_bfd
, input_section
, contents
,
1748 int_rel
.r_type
== MIPS_R_RELHI
);
1753 /* Adjust the reloc address. */
1754 int_rel
.r_vaddr
+= (input_section
->output_section
->vma
1755 + input_section
->output_offset
1756 - input_section
->vma
);
1758 /* Save the changed reloc information. */
1759 mips_ecoff_swap_reloc_out (input_bfd
, &int_rel
, (PTR
) ext_rel
);
1763 /* We are producing a final executable. */
1764 if (int_rel
.r_extern
)
1766 /* This is a reloc against a symbol. */
1767 if (h
->root
.type
== bfd_link_hash_defined
1768 || h
->root
.type
== bfd_link_hash_defweak
)
1772 hsec
= h
->root
.u
.def
.section
;
1773 relocation
= (h
->root
.u
.def
.value
1774 + hsec
->output_section
->vma
1775 + hsec
->output_offset
);
1779 if (! ((*info
->callbacks
->undefined_symbol
)
1780 (info
, h
->root
.root
.string
, input_bfd
,
1782 int_rel
.r_vaddr
- input_section
->vma
, true)))
1789 /* This is a reloc against a section. */
1790 relocation
= (s
->output_section
->vma
1794 /* A PC relative reloc is already correct in the object
1795 file. Make it look like a pcrel_offset relocation by
1796 adding in the start address. */
1797 if (howto
->pc_relative
)
1799 if (int_rel
.r_type
!= MIPS_R_RELHI
|| ! use_lo
)
1800 relocation
+= int_rel
.r_vaddr
+ adjust
;
1802 relocation
+= lo_int_rel
.r_vaddr
+ adjust
;
1806 if (int_rel
.r_type
!= MIPS_R_REFHI
1807 && int_rel
.r_type
!= MIPS_R_RELHI
)
1808 r
= _bfd_final_link_relocate (howto
,
1813 - input_section
->vma
1819 mips_relocate_hi (&int_rel
,
1820 use_lo
? &lo_int_rel
: NULL
,
1821 input_bfd
, input_section
, contents
, adjust
,
1823 int_rel
.r_type
== MIPS_R_RELHI
);
1828 /* MIPS_R_JMPADDR requires peculiar overflow detection. The
1829 instruction provides a 28 bit address (the two lower bits are
1830 implicit zeroes) which is combined with the upper four bits
1831 of the instruction address. */
1832 if (r
== bfd_reloc_ok
1833 && int_rel
.r_type
== MIPS_R_JMPADDR
1836 + (int_rel
.r_extern
? 0 : s
->vma
))
1838 != ((input_section
->output_section
->vma
1839 + input_section
->output_offset
1840 + (int_rel
.r_vaddr
- input_section
->vma
)
1843 r
= bfd_reloc_overflow
;
1845 if (r
!= bfd_reloc_ok
)
1850 case bfd_reloc_outofrange
:
1852 case bfd_reloc_overflow
:
1856 if (int_rel
.r_extern
)
1857 name
= h
->root
.root
.string
;
1859 name
= bfd_section_name (input_bfd
, s
);
1860 if (! ((*info
->callbacks
->reloc_overflow
)
1861 (info
, name
, howto
->name
, (bfd_vma
) 0,
1862 input_bfd
, input_section
,
1863 int_rel
.r_vaddr
- input_section
->vma
)))
1874 /* Read in the relocs for a section. */
1877 mips_read_relocs (abfd
, sec
)
1881 struct ecoff_section_tdata
*section_tdata
;
1884 section_tdata
= ecoff_section_data (abfd
, sec
);
1885 if (section_tdata
== (struct ecoff_section_tdata
*) NULL
)
1887 amt
= sizeof (struct ecoff_section_tdata
);
1888 sec
->used_by_bfd
= (PTR
) bfd_alloc (abfd
, amt
);
1889 if (sec
->used_by_bfd
== NULL
)
1892 section_tdata
= ecoff_section_data (abfd
, sec
);
1893 section_tdata
->external_relocs
= NULL
;
1894 section_tdata
->contents
= NULL
;
1895 section_tdata
->offsets
= NULL
;
1898 if (section_tdata
->external_relocs
== NULL
)
1900 amt
= ecoff_backend (abfd
)->external_reloc_size
;
1901 amt
*= sec
->reloc_count
;
1902 section_tdata
->external_relocs
= (PTR
) bfd_alloc (abfd
, amt
);
1903 if (section_tdata
->external_relocs
== NULL
&& amt
!= 0)
1906 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
1907 || bfd_bread (section_tdata
->external_relocs
, amt
, abfd
) != amt
)
1914 /* Relax a section when linking a MIPS ECOFF file. This is used for
1915 embedded PIC code, which always uses PC relative branches which
1916 only have an 18 bit range on MIPS. If a branch is not in range, we
1917 generate a long instruction sequence to compensate. Each time we
1918 find a branch to expand, we have to check all the others again to
1919 make sure they are still in range. This is slow, but it only has
1920 to be done when -relax is passed to the linker.
1922 This routine figures out which branches need to expand; the actual
1923 expansion is done in mips_relocate_section when the section
1924 contents are relocated. The information is stored in the offsets
1925 field of the ecoff_section_tdata structure. An offset of 1 means
1926 that the branch must be expanded into a multi-instruction PC
1927 relative branch (such an offset will only occur for a PC relative
1928 branch to an external symbol). Any other offset must be a multiple
1929 of four, and is the amount to change the branch by (such an offset
1930 will only occur for a PC relative branch within the same section).
1932 We do not modify the section relocs or contents themselves so that
1933 if memory usage becomes an issue we can discard them and read them
1934 again. The only information we must save in memory between this
1935 routine and the mips_relocate_section routine is the table of
1939 mips_relax_section (abfd
, sec
, info
, again
)
1942 struct bfd_link_info
*info
;
1945 struct ecoff_section_tdata
*section_tdata
;
1946 bfd_byte
*contents
= NULL
;
1948 struct external_reloc
*ext_rel
;
1949 struct external_reloc
*ext_rel_end
;
1952 /* Assume we are not going to need another pass. */
1955 /* If we are not generating an ECOFF file, this is much too
1956 confusing to deal with. */
1957 if (info
->hash
->creator
->flavour
!= bfd_get_flavour (abfd
))
1960 /* If there are no relocs, there is nothing to do. */
1961 if (sec
->reloc_count
== 0)
1964 /* We are only interested in PC relative relocs, and why would there
1965 ever be one from anything but the .text section? */
1966 if (strcmp (bfd_get_section_name (abfd
, sec
), ".text") != 0)
1969 /* Read in the relocs, if we haven't already got them. */
1970 section_tdata
= ecoff_section_data (abfd
, sec
);
1971 if (section_tdata
== (struct ecoff_section_tdata
*) NULL
1972 || section_tdata
->external_relocs
== NULL
)
1974 if (! mips_read_relocs (abfd
, sec
))
1976 section_tdata
= ecoff_section_data (abfd
, sec
);
1979 if (sec
->_cooked_size
== 0)
1981 /* We must initialize _cooked_size only the first time we are
1983 sec
->_cooked_size
= sec
->_raw_size
;
1986 contents
= section_tdata
->contents
;
1987 offsets
= section_tdata
->offsets
;
1989 /* Look for any external PC relative relocs. Internal PC relative
1990 relocs are already correct in the object file, so they certainly
1991 can not overflow. */
1992 ext_rel
= (struct external_reloc
*) section_tdata
->external_relocs
;
1993 ext_rel_end
= ext_rel
+ sec
->reloc_count
;
1994 for (i
= 0; ext_rel
< ext_rel_end
; ext_rel
++, i
++)
1996 struct internal_reloc int_rel
;
1997 struct ecoff_link_hash_entry
*h
;
1999 bfd_signed_vma relocation
;
2000 struct external_reloc
*adj_ext_rel
;
2002 unsigned long ext_count
;
2003 struct ecoff_link_hash_entry
**adj_h_ptr
;
2004 struct ecoff_link_hash_entry
**adj_h_ptr_end
;
2005 struct ecoff_value_adjust
*adjust
;
2008 /* If we have already expanded this reloc, we certainly don't
2009 need to do it again. */
2010 if (offsets
!= (long *) NULL
&& offsets
[i
] == 1)
2013 /* Quickly check that this reloc is external PCREL16. */
2014 if (bfd_header_big_endian (abfd
))
2016 if ((ext_rel
->r_bits
[3] & RELOC_BITS3_EXTERN_BIG
) == 0
2017 || (((ext_rel
->r_bits
[3] & RELOC_BITS3_TYPE_BIG
)
2018 >> RELOC_BITS3_TYPE_SH_BIG
)
2024 if ((ext_rel
->r_bits
[3] & RELOC_BITS3_EXTERN_LITTLE
) == 0
2025 || (((ext_rel
->r_bits
[3] & RELOC_BITS3_TYPE_LITTLE
)
2026 >> RELOC_BITS3_TYPE_SH_LITTLE
)
2031 mips_ecoff_swap_reloc_in (abfd
, (PTR
) ext_rel
, &int_rel
);
2033 h
= ecoff_data (abfd
)->sym_hashes
[int_rel
.r_symndx
];
2034 if (h
== (struct ecoff_link_hash_entry
*) NULL
)
2037 if (h
->root
.type
!= bfd_link_hash_defined
2038 && h
->root
.type
!= bfd_link_hash_defweak
)
2040 /* Just ignore undefined symbols. These will presumably
2041 generate an error later in the link. */
2045 /* Get the value of the symbol. */
2046 hsec
= h
->root
.u
.def
.section
;
2047 relocation
= (h
->root
.u
.def
.value
2048 + hsec
->output_section
->vma
2049 + hsec
->output_offset
);
2051 /* Subtract out the current address. */
2052 relocation
-= (sec
->output_section
->vma
2053 + sec
->output_offset
2054 + (int_rel
.r_vaddr
- sec
->vma
));
2056 /* The addend is stored in the object file. In the normal case
2057 of ``bal symbol'', the addend will be -4. It will only be
2058 different in the case of ``bal symbol+constant''. To avoid
2059 always reading in the section contents, we don't check the
2060 addend in the object file (we could easily check the contents
2061 if we happen to have already read them in, but I fear that
2062 this could be confusing). This means we will screw up if
2063 there is a branch to a symbol that is in range, but added to
2064 a constant which puts it out of range; in such a case the
2065 link will fail with a reloc overflow error. Since the
2066 compiler will never generate such code, it should be easy
2067 enough to work around it by changing the assembly code in the
2071 /* Now RELOCATION is the number we want to put in the object
2072 file. See whether it fits. */
2073 if (relocation
>= -0x20000 && relocation
< 0x20000)
2076 /* Now that we know this reloc needs work, which will rarely
2077 happen, go ahead and grab the section contents. */
2078 if (contents
== (bfd_byte
*) NULL
)
2080 if (info
->keep_memory
)
2081 contents
= (bfd_byte
*) bfd_alloc (abfd
, sec
->_raw_size
);
2083 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
2084 if (contents
== (bfd_byte
*) NULL
)
2086 if (! bfd_get_section_contents (abfd
, sec
, (PTR
) contents
,
2087 (file_ptr
) 0, sec
->_raw_size
))
2089 if (info
->keep_memory
)
2090 section_tdata
->contents
= contents
;
2093 /* We only support changing the bal instruction. It would be
2094 possible to handle other PC relative branches, but some of
2095 them (the conditional branches) would require a different
2096 length instruction sequence which would complicate both this
2097 routine and mips_relax_pcrel16. It could be written if
2098 somebody felt it were important. Ignoring this reloc will
2099 presumably cause a reloc overflow error later on. */
2100 if (bfd_get_32 (abfd
, contents
+ int_rel
.r_vaddr
- sec
->vma
)
2101 != 0x0411ffff) /* bgezal $0,. == bal . */
2104 /* Bother. We need to expand this reloc, and we will need to
2105 make another relaxation pass since this change may put other
2106 relocs out of range. We need to examine the local branches
2107 and we need to allocate memory to hold the offsets we must
2108 add to them. We also need to adjust the values of all
2109 symbols in the object file following this location. */
2111 sec
->_cooked_size
+= PCREL16_EXPANSION_ADJUSTMENT
;
2114 if (offsets
== (long *) NULL
)
2118 size
= (bfd_size_type
) sec
->reloc_count
* sizeof (long);
2119 offsets
= (long *) bfd_zalloc (abfd
, size
);
2120 if (offsets
== (long *) NULL
)
2122 section_tdata
->offsets
= offsets
;
2127 /* Now look for all PC relative references that cross this reloc
2128 and adjust their offsets. */
2129 adj_ext_rel
= (struct external_reloc
*) section_tdata
->external_relocs
;
2130 for (adj_i
= 0; adj_ext_rel
< ext_rel_end
; adj_ext_rel
++, adj_i
++)
2132 struct internal_reloc adj_int_rel
;
2133 bfd_vma start
, stop
;
2136 mips_ecoff_swap_reloc_in (abfd
, (PTR
) adj_ext_rel
, &adj_int_rel
);
2138 if (adj_int_rel
.r_type
== MIPS_R_PCREL16
)
2142 /* We only care about local references. External ones
2143 will be relocated correctly anyhow. */
2144 if (adj_int_rel
.r_extern
)
2147 /* We are only interested in a PC relative reloc within
2148 this section. FIXME: Cross section PC relative
2149 relocs may not be handled correctly; does anybody
2151 if (adj_int_rel
.r_symndx
!= RELOC_SECTION_TEXT
)
2154 start
= adj_int_rel
.r_vaddr
;
2156 insn
= bfd_get_32 (abfd
,
2157 contents
+ adj_int_rel
.r_vaddr
- sec
->vma
);
2159 stop
= (insn
& 0xffff) << 2;
2160 if ((stop
& 0x20000) != 0)
2162 stop
+= adj_int_rel
.r_vaddr
+ 4;
2164 else if (adj_int_rel
.r_type
== MIPS_R_RELHI
)
2166 struct internal_reloc rello
;
2169 /* The next reloc must be MIPS_R_RELLO, and we handle
2171 BFD_ASSERT (adj_ext_rel
+ 1 < ext_rel_end
);
2173 mips_ecoff_swap_reloc_in (abfd
, (PTR
) (adj_ext_rel
+ 1), &rello
);
2175 BFD_ASSERT (rello
.r_type
== MIPS_R_RELLO
);
2177 addhi
= bfd_get_32 (abfd
,
2178 contents
+ adj_int_rel
.r_vaddr
- sec
->vma
);
2184 addlo
= bfd_get_32 (abfd
, contents
+ rello
.r_vaddr
- sec
->vma
);
2189 if (adj_int_rel
.r_extern
)
2191 /* The value we want here is
2192 sym - RELLOaddr + addend
2193 which we can express as
2194 sym - (RELLOaddr - addend)
2195 Therefore if we are expanding the area between
2196 RELLOaddr and RELLOaddr - addend we must adjust
2197 the addend. This is admittedly ambiguous, since
2198 we might mean (sym + addend) - RELLOaddr, but in
2199 practice we don't, and there is no way to handle
2200 that case correctly since at this point we have
2201 no idea whether any reloc is being expanded
2202 between sym and sym + addend. */
2203 start
= rello
.r_vaddr
- (addhi
+ addlo
);
2204 stop
= rello
.r_vaddr
;
2208 /* An internal RELHI/RELLO pair represents the
2209 difference between two addresses, $LC0 - foo.
2210 The symndx value is actually the difference
2211 between the reloc address and $LC0. This lets us
2212 compute $LC0, and, by considering the addend,
2213 foo. If the reloc we are expanding falls between
2214 those two relocs, we must adjust the addend. At
2215 this point, the symndx value is actually in the
2216 r_offset field, where it was put by
2217 mips_ecoff_swap_reloc_in. */
2218 start
= rello
.r_vaddr
- adj_int_rel
.r_offset
;
2219 stop
= start
+ addhi
+ addlo
;
2222 else if (adj_int_rel
.r_type
== MIPS_R_SWITCH
)
2224 /* A MIPS_R_SWITCH reloc represents a word of the form
2226 The value in the object file is correct, assuming the
2227 original value of $L3. The symndx value is actually
2228 the difference between the reloc address and $LS12.
2229 This lets us compute the original value of $LS12 as
2231 and the original value of $L3 as
2232 vaddr - symndx + addend
2233 where addend is the value from the object file. At
2234 this point, the symndx value is actually found in the
2235 r_offset field, since it was moved by
2236 mips_ecoff_swap_reloc_in. */
2237 start
= adj_int_rel
.r_vaddr
- adj_int_rel
.r_offset
;
2238 stop
= start
+ bfd_get_32 (abfd
,
2240 + adj_int_rel
.r_vaddr
2246 /* If the range expressed by this reloc, which is the
2247 distance between START and STOP crosses the reloc we are
2248 expanding, we must adjust the offset. The sign of the
2249 adjustment depends upon the direction in which the range
2250 crosses the reloc being expanded. */
2251 if (start
<= int_rel
.r_vaddr
&& stop
> int_rel
.r_vaddr
)
2252 change
= PCREL16_EXPANSION_ADJUSTMENT
;
2253 else if (start
> int_rel
.r_vaddr
&& stop
<= int_rel
.r_vaddr
)
2254 change
= - PCREL16_EXPANSION_ADJUSTMENT
;
2258 offsets
[adj_i
] += change
;
2260 if (adj_int_rel
.r_type
== MIPS_R_RELHI
)
2264 offsets
[adj_i
] += change
;
2268 /* Find all symbols in this section defined by this object file
2269 and adjust their values. Note that we decide whether to
2270 adjust the value based on the value stored in the ECOFF EXTR
2271 structure, because the value stored in the hash table may
2272 have been changed by an earlier expanded reloc and thus may
2273 no longer correctly indicate whether the symbol is before or
2274 after the expanded reloc. */
2275 ext_count
= ecoff_data (abfd
)->debug_info
.symbolic_header
.iextMax
;
2276 adj_h_ptr
= ecoff_data (abfd
)->sym_hashes
;
2277 adj_h_ptr_end
= adj_h_ptr
+ ext_count
;
2278 for (; adj_h_ptr
< adj_h_ptr_end
; adj_h_ptr
++)
2280 struct ecoff_link_hash_entry
*adj_h
;
2283 if (adj_h
!= (struct ecoff_link_hash_entry
*) NULL
2284 && (adj_h
->root
.type
== bfd_link_hash_defined
2285 || adj_h
->root
.type
== bfd_link_hash_defweak
)
2286 && adj_h
->root
.u
.def
.section
== sec
2287 && adj_h
->esym
.asym
.value
> int_rel
.r_vaddr
)
2288 adj_h
->root
.u
.def
.value
+= PCREL16_EXPANSION_ADJUSTMENT
;
2291 /* Add an entry to the symbol value adjust list. This is used
2292 by bfd_ecoff_debug_accumulate to adjust the values of
2293 internal symbols and FDR's. */
2294 amt
= sizeof (struct ecoff_value_adjust
);
2295 adjust
= (struct ecoff_value_adjust
*) bfd_alloc (abfd
, amt
);
2296 if (adjust
== (struct ecoff_value_adjust
*) NULL
)
2299 adjust
->start
= int_rel
.r_vaddr
;
2300 adjust
->end
= sec
->vma
+ sec
->_raw_size
;
2301 adjust
->adjust
= PCREL16_EXPANSION_ADJUSTMENT
;
2303 adjust
->next
= ecoff_data (abfd
)->debug_info
.adjust
;
2304 ecoff_data (abfd
)->debug_info
.adjust
= adjust
;
2307 if (contents
!= (bfd_byte
*) NULL
&& ! info
->keep_memory
)
2313 if (contents
!= (bfd_byte
*) NULL
&& ! info
->keep_memory
)
2318 /* This routine is called from mips_relocate_section when a PC
2319 relative reloc must be expanded into the five instruction sequence.
2320 It handles all the details of the expansion, including resolving
2324 mips_relax_pcrel16 (info
, input_bfd
, input_section
, h
, location
, address
)
2325 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
2327 asection
*input_section ATTRIBUTE_UNUSED
;
2328 struct ecoff_link_hash_entry
*h
;
2334 /* 0x0411ffff is bgezal $0,. == bal . */
2335 BFD_ASSERT (bfd_get_32 (input_bfd
, location
) == 0x0411ffff);
2337 /* We need to compute the distance between the symbol and the
2338 current address plus eight. */
2339 relocation
= (h
->root
.u
.def
.value
2340 + h
->root
.u
.def
.section
->output_section
->vma
2341 + h
->root
.u
.def
.section
->output_offset
);
2342 relocation
-= address
+ 8;
2344 /* If the lower half is negative, increment the upper 16 half. */
2345 if ((relocation
& 0x8000) != 0)
2346 relocation
+= 0x10000;
2348 bfd_put_32 (input_bfd
, (bfd_vma
) 0x04110001, location
); /* bal .+8 */
2349 bfd_put_32 (input_bfd
,
2350 0x3c010000 | ((relocation
>> 16) & 0xffff), /* lui $at,XX */
2352 bfd_put_32 (input_bfd
,
2353 0x24210000 | (relocation
& 0xffff), /* addiu $at,$at,XX */
2355 bfd_put_32 (input_bfd
,
2356 (bfd_vma
) 0x003f0821, location
+ 12); /* addu $at,$at,$ra */
2357 bfd_put_32 (input_bfd
,
2358 (bfd_vma
) 0x0020f809, location
+ 16); /* jalr $at */
2363 /* Given a .sdata section and a .rel.sdata in-memory section, store
2364 relocation information into the .rel.sdata section which can be
2365 used at runtime to relocate the section. This is called by the
2366 linker when the --embedded-relocs switch is used. This is called
2367 after the add_symbols entry point has been called for all the
2368 objects, and before the final_link entry point is called. This
2369 function presumes that the object was compiled using
2373 bfd_mips_ecoff_create_embedded_relocs (abfd
, info
, datasec
, relsec
, errmsg
)
2375 struct bfd_link_info
*info
;
2380 struct ecoff_link_hash_entry
**sym_hashes
;
2381 struct ecoff_section_tdata
*section_tdata
;
2382 struct external_reloc
*ext_rel
;
2383 struct external_reloc
*ext_rel_end
;
2387 BFD_ASSERT (! info
->relocateable
);
2391 if (datasec
->reloc_count
== 0)
2394 sym_hashes
= ecoff_data (abfd
)->sym_hashes
;
2396 if (! mips_read_relocs (abfd
, datasec
))
2399 amt
= (bfd_size_type
) datasec
->reloc_count
* 4;
2400 relsec
->contents
= (bfd_byte
*) bfd_alloc (abfd
, amt
);
2401 if (relsec
->contents
== NULL
)
2404 p
= relsec
->contents
;
2406 section_tdata
= ecoff_section_data (abfd
, datasec
);
2407 ext_rel
= (struct external_reloc
*) section_tdata
->external_relocs
;
2408 ext_rel_end
= ext_rel
+ datasec
->reloc_count
;
2409 for (; ext_rel
< ext_rel_end
; ext_rel
++, p
+= 4)
2411 struct internal_reloc int_rel
;
2412 boolean text_relative
;
2414 mips_ecoff_swap_reloc_in (abfd
, (PTR
) ext_rel
, &int_rel
);
2416 /* We are going to write a four byte word into the runtime reloc
2417 section. The word will be the address in the data section
2418 which must be relocated. This must be on a word boundary,
2419 which means the lower two bits must be zero. We use the
2420 least significant bit to indicate how the value in the data
2421 section must be relocated. A 0 means that the value is
2422 relative to the text section, while a 1 indicates that the
2423 value is relative to the data section. Given that we are
2424 assuming the code was compiled using -membedded-pic, there
2425 should not be any other possibilities. */
2427 /* We can only relocate REFWORD relocs at run time. */
2428 if (int_rel
.r_type
!= MIPS_R_REFWORD
)
2430 *errmsg
= _("unsupported reloc type");
2431 bfd_set_error (bfd_error_bad_value
);
2435 if (int_rel
.r_extern
)
2437 struct ecoff_link_hash_entry
*h
;
2439 h
= sym_hashes
[int_rel
.r_symndx
];
2440 /* If h is NULL, that means that there is a reloc against an
2441 external symbol which we thought was just a debugging
2442 symbol. This should not happen. */
2443 if (h
== (struct ecoff_link_hash_entry
*) NULL
)
2445 if ((h
->root
.type
== bfd_link_hash_defined
2446 || h
->root
.type
== bfd_link_hash_defweak
)
2447 && (h
->root
.u
.def
.section
->flags
& SEC_CODE
) != 0)
2448 text_relative
= true;
2450 text_relative
= false;
2454 switch (int_rel
.r_symndx
)
2456 case RELOC_SECTION_TEXT
:
2457 text_relative
= true;
2459 case RELOC_SECTION_SDATA
:
2460 case RELOC_SECTION_SBSS
:
2461 case RELOC_SECTION_LIT8
:
2462 text_relative
= false;
2465 /* No other sections should appear in -membedded-pic
2467 *errmsg
= _("reloc against unsupported section");
2468 bfd_set_error (bfd_error_bad_value
);
2473 if ((int_rel
.r_offset
& 3) != 0)
2475 *errmsg
= _("reloc not properly aligned");
2476 bfd_set_error (bfd_error_bad_value
);
2481 (int_rel
.r_vaddr
- datasec
->vma
+ datasec
->output_offset
2482 + (text_relative
? 0 : 1)),
2489 /* This is the ECOFF backend structure. The backend field of the
2490 target vector points to this. */
2492 static const struct ecoff_backend_data mips_ecoff_backend_data
=
2494 /* COFF backend structure. */
2496 (void (*) PARAMS ((bfd
*,PTR
,int,int,int,int,PTR
))) bfd_void
, /* aux_in */
2497 (void (*) PARAMS ((bfd
*,PTR
,PTR
))) bfd_void
, /* sym_in */
2498 (void (*) PARAMS ((bfd
*,PTR
,PTR
))) bfd_void
, /* lineno_in */
2499 (unsigned (*) PARAMS ((bfd
*,PTR
,int,int,int,int,PTR
)))bfd_void
,/*aux_out*/
2500 (unsigned (*) PARAMS ((bfd
*,PTR
,PTR
))) bfd_void
, /* sym_out */
2501 (unsigned (*) PARAMS ((bfd
*,PTR
,PTR
))) bfd_void
, /* lineno_out */
2502 (unsigned (*) PARAMS ((bfd
*,PTR
,PTR
))) bfd_void
, /* reloc_out */
2503 mips_ecoff_swap_filehdr_out
, mips_ecoff_swap_aouthdr_out
,
2504 mips_ecoff_swap_scnhdr_out
,
2505 FILHSZ
, AOUTSZ
, SCNHSZ
, 0, 0, 0, 0, FILNMLEN
, true, false, 4, false, 2,
2506 mips_ecoff_swap_filehdr_in
, mips_ecoff_swap_aouthdr_in
,
2507 mips_ecoff_swap_scnhdr_in
, NULL
,
2508 mips_ecoff_bad_format_hook
, _bfd_ecoff_set_arch_mach_hook
,
2509 _bfd_ecoff_mkobject_hook
, _bfd_ecoff_styp_to_sec_flags
,
2510 _bfd_ecoff_set_alignment_hook
, _bfd_ecoff_slurp_symbol_table
,
2511 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
2514 /* Supported architecture. */
2516 /* Initial portion of armap string. */
2518 /* The page boundary used to align sections in a demand-paged
2519 executable file. E.g., 0x1000. */
2521 /* True if the .rdata section is part of the text segment, as on the
2522 Alpha. False if .rdata is part of the data segment, as on the
2525 /* Bitsize of constructor entries. */
2527 /* Reloc to use for constructor entries. */
2528 &mips_howto_table
[MIPS_R_REFWORD
],
2530 /* Symbol table magic number. */
2532 /* Alignment of debugging information. E.g., 4. */
2534 /* Sizes of external symbolic information. */
2535 sizeof (struct hdr_ext
),
2536 sizeof (struct dnr_ext
),
2537 sizeof (struct pdr_ext
),
2538 sizeof (struct sym_ext
),
2539 sizeof (struct opt_ext
),
2540 sizeof (struct fdr_ext
),
2541 sizeof (struct rfd_ext
),
2542 sizeof (struct ext_ext
),
2543 /* Functions to swap in external symbolic data. */
2552 _bfd_ecoff_swap_tir_in
,
2553 _bfd_ecoff_swap_rndx_in
,
2554 /* Functions to swap out external symbolic data. */
2563 _bfd_ecoff_swap_tir_out
,
2564 _bfd_ecoff_swap_rndx_out
,
2565 /* Function to read in symbolic data. */
2566 _bfd_ecoff_slurp_symbolic_info
2568 /* External reloc size. */
2570 /* Reloc swapping functions. */
2571 mips_ecoff_swap_reloc_in
,
2572 mips_ecoff_swap_reloc_out
,
2573 /* Backend reloc tweaking. */
2574 mips_adjust_reloc_in
,
2575 mips_adjust_reloc_out
,
2576 /* Relocate section contents while linking. */
2577 mips_relocate_section
,
2578 /* Do final adjustments to filehdr and aouthdr. */
2580 /* Read an element from an archive at a given file position. */
2581 _bfd_get_elt_at_filepos
2584 /* Looking up a reloc type is MIPS specific. */
2585 #define _bfd_ecoff_bfd_reloc_type_lookup mips_bfd_reloc_type_lookup
2587 /* Getting relocated section contents is generic. */
2588 #define _bfd_ecoff_bfd_get_relocated_section_contents \
2589 bfd_generic_get_relocated_section_contents
2591 /* Handling file windows is generic. */
2592 #define _bfd_ecoff_get_section_contents_in_window \
2593 _bfd_generic_get_section_contents_in_window
2595 /* Relaxing sections is MIPS specific. */
2596 #define _bfd_ecoff_bfd_relax_section mips_relax_section
2598 /* GC of sections is not done. */
2599 #define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections
2601 /* Merging of sections is not done. */
2602 #define _bfd_ecoff_bfd_merge_sections bfd_generic_merge_sections
2604 #define _bfd_ecoff_bfd_discard_group bfd_generic_discard_group
2606 extern const bfd_target ecoff_big_vec
;
2608 const bfd_target ecoff_little_vec
=
2610 "ecoff-littlemips", /* name */
2611 bfd_target_ecoff_flavour
,
2612 BFD_ENDIAN_LITTLE
, /* data byte order is little */
2613 BFD_ENDIAN_LITTLE
, /* header byte order is little */
2615 (HAS_RELOC
| EXEC_P
| /* object flags */
2616 HAS_LINENO
| HAS_DEBUG
|
2617 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2619 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_CODE
| SEC_DATA
),
2620 0, /* leading underscore */
2621 ' ', /* ar_pad_char */
2622 15, /* ar_max_namelen */
2623 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2624 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2625 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
2626 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2627 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2628 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
2630 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2631 _bfd_ecoff_archive_p
, _bfd_dummy_target
},
2632 {bfd_false
, _bfd_ecoff_mkobject
, /* bfd_set_format */
2633 _bfd_generic_mkarchive
, bfd_false
},
2634 {bfd_false
, _bfd_ecoff_write_object_contents
, /* bfd_write_contents */
2635 _bfd_write_archive_contents
, bfd_false
},
2637 BFD_JUMP_TABLE_GENERIC (_bfd_ecoff
),
2638 BFD_JUMP_TABLE_COPY (_bfd_ecoff
),
2639 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2640 BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff
),
2641 BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff
),
2642 BFD_JUMP_TABLE_RELOCS (_bfd_ecoff
),
2643 BFD_JUMP_TABLE_WRITE (_bfd_ecoff
),
2644 BFD_JUMP_TABLE_LINK (_bfd_ecoff
),
2645 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2649 (PTR
) &mips_ecoff_backend_data
2652 const bfd_target ecoff_big_vec
=
2654 "ecoff-bigmips", /* name */
2655 bfd_target_ecoff_flavour
,
2656 BFD_ENDIAN_BIG
, /* data byte order is big */
2657 BFD_ENDIAN_BIG
, /* header byte order is big */
2659 (HAS_RELOC
| EXEC_P
| /* object flags */
2660 HAS_LINENO
| HAS_DEBUG
|
2661 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2663 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_CODE
| SEC_DATA
),
2664 0, /* leading underscore */
2665 ' ', /* ar_pad_char */
2666 15, /* ar_max_namelen */
2667 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2668 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2669 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
,
2670 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2671 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2672 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
,
2673 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2674 _bfd_ecoff_archive_p
, _bfd_dummy_target
},
2675 {bfd_false
, _bfd_ecoff_mkobject
, /* bfd_set_format */
2676 _bfd_generic_mkarchive
, bfd_false
},
2677 {bfd_false
, _bfd_ecoff_write_object_contents
, /* bfd_write_contents */
2678 _bfd_write_archive_contents
, bfd_false
},
2680 BFD_JUMP_TABLE_GENERIC (_bfd_ecoff
),
2681 BFD_JUMP_TABLE_COPY (_bfd_ecoff
),
2682 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2683 BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff
),
2684 BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff
),
2685 BFD_JUMP_TABLE_RELOCS (_bfd_ecoff
),
2686 BFD_JUMP_TABLE_WRITE (_bfd_ecoff
),
2687 BFD_JUMP_TABLE_LINK (_bfd_ecoff
),
2688 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2692 (PTR
) &mips_ecoff_backend_data
2695 const bfd_target ecoff_biglittle_vec
=
2697 "ecoff-biglittlemips", /* name */
2698 bfd_target_ecoff_flavour
,
2699 BFD_ENDIAN_LITTLE
, /* data byte order is little */
2700 BFD_ENDIAN_BIG
, /* header byte order is big */
2702 (HAS_RELOC
| EXEC_P
| /* object flags */
2703 HAS_LINENO
| HAS_DEBUG
|
2704 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2706 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_CODE
| SEC_DATA
),
2707 0, /* leading underscore */
2708 ' ', /* ar_pad_char */
2709 15, /* ar_max_namelen */
2710 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2711 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2712 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
2713 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2714 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2715 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
2717 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2718 _bfd_ecoff_archive_p
, _bfd_dummy_target
},
2719 {bfd_false
, _bfd_ecoff_mkobject
, /* bfd_set_format */
2720 _bfd_generic_mkarchive
, bfd_false
},
2721 {bfd_false
, _bfd_ecoff_write_object_contents
, /* bfd_write_contents */
2722 _bfd_write_archive_contents
, bfd_false
},
2724 BFD_JUMP_TABLE_GENERIC (_bfd_ecoff
),
2725 BFD_JUMP_TABLE_COPY (_bfd_ecoff
),
2726 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2727 BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff
),
2728 BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff
),
2729 BFD_JUMP_TABLE_RELOCS (_bfd_ecoff
),
2730 BFD_JUMP_TABLE_WRITE (_bfd_ecoff
),
2731 BFD_JUMP_TABLE_LINK (_bfd_ecoff
),
2732 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2736 (PTR
) &mips_ecoff_backend_data