| 1 | /* BFD back-end for MIPS Extended-Coff files. | 
|---|
| 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, | 
|---|
| 3 | 2000, 2001, 2002 | 
|---|
| 4 | Free Software Foundation, Inc. | 
|---|
| 5 | Original version by Per Bothner. | 
|---|
| 6 | Full support added by Ian Lance Taylor, ian@cygnus.com. | 
|---|
| 7 |  | 
|---|
| 8 | This file is part of BFD, the Binary File Descriptor library. | 
|---|
| 9 |  | 
|---|
| 10 | This program is free software; you can redistribute it and/or modify | 
|---|
| 11 | it under the terms of the GNU General Public License as published by | 
|---|
| 12 | the Free Software Foundation; either version 2 of the License, or | 
|---|
| 13 | (at your option) any later version. | 
|---|
| 14 |  | 
|---|
| 15 | This program is distributed in the hope that it will be useful, | 
|---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 18 | GNU General Public License for more details. | 
|---|
| 19 |  | 
|---|
| 20 | You should have received a copy of the GNU General Public License | 
|---|
| 21 | along with this program; if not, write to the Free Software | 
|---|
| 22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */ | 
|---|
| 23 |  | 
|---|
| 24 | #include "bfd.h" | 
|---|
| 25 | #include "sysdep.h" | 
|---|
| 26 | #include "bfdlink.h" | 
|---|
| 27 | #include "libbfd.h" | 
|---|
| 28 | #include "coff/internal.h" | 
|---|
| 29 | #include "coff/sym.h" | 
|---|
| 30 | #include "coff/symconst.h" | 
|---|
| 31 | #include "coff/ecoff.h" | 
|---|
| 32 | #include "coff/mips.h" | 
|---|
| 33 | #include "libcoff.h" | 
|---|
| 34 | #include "libecoff.h" | 
|---|
| 35 |  | 
|---|
| 36 |  | 
|---|
| 37 | /* Prototypes for static functions.  */ | 
|---|
| 38 |  | 
|---|
| 39 | static bfd_boolean mips_ecoff_bad_format_hook | 
|---|
| 40 | PARAMS ((bfd *abfd, PTR filehdr)); | 
|---|
| 41 | static void mips_ecoff_swap_reloc_in | 
|---|
| 42 | PARAMS ((bfd *, PTR, struct internal_reloc *)); | 
|---|
| 43 | static void mips_ecoff_swap_reloc_out | 
|---|
| 44 | PARAMS ((bfd *, const struct internal_reloc *, PTR)); | 
|---|
| 45 | static void mips_adjust_reloc_in | 
|---|
| 46 | PARAMS ((bfd *, const struct internal_reloc *, arelent *)); | 
|---|
| 47 | static void mips_adjust_reloc_out | 
|---|
| 48 | PARAMS ((bfd *, const arelent *, struct internal_reloc *)); | 
|---|
| 49 | static bfd_reloc_status_type mips_generic_reloc | 
|---|
| 50 | PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, | 
|---|
| 51 | asection *section, bfd *output_bfd, char **error)); | 
|---|
| 52 | static bfd_reloc_status_type mips_refhi_reloc | 
|---|
| 53 | PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, | 
|---|
| 54 | asection *section, bfd *output_bfd, char **error)); | 
|---|
| 55 | static bfd_reloc_status_type mips_reflo_reloc | 
|---|
| 56 | PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, | 
|---|
| 57 | asection *section, bfd *output_bfd, char **error)); | 
|---|
| 58 | static bfd_reloc_status_type mips_gprel_reloc | 
|---|
| 59 | PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, | 
|---|
| 60 | asection *section, bfd *output_bfd, char **error)); | 
|---|
| 61 | static bfd_reloc_status_type mips_relhi_reloc | 
|---|
| 62 | PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, | 
|---|
| 63 | asection *section, bfd *output_bfd, char **error)); | 
|---|
| 64 | static bfd_reloc_status_type mips_rello_reloc | 
|---|
| 65 | PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, | 
|---|
| 66 | asection *section, bfd *output_bfd, char **error)); | 
|---|
| 67 | static bfd_reloc_status_type mips_switch_reloc | 
|---|
| 68 | PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, | 
|---|
| 69 | asection *section, bfd *output_bfd, char **error)); | 
|---|
| 70 | static void mips_relocate_hi | 
|---|
| 71 | PARAMS ((struct internal_reloc *refhi, struct internal_reloc *reflo, | 
|---|
| 72 | bfd *input_bfd, asection *input_section, bfd_byte *contents, | 
|---|
| 73 | size_t adjust, bfd_vma relocation, bfd_boolean pcrel)); | 
|---|
| 74 | static bfd_boolean mips_relocate_section | 
|---|
| 75 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, PTR)); | 
|---|
| 76 | static bfd_boolean mips_read_relocs | 
|---|
| 77 | PARAMS ((bfd *, asection *)); | 
|---|
| 78 | static bfd_boolean mips_relax_section | 
|---|
| 79 | PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *)); | 
|---|
| 80 | static bfd_boolean mips_relax_pcrel16 | 
|---|
| 81 | PARAMS ((struct bfd_link_info *, bfd *, asection *, | 
|---|
| 82 | struct ecoff_link_hash_entry *, bfd_byte *, bfd_vma)); | 
|---|
| 83 | static reloc_howto_type *mips_bfd_reloc_type_lookup | 
|---|
| 84 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | 
|---|
| 85 |  | 
|---|
| 86 |  | 
|---|
| 87 | /* ECOFF has COFF sections, but the debugging information is stored in | 
|---|
| 88 | a completely different format.  ECOFF targets use some of the | 
|---|
| 89 | swapping routines from coffswap.h, and some of the generic COFF | 
|---|
| 90 | routines in coffgen.c, but, unlike the real COFF targets, do not | 
|---|
| 91 | use coffcode.h itself. | 
|---|
| 92 |  | 
|---|
| 93 | Get the generic COFF swapping routines, except for the reloc, | 
|---|
| 94 | symbol, and lineno ones.  Give them ECOFF names.  */ | 
|---|
| 95 | #define MIPSECOFF | 
|---|
| 96 | #define NO_COFF_RELOCS | 
|---|
| 97 | #define NO_COFF_SYMBOLS | 
|---|
| 98 | #define NO_COFF_LINENOS | 
|---|
| 99 | #define coff_swap_filehdr_in mips_ecoff_swap_filehdr_in | 
|---|
| 100 | #define coff_swap_filehdr_out mips_ecoff_swap_filehdr_out | 
|---|
| 101 | #define coff_swap_aouthdr_in mips_ecoff_swap_aouthdr_in | 
|---|
| 102 | #define coff_swap_aouthdr_out mips_ecoff_swap_aouthdr_out | 
|---|
| 103 | #define coff_swap_scnhdr_in mips_ecoff_swap_scnhdr_in | 
|---|
| 104 | #define coff_swap_scnhdr_out mips_ecoff_swap_scnhdr_out | 
|---|
| 105 | #include "coffswap.h" | 
|---|
| 106 |  | 
|---|
| 107 | /* Get the ECOFF swapping routines.  */ | 
|---|
| 108 | #define ECOFF_32 | 
|---|
| 109 | #include "ecoffswap.h" | 
|---|
| 110 |  | 
|---|
| 111 |  | 
|---|
| 112 | /* How to process the various relocs types.  */ | 
|---|
| 113 |  | 
|---|
| 114 | static reloc_howto_type mips_howto_table[] = | 
|---|
| 115 | { | 
|---|
| 116 | /* Reloc type 0 is ignored.  The reloc reading code ensures that | 
|---|
| 117 | this is a reference to the .abs section, which will cause | 
|---|
| 118 | bfd_perform_relocation to do nothing.  */ | 
|---|
| 119 | HOWTO (MIPS_R_IGNORE, /* type */ | 
|---|
| 120 | 0,                     /* rightshift */ | 
|---|
| 121 | 0,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 122 | 8,                     /* bitsize */ | 
|---|
| 123 | FALSE,                 /* pc_relative */ | 
|---|
| 124 | 0,                     /* bitpos */ | 
|---|
| 125 | complain_overflow_dont, /* complain_on_overflow */ | 
|---|
| 126 | 0,                     /* special_function */ | 
|---|
| 127 | "IGNORE",              /* name */ | 
|---|
| 128 | FALSE,                 /* partial_inplace */ | 
|---|
| 129 | 0,                     /* src_mask */ | 
|---|
| 130 | 0,                     /* dst_mask */ | 
|---|
| 131 | FALSE),                /* pcrel_offset */ | 
|---|
| 132 |  | 
|---|
| 133 | /* A 16 bit reference to a symbol, normally from a data section.  */ | 
|---|
| 134 | HOWTO (MIPS_R_REFHALF,        /* type */ | 
|---|
| 135 | 0,                     /* rightshift */ | 
|---|
| 136 | 1,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 137 | 16,                    /* bitsize */ | 
|---|
| 138 | FALSE,                 /* pc_relative */ | 
|---|
| 139 | 0,                     /* bitpos */ | 
|---|
| 140 | complain_overflow_bitfield, /* complain_on_overflow */ | 
|---|
| 141 | mips_generic_reloc,    /* special_function */ | 
|---|
| 142 | "REFHALF",             /* name */ | 
|---|
| 143 | TRUE,                  /* partial_inplace */ | 
|---|
| 144 | 0xffff,                /* src_mask */ | 
|---|
| 145 | 0xffff,                /* dst_mask */ | 
|---|
| 146 | FALSE),                /* pcrel_offset */ | 
|---|
| 147 |  | 
|---|
| 148 | /* A 32 bit reference to a symbol, normally from a data section.  */ | 
|---|
| 149 | HOWTO (MIPS_R_REFWORD,        /* type */ | 
|---|
| 150 | 0,                     /* rightshift */ | 
|---|
| 151 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 152 | 32,                    /* bitsize */ | 
|---|
| 153 | FALSE,                 /* pc_relative */ | 
|---|
| 154 | 0,                     /* bitpos */ | 
|---|
| 155 | complain_overflow_bitfield, /* complain_on_overflow */ | 
|---|
| 156 | mips_generic_reloc,    /* special_function */ | 
|---|
| 157 | "REFWORD",             /* name */ | 
|---|
| 158 | TRUE,                  /* partial_inplace */ | 
|---|
| 159 | 0xffffffff,            /* src_mask */ | 
|---|
| 160 | 0xffffffff,            /* dst_mask */ | 
|---|
| 161 | FALSE),                /* pcrel_offset */ | 
|---|
| 162 |  | 
|---|
| 163 | /* A 26 bit absolute jump address.  */ | 
|---|
| 164 | HOWTO (MIPS_R_JMPADDR,        /* type */ | 
|---|
| 165 | 2,                     /* rightshift */ | 
|---|
| 166 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 167 | 26,                    /* bitsize */ | 
|---|
| 168 | FALSE,                 /* pc_relative */ | 
|---|
| 169 | 0,                     /* bitpos */ | 
|---|
| 170 | complain_overflow_dont, /* complain_on_overflow */ | 
|---|
| 171 | /* This needs complex overflow | 
|---|
| 172 | detection, because the upper four | 
|---|
| 173 | bits must match the PC.  */ | 
|---|
| 174 | mips_generic_reloc,    /* special_function */ | 
|---|
| 175 | "JMPADDR",             /* name */ | 
|---|
| 176 | TRUE,                  /* partial_inplace */ | 
|---|
| 177 | 0x3ffffff,             /* src_mask */ | 
|---|
| 178 | 0x3ffffff,             /* dst_mask */ | 
|---|
| 179 | FALSE),                /* pcrel_offset */ | 
|---|
| 180 |  | 
|---|
| 181 | /* The high 16 bits of a symbol value.  Handled by the function | 
|---|
| 182 | mips_refhi_reloc.  */ | 
|---|
| 183 | HOWTO (MIPS_R_REFHI,          /* type */ | 
|---|
| 184 | 16,                    /* rightshift */ | 
|---|
| 185 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 186 | 16,                    /* bitsize */ | 
|---|
| 187 | FALSE,                 /* pc_relative */ | 
|---|
| 188 | 0,                     /* bitpos */ | 
|---|
| 189 | complain_overflow_bitfield, /* complain_on_overflow */ | 
|---|
| 190 | mips_refhi_reloc,      /* special_function */ | 
|---|
| 191 | "REFHI",               /* name */ | 
|---|
| 192 | TRUE,                  /* partial_inplace */ | 
|---|
| 193 | 0xffff,                /* src_mask */ | 
|---|
| 194 | 0xffff,                /* dst_mask */ | 
|---|
| 195 | FALSE),                /* pcrel_offset */ | 
|---|
| 196 |  | 
|---|
| 197 | /* The low 16 bits of a symbol value.  */ | 
|---|
| 198 | HOWTO (MIPS_R_REFLO,          /* type */ | 
|---|
| 199 | 0,                     /* rightshift */ | 
|---|
| 200 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 201 | 16,                    /* bitsize */ | 
|---|
| 202 | FALSE,                 /* pc_relative */ | 
|---|
| 203 | 0,                     /* bitpos */ | 
|---|
| 204 | complain_overflow_dont, /* complain_on_overflow */ | 
|---|
| 205 | mips_reflo_reloc,      /* special_function */ | 
|---|
| 206 | "REFLO",               /* name */ | 
|---|
| 207 | TRUE,                  /* partial_inplace */ | 
|---|
| 208 | 0xffff,                /* src_mask */ | 
|---|
| 209 | 0xffff,                /* dst_mask */ | 
|---|
| 210 | FALSE),                /* pcrel_offset */ | 
|---|
| 211 |  | 
|---|
| 212 | /* A reference to an offset from the gp register.  Handled by the | 
|---|
| 213 | function mips_gprel_reloc.  */ | 
|---|
| 214 | HOWTO (MIPS_R_GPREL,          /* type */ | 
|---|
| 215 | 0,                     /* rightshift */ | 
|---|
| 216 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 217 | 16,                    /* bitsize */ | 
|---|
| 218 | FALSE,                 /* pc_relative */ | 
|---|
| 219 | 0,                     /* bitpos */ | 
|---|
| 220 | complain_overflow_signed, /* complain_on_overflow */ | 
|---|
| 221 | mips_gprel_reloc,      /* special_function */ | 
|---|
| 222 | "GPREL",               /* name */ | 
|---|
| 223 | TRUE,                  /* partial_inplace */ | 
|---|
| 224 | 0xffff,                /* src_mask */ | 
|---|
| 225 | 0xffff,                /* dst_mask */ | 
|---|
| 226 | FALSE),                /* pcrel_offset */ | 
|---|
| 227 |  | 
|---|
| 228 | /* A reference to a literal using an offset from the gp register. | 
|---|
| 229 | Handled by the function mips_gprel_reloc.  */ | 
|---|
| 230 | HOWTO (MIPS_R_LITERAL,        /* type */ | 
|---|
| 231 | 0,                     /* rightshift */ | 
|---|
| 232 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 233 | 16,                    /* bitsize */ | 
|---|
| 234 | FALSE,                 /* pc_relative */ | 
|---|
| 235 | 0,                     /* bitpos */ | 
|---|
| 236 | complain_overflow_signed, /* complain_on_overflow */ | 
|---|
| 237 | mips_gprel_reloc,      /* special_function */ | 
|---|
| 238 | "LITERAL",             /* name */ | 
|---|
| 239 | TRUE,                  /* partial_inplace */ | 
|---|
| 240 | 0xffff,                /* src_mask */ | 
|---|
| 241 | 0xffff,                /* dst_mask */ | 
|---|
| 242 | FALSE),                /* pcrel_offset */ | 
|---|
| 243 |  | 
|---|
| 244 | EMPTY_HOWTO (8), | 
|---|
| 245 | EMPTY_HOWTO (9), | 
|---|
| 246 | EMPTY_HOWTO (10), | 
|---|
| 247 | EMPTY_HOWTO (11), | 
|---|
| 248 |  | 
|---|
| 249 | /* This reloc is a Cygnus extension used when generating position | 
|---|
| 250 | independent code for embedded systems.  It represents a 16 bit PC | 
|---|
| 251 | relative reloc rightshifted twice as used in the MIPS branch | 
|---|
| 252 | instructions.  */ | 
|---|
| 253 | HOWTO (MIPS_R_PCREL16,        /* type */ | 
|---|
| 254 | 2,                     /* rightshift */ | 
|---|
| 255 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 256 | 16,                    /* bitsize */ | 
|---|
| 257 | TRUE,                  /* pc_relative */ | 
|---|
| 258 | 0,                     /* bitpos */ | 
|---|
| 259 | complain_overflow_signed, /* complain_on_overflow */ | 
|---|
| 260 | mips_generic_reloc,    /* special_function */ | 
|---|
| 261 | "PCREL16",             /* name */ | 
|---|
| 262 | TRUE,                  /* partial_inplace */ | 
|---|
| 263 | 0xffff,                /* src_mask */ | 
|---|
| 264 | 0xffff,                /* dst_mask */ | 
|---|
| 265 | TRUE),                 /* pcrel_offset */ | 
|---|
| 266 |  | 
|---|
| 267 | /* This reloc is a Cygnus extension used when generating position | 
|---|
| 268 | independent code for embedded systems.  It represents the high 16 | 
|---|
| 269 | bits of a PC relative reloc.  The next reloc must be | 
|---|
| 270 | MIPS_R_RELLO, and the addend is formed from the addends of the | 
|---|
| 271 | two instructions, just as in MIPS_R_REFHI and MIPS_R_REFLO.  The | 
|---|
| 272 | final value is actually PC relative to the location of the | 
|---|
| 273 | MIPS_R_RELLO reloc, not the MIPS_R_RELHI reloc.  */ | 
|---|
| 274 | HOWTO (MIPS_R_RELHI,          /* type */ | 
|---|
| 275 | 16,                    /* rightshift */ | 
|---|
| 276 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 277 | 16,                    /* bitsize */ | 
|---|
| 278 | TRUE,                  /* pc_relative */ | 
|---|
| 279 | 0,                     /* bitpos */ | 
|---|
| 280 | complain_overflow_bitfield, /* complain_on_overflow */ | 
|---|
| 281 | mips_relhi_reloc,      /* special_function */ | 
|---|
| 282 | "RELHI",               /* name */ | 
|---|
| 283 | TRUE,                  /* partial_inplace */ | 
|---|
| 284 | 0xffff,                /* src_mask */ | 
|---|
| 285 | 0xffff,                /* dst_mask */ | 
|---|
| 286 | TRUE),                 /* pcrel_offset */ | 
|---|
| 287 |  | 
|---|
| 288 | /* This reloc is a Cygnus extension used when generating position | 
|---|
| 289 | independent code for embedded systems.  It represents the low 16 | 
|---|
| 290 | bits of a PC relative reloc.  */ | 
|---|
| 291 | HOWTO (MIPS_R_RELLO,          /* type */ | 
|---|
| 292 | 0,                     /* rightshift */ | 
|---|
| 293 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 294 | 16,                    /* bitsize */ | 
|---|
| 295 | TRUE,                  /* pc_relative */ | 
|---|
| 296 | 0,                     /* bitpos */ | 
|---|
| 297 | complain_overflow_dont, /* complain_on_overflow */ | 
|---|
| 298 | mips_rello_reloc,      /* special_function */ | 
|---|
| 299 | "RELLO",               /* name */ | 
|---|
| 300 | TRUE,                  /* partial_inplace */ | 
|---|
| 301 | 0xffff,                /* src_mask */ | 
|---|
| 302 | 0xffff,                /* dst_mask */ | 
|---|
| 303 | TRUE),                 /* pcrel_offset */ | 
|---|
| 304 |  | 
|---|
| 305 | EMPTY_HOWTO (15), | 
|---|
| 306 | EMPTY_HOWTO (16), | 
|---|
| 307 | EMPTY_HOWTO (17), | 
|---|
| 308 | EMPTY_HOWTO (18), | 
|---|
| 309 | EMPTY_HOWTO (19), | 
|---|
| 310 | EMPTY_HOWTO (20), | 
|---|
| 311 | EMPTY_HOWTO (21), | 
|---|
| 312 |  | 
|---|
| 313 | /* This reloc is a Cygnus extension used when generating position | 
|---|
| 314 | independent code for embedded systems.  It represents an entry in | 
|---|
| 315 | a switch table, which is the difference between two symbols in | 
|---|
| 316 | the .text section.  The symndx is actually the offset from the | 
|---|
| 317 | reloc address to the subtrahend.  See include/coff/mips.h for | 
|---|
| 318 | more details.  */ | 
|---|
| 319 | HOWTO (MIPS_R_SWITCH,         /* type */ | 
|---|
| 320 | 0,                     /* rightshift */ | 
|---|
| 321 | 2,                     /* size (0 = byte, 1 = short, 2 = long) */ | 
|---|
| 322 | 32,                    /* bitsize */ | 
|---|
| 323 | TRUE,                  /* pc_relative */ | 
|---|
| 324 | 0,                     /* bitpos */ | 
|---|
| 325 | complain_overflow_dont, /* complain_on_overflow */ | 
|---|
| 326 | mips_switch_reloc,     /* special_function */ | 
|---|
| 327 | "SWITCH",              /* name */ | 
|---|
| 328 | TRUE,                  /* partial_inplace */ | 
|---|
| 329 | 0xffffffff,            /* src_mask */ | 
|---|
| 330 | 0xffffffff,            /* dst_mask */ | 
|---|
| 331 | TRUE)                  /* pcrel_offset */ | 
|---|
| 332 | }; | 
|---|
| 333 |  | 
|---|
| 334 | #define MIPS_HOWTO_COUNT \ | 
|---|
| 335 | (sizeof mips_howto_table / sizeof mips_howto_table[0]) | 
|---|
| 336 |  | 
|---|
| 337 | /* When the linker is doing relaxing, it may change an external PCREL16 | 
|---|
| 338 | reloc.  This typically represents an instruction like | 
|---|
| 339 | bal foo | 
|---|
| 340 | We change it to | 
|---|
| 341 | .set  noreorder | 
|---|
| 342 | bal   $L1 | 
|---|
| 343 | lui   $at,%hi(foo - $L1) | 
|---|
| 344 | $L1: | 
|---|
| 345 | addiu $at,%lo(foo - $L1) | 
|---|
| 346 | addu  $at,$at,$31 | 
|---|
| 347 | jalr  $at | 
|---|
| 348 | PCREL16_EXPANSION_ADJUSTMENT is the number of bytes this changes the | 
|---|
| 349 | instruction by.  */ | 
|---|
| 350 |  | 
|---|
| 351 | #define PCREL16_EXPANSION_ADJUSTMENT (4 * 4) | 
|---|
| 352 |  | 
|---|
| 353 |  | 
|---|
| 354 | /* See whether the magic number matches.  */ | 
|---|
| 355 |  | 
|---|
| 356 | static bfd_boolean | 
|---|
| 357 | mips_ecoff_bad_format_hook (abfd, filehdr) | 
|---|
| 358 | bfd *abfd; | 
|---|
| 359 | PTR filehdr; | 
|---|
| 360 | { | 
|---|
| 361 | struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr; | 
|---|
| 362 |  | 
|---|
| 363 | switch (internal_f->f_magic) | 
|---|
| 364 | { | 
|---|
| 365 | case MIPS_MAGIC_1: | 
|---|
| 366 | /* I don't know what endianness this implies.  */ | 
|---|
| 367 | return TRUE; | 
|---|
| 368 |  | 
|---|
| 369 | case MIPS_MAGIC_BIG: | 
|---|
| 370 | case MIPS_MAGIC_BIG2: | 
|---|
| 371 | case MIPS_MAGIC_BIG3: | 
|---|
| 372 | return bfd_big_endian (abfd); | 
|---|
| 373 |  | 
|---|
| 374 | case MIPS_MAGIC_LITTLE: | 
|---|
| 375 | case MIPS_MAGIC_LITTLE2: | 
|---|
| 376 | case MIPS_MAGIC_LITTLE3: | 
|---|
| 377 | return bfd_little_endian (abfd); | 
|---|
| 378 |  | 
|---|
| 379 | default: | 
|---|
| 380 | return FALSE; | 
|---|
| 381 | } | 
|---|
| 382 | } | 
|---|
| 383 |  | 
|---|
| 384 |  | 
|---|
| 385 | /* Reloc handling.  MIPS ECOFF relocs are packed into 8 bytes in | 
|---|
| 386 | external form.  They use a bit which indicates whether the symbol | 
|---|
| 387 | is external.  */ | 
|---|
| 388 |  | 
|---|
| 389 | /* Swap a reloc in.  */ | 
|---|
| 390 |  | 
|---|
| 391 | static void | 
|---|
| 392 | mips_ecoff_swap_reloc_in (abfd, ext_ptr, intern) | 
|---|
| 393 | bfd *abfd; | 
|---|
| 394 | PTR ext_ptr; | 
|---|
| 395 | struct internal_reloc *intern; | 
|---|
| 396 | { | 
|---|
| 397 | const RELOC *ext = (RELOC *) ext_ptr; | 
|---|
| 398 |  | 
|---|
| 399 | intern->r_vaddr = H_GET_32 (abfd, ext->r_vaddr); | 
|---|
| 400 | if (bfd_header_big_endian (abfd)) | 
|---|
| 401 | { | 
|---|
| 402 | intern->r_symndx = (((int) ext->r_bits[0] | 
|---|
| 403 | << RELOC_BITS0_SYMNDX_SH_LEFT_BIG) | 
|---|
| 404 | | ((int) ext->r_bits[1] | 
|---|
| 405 | << RELOC_BITS1_SYMNDX_SH_LEFT_BIG) | 
|---|
| 406 | | ((int) ext->r_bits[2] | 
|---|
| 407 | << RELOC_BITS2_SYMNDX_SH_LEFT_BIG)); | 
|---|
| 408 | intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG) | 
|---|
| 409 | >> RELOC_BITS3_TYPE_SH_BIG); | 
|---|
| 410 | intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0; | 
|---|
| 411 | } | 
|---|
| 412 | else | 
|---|
| 413 | { | 
|---|
| 414 | intern->r_symndx = (((int) ext->r_bits[0] | 
|---|
| 415 | << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE) | 
|---|
| 416 | | ((int) ext->r_bits[1] | 
|---|
| 417 | << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE) | 
|---|
| 418 | | ((int) ext->r_bits[2] | 
|---|
| 419 | << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE)); | 
|---|
| 420 | intern->r_type = (((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE) | 
|---|
| 421 | >> RELOC_BITS3_TYPE_SH_LITTLE) | 
|---|
| 422 | | ((ext->r_bits[3] & RELOC_BITS3_TYPEHI_LITTLE) | 
|---|
| 423 | << RELOC_BITS3_TYPEHI_SH_LITTLE)); | 
|---|
| 424 | intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0; | 
|---|
| 425 | } | 
|---|
| 426 |  | 
|---|
| 427 | /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or | 
|---|
| 428 | MIPS_R_RELLO reloc, r_symndx is actually the offset from the | 
|---|
| 429 | reloc address to the base of the difference (see | 
|---|
| 430 | include/coff/mips.h for more details).  We copy symndx into the | 
|---|
| 431 | r_offset field so as not to confuse ecoff_slurp_reloc_table in | 
|---|
| 432 | ecoff.c.  In adjust_reloc_in we then copy r_offset into the reloc | 
|---|
| 433 | addend.  */ | 
|---|
| 434 | if (intern->r_type == MIPS_R_SWITCH | 
|---|
| 435 | || (! intern->r_extern | 
|---|
| 436 | && (intern->r_type == MIPS_R_RELLO | 
|---|
| 437 | || intern->r_type == MIPS_R_RELHI))) | 
|---|
| 438 | { | 
|---|
| 439 | BFD_ASSERT (! intern->r_extern); | 
|---|
| 440 | intern->r_offset = intern->r_symndx; | 
|---|
| 441 | if (intern->r_offset & 0x800000) | 
|---|
| 442 | intern->r_offset -= 0x1000000; | 
|---|
| 443 | intern->r_symndx = RELOC_SECTION_TEXT; | 
|---|
| 444 | } | 
|---|
| 445 | } | 
|---|
| 446 |  | 
|---|
| 447 | /* Swap a reloc out.  */ | 
|---|
| 448 |  | 
|---|
| 449 | static void | 
|---|
| 450 | mips_ecoff_swap_reloc_out (abfd, intern, dst) | 
|---|
| 451 | bfd *abfd; | 
|---|
| 452 | const struct internal_reloc *intern; | 
|---|
| 453 | PTR dst; | 
|---|
| 454 | { | 
|---|
| 455 | RELOC *ext = (RELOC *) dst; | 
|---|
| 456 | long r_symndx; | 
|---|
| 457 |  | 
|---|
| 458 | BFD_ASSERT (intern->r_extern | 
|---|
| 459 | || (intern->r_symndx >= 0 && intern->r_symndx <= 12)); | 
|---|
| 460 |  | 
|---|
| 461 | /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELLO or | 
|---|
| 462 | MIPS_R_RELHI reloc, we actually want to write the contents of | 
|---|
| 463 | r_offset out as the symbol index.  This undoes the change made by | 
|---|
| 464 | mips_ecoff_swap_reloc_in.  */ | 
|---|
| 465 | if (intern->r_type != MIPS_R_SWITCH | 
|---|
| 466 | && (intern->r_extern | 
|---|
| 467 | || (intern->r_type != MIPS_R_RELHI | 
|---|
| 468 | && intern->r_type != MIPS_R_RELLO))) | 
|---|
| 469 | r_symndx = intern->r_symndx; | 
|---|
| 470 | else | 
|---|
| 471 | { | 
|---|
| 472 | BFD_ASSERT (intern->r_symndx == RELOC_SECTION_TEXT); | 
|---|
| 473 | r_symndx = intern->r_offset & 0xffffff; | 
|---|
| 474 | } | 
|---|
| 475 |  | 
|---|
| 476 | H_PUT_32 (abfd, intern->r_vaddr, ext->r_vaddr); | 
|---|
| 477 | if (bfd_header_big_endian (abfd)) | 
|---|
| 478 | { | 
|---|
| 479 | ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG; | 
|---|
| 480 | ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG; | 
|---|
| 481 | ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG; | 
|---|
| 482 | ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG) | 
|---|
| 483 | & RELOC_BITS3_TYPE_BIG) | 
|---|
| 484 | | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0)); | 
|---|
| 485 | } | 
|---|
| 486 | else | 
|---|
| 487 | { | 
|---|
| 488 | ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE; | 
|---|
| 489 | ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE; | 
|---|
| 490 | ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE; | 
|---|
| 491 | ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE) | 
|---|
| 492 | & RELOC_BITS3_TYPE_LITTLE) | 
|---|
| 493 | | ((intern->r_type >> RELOC_BITS3_TYPEHI_SH_LITTLE | 
|---|
| 494 | & RELOC_BITS3_TYPEHI_LITTLE)) | 
|---|
| 495 | | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0)); | 
|---|
| 496 | } | 
|---|
| 497 | } | 
|---|
| 498 |  | 
|---|
| 499 | /* Finish canonicalizing a reloc.  Part of this is generic to all | 
|---|
| 500 | ECOFF targets, and that part is in ecoff.c.  The rest is done in | 
|---|
| 501 | this backend routine.  It must fill in the howto field.  */ | 
|---|
| 502 |  | 
|---|
| 503 | static void | 
|---|
| 504 | mips_adjust_reloc_in (abfd, intern, rptr) | 
|---|
| 505 | bfd *abfd; | 
|---|
| 506 | const struct internal_reloc *intern; | 
|---|
| 507 | arelent *rptr; | 
|---|
| 508 | { | 
|---|
| 509 | if (intern->r_type > MIPS_R_SWITCH) | 
|---|
| 510 | abort (); | 
|---|
| 511 |  | 
|---|
| 512 | if (! intern->r_extern | 
|---|
| 513 | && (intern->r_type == MIPS_R_GPREL | 
|---|
| 514 | || intern->r_type == MIPS_R_LITERAL)) | 
|---|
| 515 | rptr->addend += ecoff_data (abfd)->gp; | 
|---|
| 516 |  | 
|---|
| 517 | /* If the type is MIPS_R_IGNORE, make sure this is a reference to | 
|---|
| 518 | the absolute section so that the reloc is ignored.  */ | 
|---|
| 519 | if (intern->r_type == MIPS_R_IGNORE) | 
|---|
| 520 | rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; | 
|---|
| 521 |  | 
|---|
| 522 | /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or | 
|---|
| 523 | MIPS_R_RELLO reloc, we want the addend field of the BFD relocto | 
|---|
| 524 | hold the value which was originally in the symndx field of the | 
|---|
| 525 | internal MIPS ECOFF reloc.  This value was copied into | 
|---|
| 526 | intern->r_offset by mips_swap_reloc_in, and here we copy it into | 
|---|
| 527 | the addend field.  */ | 
|---|
| 528 | if (intern->r_type == MIPS_R_SWITCH | 
|---|
| 529 | || (! intern->r_extern | 
|---|
| 530 | && (intern->r_type == MIPS_R_RELHI | 
|---|
| 531 | || intern->r_type == MIPS_R_RELLO))) | 
|---|
| 532 | rptr->addend = intern->r_offset; | 
|---|
| 533 |  | 
|---|
| 534 | rptr->howto = &mips_howto_table[intern->r_type]; | 
|---|
| 535 | } | 
|---|
| 536 |  | 
|---|
| 537 | /* Make any adjustments needed to a reloc before writing it out.  None | 
|---|
| 538 | are needed for MIPS.  */ | 
|---|
| 539 |  | 
|---|
| 540 | static void | 
|---|
| 541 | mips_adjust_reloc_out (abfd, rel, intern) | 
|---|
| 542 | bfd *abfd ATTRIBUTE_UNUSED; | 
|---|
| 543 | const arelent *rel; | 
|---|
| 544 | struct internal_reloc *intern; | 
|---|
| 545 | { | 
|---|
| 546 | /* For a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or | 
|---|
| 547 | MIPS_R_RELLO reloc, we must copy rel->addend into | 
|---|
| 548 | intern->r_offset.  This will then be written out as the symbol | 
|---|
| 549 | index by mips_ecoff_swap_reloc_out.  This operation parallels the | 
|---|
| 550 | action of mips_adjust_reloc_in.  */ | 
|---|
| 551 | if (intern->r_type == MIPS_R_SWITCH | 
|---|
| 552 | || (! intern->r_extern | 
|---|
| 553 | && (intern->r_type == MIPS_R_RELHI | 
|---|
| 554 | || intern->r_type == MIPS_R_RELLO))) | 
|---|
| 555 | intern->r_offset = rel->addend; | 
|---|
| 556 | } | 
|---|
| 557 |  | 
|---|
| 558 | /* ECOFF relocs are either against external symbols, or against | 
|---|
| 559 | sections.  If we are producing relocateable output, and the reloc | 
|---|
| 560 | is against an external symbol, and nothing has given us any | 
|---|
| 561 | additional addend, the resulting reloc will also be against the | 
|---|
| 562 | same symbol.  In such a case, we don't want to change anything | 
|---|
| 563 | about the way the reloc is handled, since it will all be done at | 
|---|
| 564 | final link time.  Rather than put special case code into | 
|---|
| 565 | bfd_perform_relocation, all the reloc types use this howto | 
|---|
| 566 | function.  It just short circuits the reloc if producing | 
|---|
| 567 | relocateable output against an external symbol.  */ | 
|---|
| 568 |  | 
|---|
| 569 | static bfd_reloc_status_type | 
|---|
| 570 | mips_generic_reloc (abfd, | 
|---|
| 571 | reloc_entry, | 
|---|
| 572 | symbol, | 
|---|
| 573 | data, | 
|---|
| 574 | input_section, | 
|---|
| 575 | output_bfd, | 
|---|
| 576 | error_message) | 
|---|
| 577 | bfd *abfd ATTRIBUTE_UNUSED; | 
|---|
| 578 | arelent *reloc_entry; | 
|---|
| 579 | asymbol *symbol; | 
|---|
| 580 | PTR data ATTRIBUTE_UNUSED; | 
|---|
| 581 | asection *input_section; | 
|---|
| 582 | bfd *output_bfd; | 
|---|
| 583 | char **error_message ATTRIBUTE_UNUSED; | 
|---|
| 584 | { | 
|---|
| 585 | if (output_bfd != (bfd *) NULL | 
|---|
| 586 | && (symbol->flags & BSF_SECTION_SYM) == 0 | 
|---|
| 587 | && reloc_entry->addend == 0) | 
|---|
| 588 | { | 
|---|
| 589 | reloc_entry->address += input_section->output_offset; | 
|---|
| 590 | return bfd_reloc_ok; | 
|---|
| 591 | } | 
|---|
| 592 |  | 
|---|
| 593 | return bfd_reloc_continue; | 
|---|
| 594 | } | 
|---|
| 595 |  | 
|---|
| 596 | /* Do a REFHI relocation.  This has to be done in combination with a | 
|---|
| 597 | REFLO reloc, because there is a carry from the REFLO to the REFHI. | 
|---|
| 598 | Here we just save the information we need; we do the actual | 
|---|
| 599 | relocation when we see the REFLO.  MIPS ECOFF requires that the | 
|---|
| 600 | REFLO immediately follow the REFHI.  As a GNU extension, we permit | 
|---|
| 601 | an arbitrary number of HI relocs to be associated with a single LO | 
|---|
| 602 | reloc.  This extension permits gcc to output the HI and LO relocs | 
|---|
| 603 | itself.  */ | 
|---|
| 604 |  | 
|---|
| 605 | struct mips_hi | 
|---|
| 606 | { | 
|---|
| 607 | struct mips_hi *next; | 
|---|
| 608 | bfd_byte *addr; | 
|---|
| 609 | bfd_vma addend; | 
|---|
| 610 | }; | 
|---|
| 611 |  | 
|---|
| 612 | /* FIXME: This should not be a static variable.  */ | 
|---|
| 613 |  | 
|---|
| 614 | static struct mips_hi *mips_refhi_list; | 
|---|
| 615 |  | 
|---|
| 616 | static bfd_reloc_status_type | 
|---|
| 617 | mips_refhi_reloc (abfd, | 
|---|
| 618 | reloc_entry, | 
|---|
| 619 | symbol, | 
|---|
| 620 | data, | 
|---|
| 621 | input_section, | 
|---|
| 622 | output_bfd, | 
|---|
| 623 | error_message) | 
|---|
| 624 | bfd *abfd ATTRIBUTE_UNUSED; | 
|---|
| 625 | arelent *reloc_entry; | 
|---|
| 626 | asymbol *symbol; | 
|---|
| 627 | PTR data; | 
|---|
| 628 | asection *input_section; | 
|---|
| 629 | bfd *output_bfd; | 
|---|
| 630 | char **error_message ATTRIBUTE_UNUSED; | 
|---|
| 631 | { | 
|---|
| 632 | bfd_reloc_status_type ret; | 
|---|
| 633 | bfd_vma relocation; | 
|---|
| 634 | struct mips_hi *n; | 
|---|
| 635 |  | 
|---|
| 636 | /* If we're relocating, and this an external symbol, we don't want | 
|---|
| 637 | to change anything.  */ | 
|---|
| 638 | if (output_bfd != (bfd *) NULL | 
|---|
| 639 | && (symbol->flags & BSF_SECTION_SYM) == 0 | 
|---|
| 640 | && reloc_entry->addend == 0) | 
|---|
| 641 | { | 
|---|
| 642 | reloc_entry->address += input_section->output_offset; | 
|---|
| 643 | return bfd_reloc_ok; | 
|---|
| 644 | } | 
|---|
| 645 |  | 
|---|
| 646 | ret = bfd_reloc_ok; | 
|---|
| 647 | if (bfd_is_und_section (symbol->section) | 
|---|
| 648 | && output_bfd == (bfd *) NULL) | 
|---|
| 649 | ret = bfd_reloc_undefined; | 
|---|
| 650 |  | 
|---|
| 651 | if (bfd_is_com_section (symbol->section)) | 
|---|
| 652 | relocation = 0; | 
|---|
| 653 | else | 
|---|
| 654 | relocation = symbol->value; | 
|---|
| 655 |  | 
|---|
| 656 | relocation += symbol->section->output_section->vma; | 
|---|
| 657 | relocation += symbol->section->output_offset; | 
|---|
| 658 | relocation += reloc_entry->addend; | 
|---|
| 659 |  | 
|---|
| 660 | if (reloc_entry->address > input_section->_cooked_size) | 
|---|
| 661 | return bfd_reloc_outofrange; | 
|---|
| 662 |  | 
|---|
| 663 | /* Save the information, and let REFLO do the actual relocation.  */ | 
|---|
| 664 | n = (struct mips_hi *) bfd_malloc ((bfd_size_type) sizeof *n); | 
|---|
| 665 | if (n == NULL) | 
|---|
| 666 | return bfd_reloc_outofrange; | 
|---|
| 667 | n->addr = (bfd_byte *) data + reloc_entry->address; | 
|---|
| 668 | n->addend = relocation; | 
|---|
| 669 | n->next = mips_refhi_list; | 
|---|
| 670 | mips_refhi_list = n; | 
|---|
| 671 |  | 
|---|
| 672 | if (output_bfd != (bfd *) NULL) | 
|---|
| 673 | reloc_entry->address += input_section->output_offset; | 
|---|
| 674 |  | 
|---|
| 675 | return ret; | 
|---|
| 676 | } | 
|---|
| 677 |  | 
|---|
| 678 | /* Do a REFLO relocation.  This is a straightforward 16 bit inplace | 
|---|
| 679 | relocation; this function exists in order to do the REFHI | 
|---|
| 680 | relocation described above.  */ | 
|---|
| 681 |  | 
|---|
| 682 | static bfd_reloc_status_type | 
|---|
| 683 | mips_reflo_reloc (abfd, | 
|---|
| 684 | reloc_entry, | 
|---|
| 685 | symbol, | 
|---|
| 686 | data, | 
|---|
| 687 | input_section, | 
|---|
| 688 | output_bfd, | 
|---|
| 689 | error_message) | 
|---|
| 690 | bfd *abfd; | 
|---|
| 691 | arelent *reloc_entry; | 
|---|
| 692 | asymbol *symbol; | 
|---|
| 693 | PTR data; | 
|---|
| 694 | asection *input_section; | 
|---|
| 695 | bfd *output_bfd; | 
|---|
| 696 | char **error_message; | 
|---|
| 697 | { | 
|---|
| 698 | if (mips_refhi_list != NULL) | 
|---|
| 699 | { | 
|---|
| 700 | struct mips_hi *l; | 
|---|
| 701 |  | 
|---|
| 702 | l = mips_refhi_list; | 
|---|
| 703 | while (l != NULL) | 
|---|
| 704 | { | 
|---|
| 705 | unsigned long insn; | 
|---|
| 706 | unsigned long val; | 
|---|
| 707 | unsigned long vallo; | 
|---|
| 708 | struct mips_hi *next; | 
|---|
| 709 |  | 
|---|
| 710 | /* Do the REFHI relocation.  Note that we actually don't | 
|---|
| 711 | need to know anything about the REFLO itself, except | 
|---|
| 712 | where to find the low 16 bits of the addend needed by the | 
|---|
| 713 | REFHI.  */ | 
|---|
| 714 | insn = bfd_get_32 (abfd, l->addr); | 
|---|
| 715 | vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address) | 
|---|
| 716 | & 0xffff); | 
|---|
| 717 | val = ((insn & 0xffff) << 16) + vallo; | 
|---|
| 718 | val += l->addend; | 
|---|
| 719 |  | 
|---|
| 720 | /* The low order 16 bits are always treated as a signed | 
|---|
| 721 | value.  Therefore, a negative value in the low order bits | 
|---|
| 722 | requires an adjustment in the high order bits.  We need | 
|---|
| 723 | to make this adjustment in two ways: once for the bits we | 
|---|
| 724 | took from the data, and once for the bits we are putting | 
|---|
| 725 | back in to the data.  */ | 
|---|
| 726 | if ((vallo & 0x8000) != 0) | 
|---|
| 727 | val -= 0x10000; | 
|---|
| 728 | if ((val & 0x8000) != 0) | 
|---|
| 729 | val += 0x10000; | 
|---|
| 730 |  | 
|---|
| 731 | insn = (insn &~ (unsigned) 0xffff) | ((val >> 16) & 0xffff); | 
|---|
| 732 | bfd_put_32 (abfd, (bfd_vma) insn, l->addr); | 
|---|
| 733 |  | 
|---|
| 734 | next = l->next; | 
|---|
| 735 | free (l); | 
|---|
| 736 | l = next; | 
|---|
| 737 | } | 
|---|
| 738 |  | 
|---|
| 739 | mips_refhi_list = NULL; | 
|---|
| 740 | } | 
|---|
| 741 |  | 
|---|
| 742 | /* Now do the REFLO reloc in the usual way.  */ | 
|---|
| 743 | return mips_generic_reloc (abfd, reloc_entry, symbol, data, | 
|---|
| 744 | input_section, output_bfd, error_message); | 
|---|
| 745 | } | 
|---|
| 746 |  | 
|---|
| 747 | /* Do a GPREL relocation.  This is a 16 bit value which must become | 
|---|
| 748 | the offset from the gp register.  */ | 
|---|
| 749 |  | 
|---|
| 750 | static bfd_reloc_status_type | 
|---|
| 751 | mips_gprel_reloc (abfd, | 
|---|
| 752 | reloc_entry, | 
|---|
| 753 | symbol, | 
|---|
| 754 | data, | 
|---|
| 755 | input_section, | 
|---|
| 756 | output_bfd, | 
|---|
| 757 | error_message) | 
|---|
| 758 | bfd *abfd; | 
|---|
| 759 | arelent *reloc_entry; | 
|---|
| 760 | asymbol *symbol; | 
|---|
| 761 | PTR data; | 
|---|
| 762 | asection *input_section; | 
|---|
| 763 | bfd *output_bfd; | 
|---|
| 764 | char **error_message; | 
|---|
| 765 | { | 
|---|
| 766 | bfd_boolean relocateable; | 
|---|
| 767 | bfd_vma gp; | 
|---|
| 768 | bfd_vma relocation; | 
|---|
| 769 | unsigned long val; | 
|---|
| 770 | unsigned long insn; | 
|---|
| 771 |  | 
|---|
| 772 | /* If we're relocating, and this is an external symbol with no | 
|---|
| 773 | addend, we don't want to change anything.  We will only have an | 
|---|
| 774 | addend if this is a newly created reloc, not read from an ECOFF | 
|---|
| 775 | file.  */ | 
|---|
| 776 | if (output_bfd != (bfd *) NULL | 
|---|
| 777 | && (symbol->flags & BSF_SECTION_SYM) == 0 | 
|---|
| 778 | && reloc_entry->addend == 0) | 
|---|
| 779 | { | 
|---|
| 780 | reloc_entry->address += input_section->output_offset; | 
|---|
| 781 | return bfd_reloc_ok; | 
|---|
| 782 | } | 
|---|
| 783 |  | 
|---|
| 784 | if (output_bfd != (bfd *) NULL) | 
|---|
| 785 | relocateable = TRUE; | 
|---|
| 786 | else | 
|---|
| 787 | { | 
|---|
| 788 | relocateable = FALSE; | 
|---|
| 789 | output_bfd = symbol->section->output_section->owner; | 
|---|
| 790 | } | 
|---|
| 791 |  | 
|---|
| 792 | if (bfd_is_und_section (symbol->section) && ! relocateable) | 
|---|
| 793 | return bfd_reloc_undefined; | 
|---|
| 794 |  | 
|---|
| 795 | /* We have to figure out the gp value, so that we can adjust the | 
|---|
| 796 | symbol value correctly.  We look up the symbol _gp in the output | 
|---|
| 797 | BFD.  If we can't find it, we're stuck.  We cache it in the ECOFF | 
|---|
| 798 | target data.  We don't need to adjust the symbol value for an | 
|---|
| 799 | external symbol if we are producing relocateable output.  */ | 
|---|
| 800 | gp = _bfd_get_gp_value (output_bfd); | 
|---|
| 801 | if (gp == 0 | 
|---|
| 802 | && (! relocateable | 
|---|
| 803 | || (symbol->flags & BSF_SECTION_SYM) != 0)) | 
|---|
| 804 | { | 
|---|
| 805 | if (relocateable) | 
|---|
| 806 | { | 
|---|
| 807 | /* Make up a value.  */ | 
|---|
| 808 | gp = symbol->section->output_section->vma + 0x4000; | 
|---|
| 809 | _bfd_set_gp_value (output_bfd, gp); | 
|---|
| 810 | } | 
|---|
| 811 | else | 
|---|
| 812 | { | 
|---|
| 813 | unsigned int count; | 
|---|
| 814 | asymbol **sym; | 
|---|
| 815 | unsigned int i; | 
|---|
| 816 |  | 
|---|
| 817 | count = bfd_get_symcount (output_bfd); | 
|---|
| 818 | sym = bfd_get_outsymbols (output_bfd); | 
|---|
| 819 |  | 
|---|
| 820 | if (sym == (asymbol **) NULL) | 
|---|
| 821 | i = count; | 
|---|
| 822 | else | 
|---|
| 823 | { | 
|---|
| 824 | for (i = 0; i < count; i++, sym++) | 
|---|
| 825 | { | 
|---|
| 826 | register const char *name; | 
|---|
| 827 |  | 
|---|
| 828 | name = bfd_asymbol_name (*sym); | 
|---|
| 829 | if (*name == '_' && strcmp (name, "_gp") == 0) | 
|---|
| 830 | { | 
|---|
| 831 | gp = bfd_asymbol_value (*sym); | 
|---|
| 832 | _bfd_set_gp_value (output_bfd, gp); | 
|---|
| 833 | break; | 
|---|
| 834 | } | 
|---|
| 835 | } | 
|---|
| 836 | } | 
|---|
| 837 |  | 
|---|
| 838 | if (i >= count) | 
|---|
| 839 | { | 
|---|
| 840 | /* Only get the error once.  */ | 
|---|
| 841 | gp = 4; | 
|---|
| 842 | _bfd_set_gp_value (output_bfd, gp); | 
|---|
| 843 | *error_message = | 
|---|
| 844 | (char *) _("GP relative relocation when _gp not defined"); | 
|---|
| 845 | return bfd_reloc_dangerous; | 
|---|
| 846 | } | 
|---|
| 847 | } | 
|---|
| 848 | } | 
|---|
| 849 |  | 
|---|
| 850 | if (bfd_is_com_section (symbol->section)) | 
|---|
| 851 | relocation = 0; | 
|---|
| 852 | else | 
|---|
| 853 | relocation = symbol->value; | 
|---|
| 854 |  | 
|---|
| 855 | relocation += symbol->section->output_section->vma; | 
|---|
| 856 | relocation += symbol->section->output_offset; | 
|---|
| 857 |  | 
|---|
| 858 | if (reloc_entry->address > input_section->_cooked_size) | 
|---|
| 859 | return bfd_reloc_outofrange; | 
|---|
| 860 |  | 
|---|
| 861 | insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address); | 
|---|
| 862 |  | 
|---|
| 863 | /* Set val to the offset into the section or symbol.  */ | 
|---|
| 864 | val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff; | 
|---|
| 865 | if (val & 0x8000) | 
|---|
| 866 | val -= 0x10000; | 
|---|
| 867 |  | 
|---|
| 868 | /* Adjust val for the final section location and GP value.  If we | 
|---|
| 869 | are producing relocateable output, we don't want to do this for | 
|---|
| 870 | an external symbol.  */ | 
|---|
| 871 | if (! relocateable | 
|---|
| 872 | || (symbol->flags & BSF_SECTION_SYM) != 0) | 
|---|
| 873 | val += relocation - gp; | 
|---|
| 874 |  | 
|---|
| 875 | insn = (insn &~ (unsigned) 0xffff) | (val & 0xffff); | 
|---|
| 876 | bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + reloc_entry->address); | 
|---|
| 877 |  | 
|---|
| 878 | if (relocateable) | 
|---|
| 879 | reloc_entry->address += input_section->output_offset; | 
|---|
| 880 |  | 
|---|
| 881 | /* Make sure it fit in 16 bits.  */ | 
|---|
| 882 | if ((long) val >= 0x8000 || (long) val < -0x8000) | 
|---|
| 883 | return bfd_reloc_overflow; | 
|---|
| 884 |  | 
|---|
| 885 | return bfd_reloc_ok; | 
|---|
| 886 | } | 
|---|
| 887 |  | 
|---|
| 888 | /* Do a RELHI relocation.  We do this in conjunction with a RELLO | 
|---|
| 889 | reloc, just as REFHI and REFLO are done together.  RELHI and RELLO | 
|---|
| 890 | are Cygnus extensions used when generating position independent | 
|---|
| 891 | code for embedded systems.  */ | 
|---|
| 892 |  | 
|---|
| 893 | /* FIXME: This should not be a static variable.  */ | 
|---|
| 894 |  | 
|---|
| 895 | static struct mips_hi *mips_relhi_list; | 
|---|
| 896 |  | 
|---|
| 897 | static bfd_reloc_status_type | 
|---|
| 898 | mips_relhi_reloc (abfd, | 
|---|
| 899 | reloc_entry, | 
|---|
| 900 | symbol, | 
|---|
| 901 | data, | 
|---|
| 902 | input_section, | 
|---|
| 903 | output_bfd, | 
|---|
| 904 | error_message) | 
|---|
| 905 | bfd *abfd ATTRIBUTE_UNUSED; | 
|---|
| 906 | arelent *reloc_entry; | 
|---|
| 907 | asymbol *symbol; | 
|---|
| 908 | PTR data; | 
|---|
| 909 | asection *input_section; | 
|---|
| 910 | bfd *output_bfd; | 
|---|
| 911 | char **error_message ATTRIBUTE_UNUSED; | 
|---|
| 912 | { | 
|---|
| 913 | bfd_reloc_status_type ret; | 
|---|
| 914 | bfd_vma relocation; | 
|---|
| 915 | struct mips_hi *n; | 
|---|
| 916 |  | 
|---|
| 917 | /* If this is a reloc against a section symbol, then it is correct | 
|---|
| 918 | in the object file.  The only time we want to change this case is | 
|---|
| 919 | when we are relaxing, and that is handled entirely by | 
|---|
| 920 | mips_relocate_section and never calls this function.  */ | 
|---|
| 921 | if ((symbol->flags & BSF_SECTION_SYM) != 0) | 
|---|
| 922 | { | 
|---|
| 923 | if (output_bfd != (bfd *) NULL) | 
|---|
| 924 | reloc_entry->address += input_section->output_offset; | 
|---|
| 925 | return bfd_reloc_ok; | 
|---|
| 926 | } | 
|---|
| 927 |  | 
|---|
| 928 | /* This is an external symbol.  If we're relocating, we don't want | 
|---|
| 929 | to change anything.  */ | 
|---|
| 930 | if (output_bfd != (bfd *) NULL) | 
|---|
| 931 | { | 
|---|
| 932 | reloc_entry->address += input_section->output_offset; | 
|---|
| 933 | return bfd_reloc_ok; | 
|---|
| 934 | } | 
|---|
| 935 |  | 
|---|
| 936 | ret = bfd_reloc_ok; | 
|---|
| 937 | if (bfd_is_und_section (symbol->section) | 
|---|
| 938 | && output_bfd == (bfd *) NULL) | 
|---|
| 939 | ret = bfd_reloc_undefined; | 
|---|
| 940 |  | 
|---|
| 941 | if (bfd_is_com_section (symbol->section)) | 
|---|
| 942 | relocation = 0; | 
|---|
| 943 | else | 
|---|
| 944 | relocation = symbol->value; | 
|---|
| 945 |  | 
|---|
| 946 | relocation += symbol->section->output_section->vma; | 
|---|
| 947 | relocation += symbol->section->output_offset; | 
|---|
| 948 | relocation += reloc_entry->addend; | 
|---|
| 949 |  | 
|---|
| 950 | if (reloc_entry->address > input_section->_cooked_size) | 
|---|
| 951 | return bfd_reloc_outofrange; | 
|---|
| 952 |  | 
|---|
| 953 | /* Save the information, and let RELLO do the actual relocation.  */ | 
|---|
| 954 | n = (struct mips_hi *) bfd_malloc ((bfd_size_type) sizeof *n); | 
|---|
| 955 | if (n == NULL) | 
|---|
| 956 | return bfd_reloc_outofrange; | 
|---|
| 957 | n->addr = (bfd_byte *) data + reloc_entry->address; | 
|---|
| 958 | n->addend = relocation; | 
|---|
| 959 | n->next = mips_relhi_list; | 
|---|
| 960 | mips_relhi_list = n; | 
|---|
| 961 |  | 
|---|
| 962 | if (output_bfd != (bfd *) NULL) | 
|---|
| 963 | reloc_entry->address += input_section->output_offset; | 
|---|
| 964 |  | 
|---|
| 965 | return ret; | 
|---|
| 966 | } | 
|---|
| 967 |  | 
|---|
| 968 | /* Do a RELLO relocation.  This is a straightforward 16 bit PC | 
|---|
| 969 | relative relocation; this function exists in order to do the RELHI | 
|---|
| 970 | relocation described above.  */ | 
|---|
| 971 |  | 
|---|
| 972 | static bfd_reloc_status_type | 
|---|
| 973 | mips_rello_reloc (abfd, | 
|---|
| 974 | reloc_entry, | 
|---|
| 975 | symbol, | 
|---|
| 976 | data, | 
|---|
| 977 | input_section, | 
|---|
| 978 | output_bfd, | 
|---|
| 979 | error_message) | 
|---|
| 980 | bfd *abfd; | 
|---|
| 981 | arelent *reloc_entry; | 
|---|
| 982 | asymbol *symbol; | 
|---|
| 983 | PTR data; | 
|---|
| 984 | asection *input_section; | 
|---|
| 985 | bfd *output_bfd; | 
|---|
| 986 | char **error_message; | 
|---|
| 987 | { | 
|---|
| 988 | if (mips_relhi_list != NULL) | 
|---|
| 989 | { | 
|---|
| 990 | struct mips_hi *l; | 
|---|
| 991 |  | 
|---|
| 992 | l = mips_relhi_list; | 
|---|
| 993 | while (l != NULL) | 
|---|
| 994 | { | 
|---|
| 995 | unsigned long insn; | 
|---|
| 996 | unsigned long val; | 
|---|
| 997 | unsigned long vallo; | 
|---|
| 998 | struct mips_hi *next; | 
|---|
| 999 |  | 
|---|
| 1000 | /* Do the RELHI relocation.  Note that we actually don't | 
|---|
| 1001 | need to know anything about the RELLO itself, except | 
|---|
| 1002 | where to find the low 16 bits of the addend needed by the | 
|---|
| 1003 | RELHI.  */ | 
|---|
| 1004 | insn = bfd_get_32 (abfd, l->addr); | 
|---|
| 1005 | vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address) | 
|---|
| 1006 | & 0xffff); | 
|---|
| 1007 | val = ((insn & 0xffff) << 16) + vallo; | 
|---|
| 1008 | val += l->addend; | 
|---|
| 1009 |  | 
|---|
| 1010 | /* If the symbol is defined, make val PC relative.  If the | 
|---|
| 1011 | symbol is not defined we don't want to do this, because | 
|---|
| 1012 | we don't want the value in the object file to incorporate | 
|---|
| 1013 | the address of the reloc.  */ | 
|---|
| 1014 | if (! bfd_is_und_section (bfd_get_section (symbol)) | 
|---|
| 1015 | && ! bfd_is_com_section (bfd_get_section (symbol))) | 
|---|
| 1016 | val -= (input_section->output_section->vma | 
|---|
| 1017 | + input_section->output_offset | 
|---|
| 1018 | + reloc_entry->address); | 
|---|
| 1019 |  | 
|---|
| 1020 | /* The low order 16 bits are always treated as a signed | 
|---|
| 1021 | value.  Therefore, a negative value in the low order bits | 
|---|
| 1022 | requires an adjustment in the high order bits.  We need | 
|---|
| 1023 | to make this adjustment in two ways: once for the bits we | 
|---|
| 1024 | took from the data, and once for the bits we are putting | 
|---|
| 1025 | back in to the data.  */ | 
|---|
| 1026 | if ((vallo & 0x8000) != 0) | 
|---|
| 1027 | val -= 0x10000; | 
|---|
| 1028 | if ((val & 0x8000) != 0) | 
|---|
| 1029 | val += 0x10000; | 
|---|
| 1030 |  | 
|---|
| 1031 | insn = (insn &~ (unsigned) 0xffff) | ((val >> 16) & 0xffff); | 
|---|
| 1032 | bfd_put_32 (abfd, (bfd_vma) insn, l->addr); | 
|---|
| 1033 |  | 
|---|
| 1034 | next = l->next; | 
|---|
| 1035 | free (l); | 
|---|
| 1036 | l = next; | 
|---|
| 1037 | } | 
|---|
| 1038 |  | 
|---|
| 1039 | mips_relhi_list = NULL; | 
|---|
| 1040 | } | 
|---|
| 1041 |  | 
|---|
| 1042 | /* If this is a reloc against a section symbol, then it is correct | 
|---|
| 1043 | in the object file.  The only time we want to change this case is | 
|---|
| 1044 | when we are relaxing, and that is handled entirely by | 
|---|
| 1045 | mips_relocate_section and never calls this function.  */ | 
|---|
| 1046 | if ((symbol->flags & BSF_SECTION_SYM) != 0) | 
|---|
| 1047 | { | 
|---|
| 1048 | if (output_bfd != (bfd *) NULL) | 
|---|
| 1049 | reloc_entry->address += input_section->output_offset; | 
|---|
| 1050 | return bfd_reloc_ok; | 
|---|
| 1051 | } | 
|---|
| 1052 |  | 
|---|
| 1053 | /* bfd_perform_relocation does not handle pcrel_offset relocations | 
|---|
| 1054 | correctly when generating a relocateable file, so handle them | 
|---|
| 1055 | directly here.  */ | 
|---|
| 1056 | if (output_bfd != (bfd *) NULL) | 
|---|
| 1057 | { | 
|---|
| 1058 | reloc_entry->address += input_section->output_offset; | 
|---|
| 1059 | return bfd_reloc_ok; | 
|---|
| 1060 | } | 
|---|
| 1061 |  | 
|---|
| 1062 | /* Now do the RELLO reloc in the usual way.  */ | 
|---|
| 1063 | return mips_generic_reloc (abfd, reloc_entry, symbol, data, | 
|---|
| 1064 | input_section, output_bfd, error_message); | 
|---|
| 1065 | } | 
|---|
| 1066 |  | 
|---|
| 1067 | /* This is the special function for the MIPS_R_SWITCH reloc.  This | 
|---|
| 1068 | special reloc is normally correct in the object file, and only | 
|---|
| 1069 | requires special handling when relaxing.  We don't want | 
|---|
| 1070 | bfd_perform_relocation to tamper with it at all.  */ | 
|---|
| 1071 |  | 
|---|
| 1072 | static bfd_reloc_status_type | 
|---|
| 1073 | mips_switch_reloc (abfd, | 
|---|
| 1074 | reloc_entry, | 
|---|
| 1075 | symbol, | 
|---|
| 1076 | data, | 
|---|
| 1077 | input_section, | 
|---|
| 1078 | output_bfd, | 
|---|
| 1079 | error_message) | 
|---|
| 1080 | bfd *abfd ATTRIBUTE_UNUSED; | 
|---|
| 1081 | arelent *reloc_entry ATTRIBUTE_UNUSED; | 
|---|
| 1082 | asymbol *symbol ATTRIBUTE_UNUSED; | 
|---|
| 1083 | PTR data ATTRIBUTE_UNUSED; | 
|---|
| 1084 | asection *input_section ATTRIBUTE_UNUSED; | 
|---|
| 1085 | bfd *output_bfd ATTRIBUTE_UNUSED; | 
|---|
| 1086 | char **error_message ATTRIBUTE_UNUSED; | 
|---|
| 1087 | { | 
|---|
| 1088 | return bfd_reloc_ok; | 
|---|
| 1089 | } | 
|---|
| 1090 |  | 
|---|
| 1091 | /* Get the howto structure for a generic reloc type.  */ | 
|---|
| 1092 |  | 
|---|
| 1093 | static reloc_howto_type * | 
|---|
| 1094 | mips_bfd_reloc_type_lookup (abfd, code) | 
|---|
| 1095 | bfd *abfd ATTRIBUTE_UNUSED; | 
|---|
| 1096 | bfd_reloc_code_real_type code; | 
|---|
| 1097 | { | 
|---|
| 1098 | int mips_type; | 
|---|
| 1099 |  | 
|---|
| 1100 | switch (code) | 
|---|
| 1101 | { | 
|---|
| 1102 | case BFD_RELOC_16: | 
|---|
| 1103 | mips_type = MIPS_R_REFHALF; | 
|---|
| 1104 | break; | 
|---|
| 1105 | case BFD_RELOC_32: | 
|---|
| 1106 | case BFD_RELOC_CTOR: | 
|---|
| 1107 | mips_type = MIPS_R_REFWORD; | 
|---|
| 1108 | break; | 
|---|
| 1109 | case BFD_RELOC_MIPS_JMP: | 
|---|
| 1110 | mips_type = MIPS_R_JMPADDR; | 
|---|
| 1111 | break; | 
|---|
| 1112 | case BFD_RELOC_HI16_S: | 
|---|
| 1113 | mips_type = MIPS_R_REFHI; | 
|---|
| 1114 | break; | 
|---|
| 1115 | case BFD_RELOC_LO16: | 
|---|
| 1116 | mips_type = MIPS_R_REFLO; | 
|---|
| 1117 | break; | 
|---|
| 1118 | case BFD_RELOC_GPREL16: | 
|---|
| 1119 | mips_type = MIPS_R_GPREL; | 
|---|
| 1120 | break; | 
|---|
| 1121 | case BFD_RELOC_MIPS_LITERAL: | 
|---|
| 1122 | mips_type = MIPS_R_LITERAL; | 
|---|
| 1123 | break; | 
|---|
| 1124 | case BFD_RELOC_16_PCREL_S2: | 
|---|
| 1125 | mips_type = MIPS_R_PCREL16; | 
|---|
| 1126 | break; | 
|---|
| 1127 | case BFD_RELOC_PCREL_HI16_S: | 
|---|
| 1128 | mips_type = MIPS_R_RELHI; | 
|---|
| 1129 | break; | 
|---|
| 1130 | case BFD_RELOC_PCREL_LO16: | 
|---|
| 1131 | mips_type = MIPS_R_RELLO; | 
|---|
| 1132 | break; | 
|---|
| 1133 | case BFD_RELOC_GPREL32: | 
|---|
| 1134 | mips_type = MIPS_R_SWITCH; | 
|---|
| 1135 | break; | 
|---|
| 1136 | default: | 
|---|
| 1137 | return (reloc_howto_type *) NULL; | 
|---|
| 1138 | } | 
|---|
| 1139 |  | 
|---|
| 1140 | return &mips_howto_table[mips_type]; | 
|---|
| 1141 | } | 
|---|
| 1142 |  | 
|---|
| 1143 |  | 
|---|
| 1144 | /* A helper routine for mips_relocate_section which handles the REFHI | 
|---|
| 1145 | and RELHI relocations.  The REFHI relocation must be followed by a | 
|---|
| 1146 | REFLO relocation (and RELHI by a RELLO), and the addend used is | 
|---|
| 1147 | formed from the addends of both instructions.  */ | 
|---|
| 1148 |  | 
|---|
| 1149 | static void | 
|---|
| 1150 | mips_relocate_hi (refhi, reflo, input_bfd, input_section, contents, adjust, | 
|---|
| 1151 | relocation, pcrel) | 
|---|
| 1152 | struct internal_reloc *refhi; | 
|---|
| 1153 | struct internal_reloc *reflo; | 
|---|
| 1154 | bfd *input_bfd; | 
|---|
| 1155 | asection *input_section; | 
|---|
| 1156 | bfd_byte *contents; | 
|---|
| 1157 | size_t adjust; | 
|---|
| 1158 | bfd_vma relocation; | 
|---|
| 1159 | bfd_boolean pcrel; | 
|---|
| 1160 | { | 
|---|
| 1161 | unsigned long insn; | 
|---|
| 1162 | unsigned long val; | 
|---|
| 1163 | unsigned long vallo; | 
|---|
| 1164 |  | 
|---|
| 1165 | if (refhi == NULL) | 
|---|
| 1166 | return; | 
|---|
| 1167 |  | 
|---|
| 1168 | insn = bfd_get_32 (input_bfd, | 
|---|
| 1169 | contents + adjust + refhi->r_vaddr - input_section->vma); | 
|---|
| 1170 | if (reflo == NULL) | 
|---|
| 1171 | vallo = 0; | 
|---|
| 1172 | else | 
|---|
| 1173 | vallo = (bfd_get_32 (input_bfd, | 
|---|
| 1174 | contents + adjust + reflo->r_vaddr - input_section->vma) | 
|---|
| 1175 | & 0xffff); | 
|---|
| 1176 |  | 
|---|
| 1177 | val = ((insn & 0xffff) << 16) + vallo; | 
|---|
| 1178 | val += relocation; | 
|---|
| 1179 |  | 
|---|
| 1180 | /* The low order 16 bits are always treated as a signed value. | 
|---|
| 1181 | Therefore, a negative value in the low order bits requires an | 
|---|
| 1182 | adjustment in the high order bits.  We need to make this | 
|---|
| 1183 | adjustment in two ways: once for the bits we took from the data, | 
|---|
| 1184 | and once for the bits we are putting back in to the data.  */ | 
|---|
| 1185 | if ((vallo & 0x8000) != 0) | 
|---|
| 1186 | val -= 0x10000; | 
|---|
| 1187 |  | 
|---|
| 1188 | if (pcrel) | 
|---|
| 1189 | val -= (input_section->output_section->vma | 
|---|
| 1190 | + input_section->output_offset | 
|---|
| 1191 | + (reflo->r_vaddr - input_section->vma + adjust)); | 
|---|
| 1192 |  | 
|---|
| 1193 | if ((val & 0x8000) != 0) | 
|---|
| 1194 | val += 0x10000; | 
|---|
| 1195 |  | 
|---|
| 1196 | insn = (insn &~ (unsigned) 0xffff) | ((val >> 16) & 0xffff); | 
|---|
| 1197 | bfd_put_32 (input_bfd, (bfd_vma) insn, | 
|---|
| 1198 | contents + adjust + refhi->r_vaddr - input_section->vma); | 
|---|
| 1199 | } | 
|---|
| 1200 |  | 
|---|
| 1201 | /* Relocate a section while linking a MIPS ECOFF file.  */ | 
|---|
| 1202 |  | 
|---|
| 1203 | static bfd_boolean | 
|---|
| 1204 | mips_relocate_section (output_bfd, info, input_bfd, input_section, | 
|---|
| 1205 | contents, external_relocs) | 
|---|
| 1206 | bfd *output_bfd; | 
|---|
| 1207 | struct bfd_link_info *info; | 
|---|
| 1208 | bfd *input_bfd; | 
|---|
| 1209 | asection *input_section; | 
|---|
| 1210 | bfd_byte *contents; | 
|---|
| 1211 | PTR external_relocs; | 
|---|
| 1212 | { | 
|---|
| 1213 | asection **symndx_to_section; | 
|---|
| 1214 | struct ecoff_link_hash_entry **sym_hashes; | 
|---|
| 1215 | bfd_vma gp; | 
|---|
| 1216 | bfd_boolean gp_undefined; | 
|---|
| 1217 | size_t adjust; | 
|---|
| 1218 | long *offsets; | 
|---|
| 1219 | struct external_reloc *ext_rel; | 
|---|
| 1220 | struct external_reloc *ext_rel_end; | 
|---|
| 1221 | unsigned int i; | 
|---|
| 1222 | bfd_boolean got_lo; | 
|---|
| 1223 | struct internal_reloc lo_int_rel; | 
|---|
| 1224 | bfd_size_type amt; | 
|---|
| 1225 |  | 
|---|
| 1226 | BFD_ASSERT (input_bfd->xvec->byteorder | 
|---|
| 1227 | == output_bfd->xvec->byteorder); | 
|---|
| 1228 |  | 
|---|
| 1229 | /* We keep a table mapping the symndx found in an internal reloc to | 
|---|
| 1230 | the appropriate section.  This is faster than looking up the | 
|---|
| 1231 | section by name each time.  */ | 
|---|
| 1232 | symndx_to_section = ecoff_data (input_bfd)->symndx_to_section; | 
|---|
| 1233 | if (symndx_to_section == (asection **) NULL) | 
|---|
| 1234 | { | 
|---|
| 1235 | amt = NUM_RELOC_SECTIONS * sizeof (asection *); | 
|---|
| 1236 | symndx_to_section = (asection **) bfd_alloc (input_bfd, amt); | 
|---|
| 1237 | if (!symndx_to_section) | 
|---|
| 1238 | return FALSE; | 
|---|
| 1239 |  | 
|---|
| 1240 | symndx_to_section[RELOC_SECTION_NONE] = NULL; | 
|---|
| 1241 | symndx_to_section[RELOC_SECTION_TEXT] = | 
|---|
| 1242 | bfd_get_section_by_name (input_bfd, ".text"); | 
|---|
| 1243 | symndx_to_section[RELOC_SECTION_RDATA] = | 
|---|
| 1244 | bfd_get_section_by_name (input_bfd, ".rdata"); | 
|---|
| 1245 | symndx_to_section[RELOC_SECTION_DATA] = | 
|---|
| 1246 | bfd_get_section_by_name (input_bfd, ".data"); | 
|---|
| 1247 | symndx_to_section[RELOC_SECTION_SDATA] = | 
|---|
| 1248 | bfd_get_section_by_name (input_bfd, ".sdata"); | 
|---|
| 1249 | symndx_to_section[RELOC_SECTION_SBSS] = | 
|---|
| 1250 | bfd_get_section_by_name (input_bfd, ".sbss"); | 
|---|
| 1251 | symndx_to_section[RELOC_SECTION_BSS] = | 
|---|
| 1252 | bfd_get_section_by_name (input_bfd, ".bss"); | 
|---|
| 1253 | symndx_to_section[RELOC_SECTION_INIT] = | 
|---|
| 1254 | bfd_get_section_by_name (input_bfd, ".init"); | 
|---|
| 1255 | symndx_to_section[RELOC_SECTION_LIT8] = | 
|---|
| 1256 | bfd_get_section_by_name (input_bfd, ".lit8"); | 
|---|
| 1257 | symndx_to_section[RELOC_SECTION_LIT4] = | 
|---|
| 1258 | bfd_get_section_by_name (input_bfd, ".lit4"); | 
|---|
| 1259 | symndx_to_section[RELOC_SECTION_XDATA] = NULL; | 
|---|
| 1260 | symndx_to_section[RELOC_SECTION_PDATA] = NULL; | 
|---|
| 1261 | symndx_to_section[RELOC_SECTION_FINI] = | 
|---|
| 1262 | bfd_get_section_by_name (input_bfd, ".fini"); | 
|---|
| 1263 | symndx_to_section[RELOC_SECTION_LITA] = NULL; | 
|---|
| 1264 | symndx_to_section[RELOC_SECTION_ABS] = NULL; | 
|---|
| 1265 |  | 
|---|
| 1266 | ecoff_data (input_bfd)->symndx_to_section = symndx_to_section; | 
|---|
| 1267 | } | 
|---|
| 1268 |  | 
|---|
| 1269 | sym_hashes = ecoff_data (input_bfd)->sym_hashes; | 
|---|
| 1270 |  | 
|---|
| 1271 | gp = _bfd_get_gp_value (output_bfd); | 
|---|
| 1272 | if (gp == 0) | 
|---|
| 1273 | gp_undefined = TRUE; | 
|---|
| 1274 | else | 
|---|
| 1275 | gp_undefined = FALSE; | 
|---|
| 1276 |  | 
|---|
| 1277 | got_lo = FALSE; | 
|---|
| 1278 |  | 
|---|
| 1279 | adjust = 0; | 
|---|
| 1280 |  | 
|---|
| 1281 | if (ecoff_section_data (input_bfd, input_section) == NULL) | 
|---|
| 1282 | offsets = NULL; | 
|---|
| 1283 | else | 
|---|
| 1284 | offsets = ecoff_section_data (input_bfd, input_section)->offsets; | 
|---|
| 1285 |  | 
|---|
| 1286 | ext_rel = (struct external_reloc *) external_relocs; | 
|---|
| 1287 | ext_rel_end = ext_rel + input_section->reloc_count; | 
|---|
| 1288 | for (i = 0; ext_rel < ext_rel_end; ext_rel++, i++) | 
|---|
| 1289 | { | 
|---|
| 1290 | struct internal_reloc int_rel; | 
|---|
| 1291 | bfd_boolean use_lo = FALSE; | 
|---|
| 1292 | bfd_vma addend; | 
|---|
| 1293 | reloc_howto_type *howto; | 
|---|
| 1294 | struct ecoff_link_hash_entry *h = NULL; | 
|---|
| 1295 | asection *s = NULL; | 
|---|
| 1296 | bfd_vma relocation; | 
|---|
| 1297 | bfd_reloc_status_type r; | 
|---|
| 1298 |  | 
|---|
| 1299 | if (! got_lo) | 
|---|
| 1300 | mips_ecoff_swap_reloc_in (input_bfd, (PTR) ext_rel, &int_rel); | 
|---|
| 1301 | else | 
|---|
| 1302 | { | 
|---|
| 1303 | int_rel = lo_int_rel; | 
|---|
| 1304 | got_lo = FALSE; | 
|---|
| 1305 | } | 
|---|
| 1306 |  | 
|---|
| 1307 | BFD_ASSERT (int_rel.r_type | 
|---|
| 1308 | < sizeof mips_howto_table / sizeof mips_howto_table[0]); | 
|---|
| 1309 |  | 
|---|
| 1310 | /* The REFHI and RELHI relocs requires special handling.  they | 
|---|
| 1311 | must be followed by a REFLO or RELLO reloc, respectively, and | 
|---|
| 1312 | the addend is formed from both relocs.  */ | 
|---|
| 1313 | if (int_rel.r_type == MIPS_R_REFHI | 
|---|
| 1314 | || int_rel.r_type == MIPS_R_RELHI) | 
|---|
| 1315 | { | 
|---|
| 1316 | struct external_reloc *lo_ext_rel; | 
|---|
| 1317 |  | 
|---|
| 1318 | /* As a GNU extension, permit an arbitrary number of REFHI | 
|---|
| 1319 | or RELHI relocs before the REFLO or RELLO reloc.  This | 
|---|
| 1320 | permits gcc to emit the HI and LO relocs itself.  */ | 
|---|
| 1321 | for (lo_ext_rel = ext_rel + 1; | 
|---|
| 1322 | lo_ext_rel < ext_rel_end; | 
|---|
| 1323 | lo_ext_rel++) | 
|---|
| 1324 | { | 
|---|
| 1325 | mips_ecoff_swap_reloc_in (input_bfd, (PTR) lo_ext_rel, | 
|---|
| 1326 | &lo_int_rel); | 
|---|
| 1327 | if (lo_int_rel.r_type != int_rel.r_type) | 
|---|
| 1328 | break; | 
|---|
| 1329 | } | 
|---|
| 1330 |  | 
|---|
| 1331 | if (lo_ext_rel < ext_rel_end | 
|---|
| 1332 | && (lo_int_rel.r_type | 
|---|
| 1333 | == (int_rel.r_type == MIPS_R_REFHI | 
|---|
| 1334 | ? MIPS_R_REFLO | 
|---|
| 1335 | : MIPS_R_RELLO)) | 
|---|
| 1336 | && int_rel.r_extern == lo_int_rel.r_extern | 
|---|
| 1337 | && int_rel.r_symndx == lo_int_rel.r_symndx) | 
|---|
| 1338 | { | 
|---|
| 1339 | use_lo = TRUE; | 
|---|
| 1340 | if (lo_ext_rel == ext_rel + 1) | 
|---|
| 1341 | got_lo = TRUE; | 
|---|
| 1342 | } | 
|---|
| 1343 | } | 
|---|
| 1344 |  | 
|---|
| 1345 | howto = &mips_howto_table[int_rel.r_type]; | 
|---|
| 1346 |  | 
|---|
| 1347 | /* The SWITCH reloc must be handled specially.  This reloc is | 
|---|
| 1348 | marks the location of a difference between two portions of an | 
|---|
| 1349 | object file.  The symbol index does not reference a symbol, | 
|---|
| 1350 | but is actually the offset from the reloc to the subtrahend | 
|---|
| 1351 | of the difference.  This reloc is correct in the object file, | 
|---|
| 1352 | and needs no further adjustment, unless we are relaxing.  If | 
|---|
| 1353 | we are relaxing, we may have to add in an offset.  Since no | 
|---|
| 1354 | symbols are involved in this reloc, we handle it completely | 
|---|
| 1355 | here.  */ | 
|---|
| 1356 | if (int_rel.r_type == MIPS_R_SWITCH) | 
|---|
| 1357 | { | 
|---|
| 1358 | if (offsets != NULL | 
|---|
| 1359 | && offsets[i] != 0) | 
|---|
| 1360 | { | 
|---|
| 1361 | r = _bfd_relocate_contents (howto, input_bfd, | 
|---|
| 1362 | (bfd_vma) offsets[i], | 
|---|
| 1363 | (contents | 
|---|
| 1364 | + adjust | 
|---|
| 1365 | + int_rel.r_vaddr | 
|---|
| 1366 | - input_section->vma)); | 
|---|
| 1367 | BFD_ASSERT (r == bfd_reloc_ok); | 
|---|
| 1368 | } | 
|---|
| 1369 |  | 
|---|
| 1370 | continue; | 
|---|
| 1371 | } | 
|---|
| 1372 |  | 
|---|
| 1373 | if (int_rel.r_extern) | 
|---|
| 1374 | { | 
|---|
| 1375 | h = sym_hashes[int_rel.r_symndx]; | 
|---|
| 1376 | /* If h is NULL, that means that there is a reloc against an | 
|---|
| 1377 | external symbol which we thought was just a debugging | 
|---|
| 1378 | symbol.  This should not happen.  */ | 
|---|
| 1379 | if (h == (struct ecoff_link_hash_entry *) NULL) | 
|---|
| 1380 | abort (); | 
|---|
| 1381 | } | 
|---|
| 1382 | else | 
|---|
| 1383 | { | 
|---|
| 1384 | if (int_rel.r_symndx < 0 || int_rel.r_symndx >= NUM_RELOC_SECTIONS) | 
|---|
| 1385 | s = NULL; | 
|---|
| 1386 | else | 
|---|
| 1387 | s = symndx_to_section[int_rel.r_symndx]; | 
|---|
| 1388 |  | 
|---|
| 1389 | if (s == (asection *) NULL) | 
|---|
| 1390 | abort (); | 
|---|
| 1391 | } | 
|---|
| 1392 |  | 
|---|
| 1393 | /* The GPREL reloc uses an addend: the difference in the GP | 
|---|
| 1394 | values.  */ | 
|---|
| 1395 | if (int_rel.r_type != MIPS_R_GPREL | 
|---|
| 1396 | && int_rel.r_type != MIPS_R_LITERAL) | 
|---|
| 1397 | addend = 0; | 
|---|
| 1398 | else | 
|---|
| 1399 | { | 
|---|
| 1400 | if (gp_undefined) | 
|---|
| 1401 | { | 
|---|
| 1402 | if (! ((*info->callbacks->reloc_dangerous) | 
|---|
| 1403 | (info, _("GP relative relocation used when GP not defined"), | 
|---|
| 1404 | input_bfd, input_section, | 
|---|
| 1405 | int_rel.r_vaddr - input_section->vma))) | 
|---|
| 1406 | return FALSE; | 
|---|
| 1407 | /* Only give the error once per link.  */ | 
|---|
| 1408 | gp = 4; | 
|---|
| 1409 | _bfd_set_gp_value (output_bfd, gp); | 
|---|
| 1410 | gp_undefined = FALSE; | 
|---|
| 1411 | } | 
|---|
| 1412 | if (! int_rel.r_extern) | 
|---|
| 1413 | { | 
|---|
| 1414 | /* This is a relocation against a section.  The current | 
|---|
| 1415 | addend in the instruction is the difference between | 
|---|
| 1416 | INPUT_SECTION->vma and the GP value of INPUT_BFD.  We | 
|---|
| 1417 | must change this to be the difference between the | 
|---|
| 1418 | final definition (which will end up in RELOCATION) | 
|---|
| 1419 | and the GP value of OUTPUT_BFD (which is in GP).  */ | 
|---|
| 1420 | addend = ecoff_data (input_bfd)->gp - gp; | 
|---|
| 1421 | } | 
|---|
| 1422 | else if (! info->relocateable | 
|---|
| 1423 | || h->root.type == bfd_link_hash_defined | 
|---|
| 1424 | || h->root.type == bfd_link_hash_defweak) | 
|---|
| 1425 | { | 
|---|
| 1426 | /* This is a relocation against a defined symbol.  The | 
|---|
| 1427 | current addend in the instruction is simply the | 
|---|
| 1428 | desired offset into the symbol (normally zero).  We | 
|---|
| 1429 | are going to change this into a relocation against a | 
|---|
| 1430 | defined symbol, so we want the instruction to hold | 
|---|
| 1431 | the difference between the final definition of the | 
|---|
| 1432 | symbol (which will end up in RELOCATION) and the GP | 
|---|
| 1433 | value of OUTPUT_BFD (which is in GP).  */ | 
|---|
| 1434 | addend = - gp; | 
|---|
| 1435 | } | 
|---|
| 1436 | else | 
|---|
| 1437 | { | 
|---|
| 1438 | /* This is a relocation against an undefined or common | 
|---|
| 1439 | symbol.  The current addend in the instruction is | 
|---|
| 1440 | simply the desired offset into the symbol (normally | 
|---|
| 1441 | zero).  We are generating relocateable output, and we | 
|---|
| 1442 | aren't going to define this symbol, so we just leave | 
|---|
| 1443 | the instruction alone.  */ | 
|---|
| 1444 | addend = 0; | 
|---|
| 1445 | } | 
|---|
| 1446 | } | 
|---|
| 1447 |  | 
|---|
| 1448 | /* If we are relaxing, mips_relax_section may have set | 
|---|
| 1449 | offsets[i] to some value.  A value of 1 means we must expand | 
|---|
| 1450 | a PC relative branch into a multi-instruction of sequence, | 
|---|
| 1451 | and any other value is an addend.  */ | 
|---|
| 1452 | if (offsets != NULL | 
|---|
| 1453 | && offsets[i] != 0) | 
|---|
| 1454 | { | 
|---|
| 1455 | BFD_ASSERT (! info->relocateable); | 
|---|
| 1456 | BFD_ASSERT (int_rel.r_type == MIPS_R_PCREL16 | 
|---|
| 1457 | || int_rel.r_type == MIPS_R_RELHI | 
|---|
| 1458 | || int_rel.r_type == MIPS_R_RELLO); | 
|---|
| 1459 | if (offsets[i] != 1) | 
|---|
| 1460 | addend += offsets[i]; | 
|---|
| 1461 | else | 
|---|
| 1462 | { | 
|---|
| 1463 | bfd_byte *here; | 
|---|
| 1464 |  | 
|---|
| 1465 | BFD_ASSERT (int_rel.r_extern | 
|---|
| 1466 | && int_rel.r_type == MIPS_R_PCREL16); | 
|---|
| 1467 |  | 
|---|
| 1468 | /* Move the rest of the instructions up.  */ | 
|---|
| 1469 | here = (contents | 
|---|
| 1470 | + adjust | 
|---|
| 1471 | + int_rel.r_vaddr | 
|---|
| 1472 | - input_section->vma); | 
|---|
| 1473 | memmove (here + PCREL16_EXPANSION_ADJUSTMENT, here, | 
|---|
| 1474 | (size_t) (input_section->_raw_size | 
|---|
| 1475 | - (int_rel.r_vaddr - input_section->vma))); | 
|---|
| 1476 |  | 
|---|
| 1477 | /* Generate the new instructions.  */ | 
|---|
| 1478 | if (! mips_relax_pcrel16 (info, input_bfd, input_section, | 
|---|
| 1479 | h, here, | 
|---|
| 1480 | (input_section->output_section->vma | 
|---|
| 1481 | + input_section->output_offset | 
|---|
| 1482 | + (int_rel.r_vaddr | 
|---|
| 1483 | - input_section->vma) | 
|---|
| 1484 | + adjust))) | 
|---|
| 1485 | return FALSE; | 
|---|
| 1486 |  | 
|---|
| 1487 | /* We must adjust everything else up a notch.  */ | 
|---|
| 1488 | adjust += PCREL16_EXPANSION_ADJUSTMENT; | 
|---|
| 1489 |  | 
|---|
| 1490 | /* mips_relax_pcrel16 handles all the details of this | 
|---|
| 1491 | relocation.  */ | 
|---|
| 1492 | continue; | 
|---|
| 1493 | } | 
|---|
| 1494 | } | 
|---|
| 1495 |  | 
|---|
| 1496 | /* If we are relaxing, and this is a reloc against the .text | 
|---|
| 1497 | segment, we may need to adjust it if some branches have been | 
|---|
| 1498 | expanded.  The reloc types which are likely to occur in the | 
|---|
| 1499 | .text section are handled efficiently by mips_relax_section, | 
|---|
| 1500 | and thus do not need to be handled here.  */ | 
|---|
| 1501 | if (ecoff_data (input_bfd)->debug_info.adjust != NULL | 
|---|
| 1502 | && ! int_rel.r_extern | 
|---|
| 1503 | && int_rel.r_symndx == RELOC_SECTION_TEXT | 
|---|
| 1504 | && (strcmp (bfd_get_section_name (input_bfd, input_section), | 
|---|
| 1505 | ".text") != 0 | 
|---|
| 1506 | || (int_rel.r_type != MIPS_R_PCREL16 | 
|---|
| 1507 | && int_rel.r_type != MIPS_R_SWITCH | 
|---|
| 1508 | && int_rel.r_type != MIPS_R_RELHI | 
|---|
| 1509 | && int_rel.r_type != MIPS_R_RELLO))) | 
|---|
| 1510 | { | 
|---|
| 1511 | bfd_vma adr; | 
|---|
| 1512 | struct ecoff_value_adjust *a; | 
|---|
| 1513 |  | 
|---|
| 1514 | /* We need to get the addend so that we know whether we need | 
|---|
| 1515 | to adjust the address.  */ | 
|---|
| 1516 | BFD_ASSERT (int_rel.r_type == MIPS_R_REFWORD); | 
|---|
| 1517 |  | 
|---|
| 1518 | adr = bfd_get_32 (input_bfd, | 
|---|
| 1519 | (contents | 
|---|
| 1520 | + adjust | 
|---|
| 1521 | + int_rel.r_vaddr | 
|---|
| 1522 | - input_section->vma)); | 
|---|
| 1523 |  | 
|---|
| 1524 | for (a = ecoff_data (input_bfd)->debug_info.adjust; | 
|---|
| 1525 | a != (struct ecoff_value_adjust *) NULL; | 
|---|
| 1526 | a = a->next) | 
|---|
| 1527 | { | 
|---|
| 1528 | if (adr >= a->start && adr < a->end) | 
|---|
| 1529 | addend += a->adjust; | 
|---|
| 1530 | } | 
|---|
| 1531 | } | 
|---|
| 1532 |  | 
|---|
| 1533 | if (info->relocateable) | 
|---|
| 1534 | { | 
|---|
| 1535 | /* We are generating relocateable output, and must convert | 
|---|
| 1536 | the existing reloc.  */ | 
|---|
| 1537 | if (int_rel.r_extern) | 
|---|
| 1538 | { | 
|---|
| 1539 | if ((h->root.type == bfd_link_hash_defined | 
|---|
| 1540 | || h->root.type == bfd_link_hash_defweak) | 
|---|
| 1541 | && ! bfd_is_abs_section (h->root.u.def.section)) | 
|---|
| 1542 | { | 
|---|
| 1543 | const char *name; | 
|---|
| 1544 |  | 
|---|
| 1545 | /* This symbol is defined in the output.  Convert | 
|---|
| 1546 | the reloc from being against the symbol to being | 
|---|
| 1547 | against the section.  */ | 
|---|
| 1548 |  | 
|---|
| 1549 | /* Clear the r_extern bit.  */ | 
|---|
| 1550 | int_rel.r_extern = 0; | 
|---|
| 1551 |  | 
|---|
| 1552 | /* Compute a new r_symndx value.  */ | 
|---|
| 1553 | s = h->root.u.def.section; | 
|---|
| 1554 | name = bfd_get_section_name (output_bfd, | 
|---|
| 1555 | s->output_section); | 
|---|
| 1556 |  | 
|---|
| 1557 | int_rel.r_symndx = -1; | 
|---|
| 1558 | switch (name[1]) | 
|---|
| 1559 | { | 
|---|
| 1560 | case 'b': | 
|---|
| 1561 | if (strcmp (name, ".bss") == 0) | 
|---|
| 1562 | int_rel.r_symndx = RELOC_SECTION_BSS; | 
|---|
| 1563 | break; | 
|---|
| 1564 | case 'd': | 
|---|
| 1565 | if (strcmp (name, ".data") == 0) | 
|---|
| 1566 | int_rel.r_symndx = RELOC_SECTION_DATA; | 
|---|
| 1567 | break; | 
|---|
| 1568 | case 'f': | 
|---|
| 1569 | if (strcmp (name, ".fini") == 0) | 
|---|
| 1570 | int_rel.r_symndx = RELOC_SECTION_FINI; | 
|---|
| 1571 | break; | 
|---|
| 1572 | case 'i': | 
|---|
| 1573 | if (strcmp (name, ".init") == 0) | 
|---|
| 1574 | int_rel.r_symndx = RELOC_SECTION_INIT; | 
|---|
| 1575 | break; | 
|---|
| 1576 | case 'l': | 
|---|
| 1577 | if (strcmp (name, ".lit8") == 0) | 
|---|
| 1578 | int_rel.r_symndx = RELOC_SECTION_LIT8; | 
|---|
| 1579 | else if (strcmp (name, ".lit4") == 0) | 
|---|
| 1580 | int_rel.r_symndx = RELOC_SECTION_LIT4; | 
|---|
| 1581 | break; | 
|---|
| 1582 | case 'r': | 
|---|
| 1583 | if (strcmp (name, ".rdata") == 0) | 
|---|
| 1584 | int_rel.r_symndx = RELOC_SECTION_RDATA; | 
|---|
| 1585 | break; | 
|---|
| 1586 | case 's': | 
|---|
| 1587 | if (strcmp (name, ".sdata") == 0) | 
|---|
| 1588 | int_rel.r_symndx = RELOC_SECTION_SDATA; | 
|---|
| 1589 | else if (strcmp (name, ".sbss") == 0) | 
|---|
| 1590 | int_rel.r_symndx = RELOC_SECTION_SBSS; | 
|---|
| 1591 | break; | 
|---|
| 1592 | case 't': | 
|---|
| 1593 | if (strcmp (name, ".text") == 0) | 
|---|
| 1594 | int_rel.r_symndx = RELOC_SECTION_TEXT; | 
|---|
| 1595 | break; | 
|---|
| 1596 | } | 
|---|
| 1597 |  | 
|---|
| 1598 | if (int_rel.r_symndx == -1) | 
|---|
| 1599 | abort (); | 
|---|
| 1600 |  | 
|---|
| 1601 | /* Add the section VMA and the symbol value.  */ | 
|---|
| 1602 | relocation = (h->root.u.def.value | 
|---|
| 1603 | + s->output_section->vma | 
|---|
| 1604 | + s->output_offset); | 
|---|
| 1605 |  | 
|---|
| 1606 | /* For a PC relative relocation, the object file | 
|---|
| 1607 | currently holds just the addend.  We must adjust | 
|---|
| 1608 | by the address to get the right value.  */ | 
|---|
| 1609 | if (howto->pc_relative) | 
|---|
| 1610 | { | 
|---|
| 1611 | relocation -= int_rel.r_vaddr - input_section->vma; | 
|---|
| 1612 |  | 
|---|
| 1613 | /* If we are converting a RELHI or RELLO reloc | 
|---|
| 1614 | from being against an external symbol to | 
|---|
| 1615 | being against a section, we must put a | 
|---|
| 1616 | special value into the r_offset field.  This | 
|---|
| 1617 | value is the old addend.  The r_offset for | 
|---|
| 1618 | both the RELHI and RELLO relocs are the same, | 
|---|
| 1619 | and we set both when we see RELHI.  */ | 
|---|
| 1620 | if (int_rel.r_type == MIPS_R_RELHI) | 
|---|
| 1621 | { | 
|---|
| 1622 | long addhi, addlo; | 
|---|
| 1623 |  | 
|---|
| 1624 | addhi = bfd_get_32 (input_bfd, | 
|---|
| 1625 | (contents | 
|---|
| 1626 | + adjust | 
|---|
| 1627 | + int_rel.r_vaddr | 
|---|
| 1628 | - input_section->vma)); | 
|---|
| 1629 | addhi &= 0xffff; | 
|---|
| 1630 | if (addhi & 0x8000) | 
|---|
| 1631 | addhi -= 0x10000; | 
|---|
| 1632 | addhi <<= 16; | 
|---|
| 1633 |  | 
|---|
| 1634 | if (! use_lo) | 
|---|
| 1635 | addlo = 0; | 
|---|
| 1636 | else | 
|---|
| 1637 | { | 
|---|
| 1638 | addlo = bfd_get_32 (input_bfd, | 
|---|
| 1639 | (contents | 
|---|
| 1640 | + adjust | 
|---|
| 1641 | + lo_int_rel.r_vaddr | 
|---|
| 1642 | - input_section->vma)); | 
|---|
| 1643 | addlo &= 0xffff; | 
|---|
| 1644 | if (addlo & 0x8000) | 
|---|
| 1645 | addlo -= 0x10000; | 
|---|
| 1646 |  | 
|---|
| 1647 | lo_int_rel.r_offset = addhi + addlo; | 
|---|
| 1648 | } | 
|---|
| 1649 |  | 
|---|
| 1650 | int_rel.r_offset = addhi + addlo; | 
|---|
| 1651 | } | 
|---|
| 1652 | } | 
|---|
| 1653 |  | 
|---|
| 1654 | h = NULL; | 
|---|
| 1655 | } | 
|---|
| 1656 | else | 
|---|
| 1657 | { | 
|---|
| 1658 | /* Change the symndx value to the right one for the | 
|---|
| 1659 | output BFD.  */ | 
|---|
| 1660 | int_rel.r_symndx = h->indx; | 
|---|
| 1661 | if (int_rel.r_symndx == -1) | 
|---|
| 1662 | { | 
|---|
| 1663 | /* This symbol is not being written out.  */ | 
|---|
| 1664 | if (! ((*info->callbacks->unattached_reloc) | 
|---|
| 1665 | (info, h->root.root.string, input_bfd, | 
|---|
| 1666 | input_section, | 
|---|
| 1667 | int_rel.r_vaddr - input_section->vma))) | 
|---|
| 1668 | return FALSE; | 
|---|
| 1669 | int_rel.r_symndx = 0; | 
|---|
| 1670 | } | 
|---|
| 1671 | relocation = 0; | 
|---|
| 1672 | } | 
|---|
| 1673 | } | 
|---|
| 1674 | else | 
|---|
| 1675 | { | 
|---|
| 1676 | /* This is a relocation against a section.  Adjust the | 
|---|
| 1677 | value by the amount the section moved.  */ | 
|---|
| 1678 | relocation = (s->output_section->vma | 
|---|
| 1679 | + s->output_offset | 
|---|
| 1680 | - s->vma); | 
|---|
| 1681 | } | 
|---|
| 1682 |  | 
|---|
| 1683 | relocation += addend; | 
|---|
| 1684 | addend = 0; | 
|---|
| 1685 |  | 
|---|
| 1686 | /* Adjust a PC relative relocation by removing the reference | 
|---|
| 1687 | to the original address in the section and including the | 
|---|
| 1688 | reference to the new address.  However, external RELHI | 
|---|
| 1689 | and RELLO relocs are PC relative, but don't include any | 
|---|
| 1690 | reference to the address.  The addend is merely an | 
|---|
| 1691 | addend.  */ | 
|---|
| 1692 | if (howto->pc_relative | 
|---|
| 1693 | && (! int_rel.r_extern | 
|---|
| 1694 | || (int_rel.r_type != MIPS_R_RELHI | 
|---|
| 1695 | && int_rel.r_type != MIPS_R_RELLO))) | 
|---|
| 1696 | relocation -= (input_section->output_section->vma | 
|---|
| 1697 | + input_section->output_offset | 
|---|
| 1698 | - input_section->vma); | 
|---|
| 1699 |  | 
|---|
| 1700 | /* Adjust the contents.  */ | 
|---|
| 1701 | if (relocation == 0) | 
|---|
| 1702 | r = bfd_reloc_ok; | 
|---|
| 1703 | else | 
|---|
| 1704 | { | 
|---|
| 1705 | if (int_rel.r_type != MIPS_R_REFHI | 
|---|
| 1706 | && int_rel.r_type != MIPS_R_RELHI) | 
|---|
| 1707 | r = _bfd_relocate_contents (howto, input_bfd, relocation, | 
|---|
| 1708 | (contents | 
|---|
| 1709 | + adjust | 
|---|
| 1710 | + int_rel.r_vaddr | 
|---|
| 1711 | - input_section->vma)); | 
|---|
| 1712 | else | 
|---|
| 1713 | { | 
|---|
| 1714 | mips_relocate_hi (&int_rel, | 
|---|
| 1715 | use_lo ? &lo_int_rel : NULL, | 
|---|
| 1716 | input_bfd, input_section, contents, | 
|---|
| 1717 | adjust, relocation, | 
|---|
| 1718 | int_rel.r_type == MIPS_R_RELHI); | 
|---|
| 1719 | r = bfd_reloc_ok; | 
|---|
| 1720 | } | 
|---|
| 1721 | } | 
|---|
| 1722 |  | 
|---|
| 1723 | /* Adjust the reloc address.  */ | 
|---|
| 1724 | int_rel.r_vaddr += (input_section->output_section->vma | 
|---|
| 1725 | + input_section->output_offset | 
|---|
| 1726 | - input_section->vma); | 
|---|
| 1727 |  | 
|---|
| 1728 | /* Save the changed reloc information.  */ | 
|---|
| 1729 | mips_ecoff_swap_reloc_out (input_bfd, &int_rel, (PTR) ext_rel); | 
|---|
| 1730 | } | 
|---|
| 1731 | else | 
|---|
| 1732 | { | 
|---|
| 1733 | /* We are producing a final executable.  */ | 
|---|
| 1734 | if (int_rel.r_extern) | 
|---|
| 1735 | { | 
|---|
| 1736 | /* This is a reloc against a symbol.  */ | 
|---|
| 1737 | if (h->root.type == bfd_link_hash_defined | 
|---|
| 1738 | || h->root.type == bfd_link_hash_defweak) | 
|---|
| 1739 | { | 
|---|
| 1740 | asection *hsec; | 
|---|
| 1741 |  | 
|---|
| 1742 | hsec = h->root.u.def.section; | 
|---|
| 1743 | relocation = (h->root.u.def.value | 
|---|
| 1744 | + hsec->output_section->vma | 
|---|
| 1745 | + hsec->output_offset); | 
|---|
| 1746 | } | 
|---|
| 1747 | else | 
|---|
| 1748 | { | 
|---|
| 1749 | if (! ((*info->callbacks->undefined_symbol) | 
|---|
| 1750 | (info, h->root.root.string, input_bfd, | 
|---|
| 1751 | input_section, | 
|---|
| 1752 | int_rel.r_vaddr - input_section->vma, TRUE))) | 
|---|
| 1753 | return FALSE; | 
|---|
| 1754 | relocation = 0; | 
|---|
| 1755 | } | 
|---|
| 1756 | } | 
|---|
| 1757 | else | 
|---|
| 1758 | { | 
|---|
| 1759 | /* This is a reloc against a section.  */ | 
|---|
| 1760 | relocation = (s->output_section->vma | 
|---|
| 1761 | + s->output_offset | 
|---|
| 1762 | - s->vma); | 
|---|
| 1763 |  | 
|---|
| 1764 | /* A PC relative reloc is already correct in the object | 
|---|
| 1765 | file.  Make it look like a pcrel_offset relocation by | 
|---|
| 1766 | adding in the start address.  */ | 
|---|
| 1767 | if (howto->pc_relative) | 
|---|
| 1768 | { | 
|---|
| 1769 | if (int_rel.r_type != MIPS_R_RELHI || ! use_lo) | 
|---|
| 1770 | relocation += int_rel.r_vaddr + adjust; | 
|---|
| 1771 | else | 
|---|
| 1772 | relocation += lo_int_rel.r_vaddr + adjust; | 
|---|
| 1773 | } | 
|---|
| 1774 | } | 
|---|
| 1775 |  | 
|---|
| 1776 | if (int_rel.r_type != MIPS_R_REFHI | 
|---|
| 1777 | && int_rel.r_type != MIPS_R_RELHI) | 
|---|
| 1778 | r = _bfd_final_link_relocate (howto, | 
|---|
| 1779 | input_bfd, | 
|---|
| 1780 | input_section, | 
|---|
| 1781 | contents, | 
|---|
| 1782 | (int_rel.r_vaddr | 
|---|
| 1783 | - input_section->vma | 
|---|
| 1784 | + adjust), | 
|---|
| 1785 | relocation, | 
|---|
| 1786 | addend); | 
|---|
| 1787 | else | 
|---|
| 1788 | { | 
|---|
| 1789 | mips_relocate_hi (&int_rel, | 
|---|
| 1790 | use_lo ? &lo_int_rel : NULL, | 
|---|
| 1791 | input_bfd, input_section, contents, adjust, | 
|---|
| 1792 | relocation, | 
|---|
| 1793 | int_rel.r_type == MIPS_R_RELHI); | 
|---|
| 1794 | r = bfd_reloc_ok; | 
|---|
| 1795 | } | 
|---|
| 1796 | } | 
|---|
| 1797 |  | 
|---|
| 1798 | /* MIPS_R_JMPADDR requires peculiar overflow detection.  The | 
|---|
| 1799 | instruction provides a 28 bit address (the two lower bits are | 
|---|
| 1800 | implicit zeroes) which is combined with the upper four bits | 
|---|
| 1801 | of the instruction address.  */ | 
|---|
| 1802 | if (r == bfd_reloc_ok | 
|---|
| 1803 | && int_rel.r_type == MIPS_R_JMPADDR | 
|---|
| 1804 | && (((relocation | 
|---|
| 1805 | + addend | 
|---|
| 1806 | + (int_rel.r_extern ? 0 : s->vma)) | 
|---|
| 1807 | & 0xf0000000) | 
|---|
| 1808 | != ((input_section->output_section->vma | 
|---|
| 1809 | + input_section->output_offset | 
|---|
| 1810 | + (int_rel.r_vaddr - input_section->vma) | 
|---|
| 1811 | + adjust) | 
|---|
| 1812 | & 0xf0000000))) | 
|---|
| 1813 | r = bfd_reloc_overflow; | 
|---|
| 1814 |  | 
|---|
| 1815 | if (r != bfd_reloc_ok) | 
|---|
| 1816 | { | 
|---|
| 1817 | switch (r) | 
|---|
| 1818 | { | 
|---|
| 1819 | default: | 
|---|
| 1820 | case bfd_reloc_outofrange: | 
|---|
| 1821 | abort (); | 
|---|
| 1822 | case bfd_reloc_overflow: | 
|---|
| 1823 | { | 
|---|
| 1824 | const char *name; | 
|---|
| 1825 |  | 
|---|
| 1826 | if (int_rel.r_extern) | 
|---|
| 1827 | name = h->root.root.string; | 
|---|
| 1828 | else | 
|---|
| 1829 | name = bfd_section_name (input_bfd, s); | 
|---|
| 1830 | if (! ((*info->callbacks->reloc_overflow) | 
|---|
| 1831 | (info, name, howto->name, (bfd_vma) 0, | 
|---|
| 1832 | input_bfd, input_section, | 
|---|
| 1833 | int_rel.r_vaddr - input_section->vma))) | 
|---|
| 1834 | return FALSE; | 
|---|
| 1835 | } | 
|---|
| 1836 | break; | 
|---|
| 1837 | } | 
|---|
| 1838 | } | 
|---|
| 1839 | } | 
|---|
| 1840 |  | 
|---|
| 1841 | return TRUE; | 
|---|
| 1842 | } | 
|---|
| 1843 |  | 
|---|
| 1844 |  | 
|---|
| 1845 | /* Read in the relocs for a section.  */ | 
|---|
| 1846 |  | 
|---|
| 1847 | static bfd_boolean | 
|---|
| 1848 | mips_read_relocs (abfd, sec) | 
|---|
| 1849 | bfd *abfd; | 
|---|
| 1850 | asection *sec; | 
|---|
| 1851 | { | 
|---|
| 1852 | struct ecoff_section_tdata *section_tdata; | 
|---|
| 1853 | bfd_size_type amt; | 
|---|
| 1854 |  | 
|---|
| 1855 | section_tdata = ecoff_section_data (abfd, sec); | 
|---|
| 1856 | if (section_tdata == (struct ecoff_section_tdata *) NULL) | 
|---|
| 1857 | { | 
|---|
| 1858 | amt = sizeof (struct ecoff_section_tdata); | 
|---|
| 1859 | sec->used_by_bfd = (PTR) bfd_alloc (abfd, amt); | 
|---|
| 1860 | if (sec->used_by_bfd == NULL) | 
|---|
| 1861 | return FALSE; | 
|---|
| 1862 |  | 
|---|
| 1863 | section_tdata = ecoff_section_data (abfd, sec); | 
|---|
| 1864 | section_tdata->external_relocs = NULL; | 
|---|
| 1865 | section_tdata->contents = NULL; | 
|---|
| 1866 | section_tdata->offsets = NULL; | 
|---|
| 1867 | } | 
|---|
| 1868 |  | 
|---|
| 1869 | if (section_tdata->external_relocs == NULL) | 
|---|
| 1870 | { | 
|---|
| 1871 | amt = ecoff_backend (abfd)->external_reloc_size; | 
|---|
| 1872 | amt *= sec->reloc_count; | 
|---|
| 1873 | section_tdata->external_relocs = (PTR) bfd_alloc (abfd, amt); | 
|---|
| 1874 | if (section_tdata->external_relocs == NULL && amt != 0) | 
|---|
| 1875 | return FALSE; | 
|---|
| 1876 |  | 
|---|
| 1877 | if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0 | 
|---|
| 1878 | || bfd_bread (section_tdata->external_relocs, amt, abfd) != amt) | 
|---|
| 1879 | return FALSE; | 
|---|
| 1880 | } | 
|---|
| 1881 |  | 
|---|
| 1882 | return TRUE; | 
|---|
| 1883 | } | 
|---|
| 1884 |  | 
|---|
| 1885 | /* Relax a section when linking a MIPS ECOFF file.  This is used for | 
|---|
| 1886 | embedded PIC code, which always uses PC relative branches which | 
|---|
| 1887 | only have an 18 bit range on MIPS.  If a branch is not in range, we | 
|---|
| 1888 | generate a long instruction sequence to compensate.  Each time we | 
|---|
| 1889 | find a branch to expand, we have to check all the others again to | 
|---|
| 1890 | make sure they are still in range.  This is slow, but it only has | 
|---|
| 1891 | to be done when -relax is passed to the linker. | 
|---|
| 1892 |  | 
|---|
| 1893 | This routine figures out which branches need to expand; the actual | 
|---|
| 1894 | expansion is done in mips_relocate_section when the section | 
|---|
| 1895 | contents are relocated.  The information is stored in the offsets | 
|---|
| 1896 | field of the ecoff_section_tdata structure.  An offset of 1 means | 
|---|
| 1897 | that the branch must be expanded into a multi-instruction PC | 
|---|
| 1898 | relative branch (such an offset will only occur for a PC relative | 
|---|
| 1899 | branch to an external symbol).  Any other offset must be a multiple | 
|---|
| 1900 | of four, and is the amount to change the branch by (such an offset | 
|---|
| 1901 | will only occur for a PC relative branch within the same section). | 
|---|
| 1902 |  | 
|---|
| 1903 | We do not modify the section relocs or contents themselves so that | 
|---|
| 1904 | if memory usage becomes an issue we can discard them and read them | 
|---|
| 1905 | again.  The only information we must save in memory between this | 
|---|
| 1906 | routine and the mips_relocate_section routine is the table of | 
|---|
| 1907 | offsets.  */ | 
|---|
| 1908 |  | 
|---|
| 1909 | static bfd_boolean | 
|---|
| 1910 | mips_relax_section (abfd, sec, info, again) | 
|---|
| 1911 | bfd *abfd; | 
|---|
| 1912 | asection *sec; | 
|---|
| 1913 | struct bfd_link_info *info; | 
|---|
| 1914 | bfd_boolean *again; | 
|---|
| 1915 | { | 
|---|
| 1916 | struct ecoff_section_tdata *section_tdata; | 
|---|
| 1917 | bfd_byte *contents = NULL; | 
|---|
| 1918 | long *offsets; | 
|---|
| 1919 | struct external_reloc *ext_rel; | 
|---|
| 1920 | struct external_reloc *ext_rel_end; | 
|---|
| 1921 | unsigned int i; | 
|---|
| 1922 |  | 
|---|
| 1923 | /* Assume we are not going to need another pass.  */ | 
|---|
| 1924 | *again = FALSE; | 
|---|
| 1925 |  | 
|---|
| 1926 | /* If we are not generating an ECOFF file, this is much too | 
|---|
| 1927 | confusing to deal with.  */ | 
|---|
| 1928 | if (info->hash->creator->flavour != bfd_get_flavour (abfd)) | 
|---|
| 1929 | return TRUE; | 
|---|
| 1930 |  | 
|---|
| 1931 | /* If there are no relocs, there is nothing to do.  */ | 
|---|
| 1932 | if (sec->reloc_count == 0) | 
|---|
| 1933 | return TRUE; | 
|---|
| 1934 |  | 
|---|
| 1935 | /* We are only interested in PC relative relocs, and why would there | 
|---|
| 1936 | ever be one from anything but the .text section?  */ | 
|---|
| 1937 | if (strcmp (bfd_get_section_name (abfd, sec), ".text") != 0) | 
|---|
| 1938 | return TRUE; | 
|---|
| 1939 |  | 
|---|
| 1940 | /* Read in the relocs, if we haven't already got them.  */ | 
|---|
| 1941 | section_tdata = ecoff_section_data (abfd, sec); | 
|---|
| 1942 | if (section_tdata == (struct ecoff_section_tdata *) NULL | 
|---|
| 1943 | || section_tdata->external_relocs == NULL) | 
|---|
| 1944 | { | 
|---|
| 1945 | if (! mips_read_relocs (abfd, sec)) | 
|---|
| 1946 | goto error_return; | 
|---|
| 1947 | section_tdata = ecoff_section_data (abfd, sec); | 
|---|
| 1948 | } | 
|---|
| 1949 |  | 
|---|
| 1950 | if (sec->_cooked_size == 0) | 
|---|
| 1951 | { | 
|---|
| 1952 | /* We must initialize _cooked_size only the first time we are | 
|---|
| 1953 | called.  */ | 
|---|
| 1954 | sec->_cooked_size = sec->_raw_size; | 
|---|
| 1955 | } | 
|---|
| 1956 |  | 
|---|
| 1957 | contents = section_tdata->contents; | 
|---|
| 1958 | offsets = section_tdata->offsets; | 
|---|
| 1959 |  | 
|---|
| 1960 | /* Look for any external PC relative relocs.  Internal PC relative | 
|---|
| 1961 | relocs are already correct in the object file, so they certainly | 
|---|
| 1962 | can not overflow.  */ | 
|---|
| 1963 | ext_rel = (struct external_reloc *) section_tdata->external_relocs; | 
|---|
| 1964 | ext_rel_end = ext_rel + sec->reloc_count; | 
|---|
| 1965 | for (i = 0; ext_rel < ext_rel_end; ext_rel++, i++) | 
|---|
| 1966 | { | 
|---|
| 1967 | struct internal_reloc int_rel; | 
|---|
| 1968 | struct ecoff_link_hash_entry *h; | 
|---|
| 1969 | asection *hsec; | 
|---|
| 1970 | bfd_signed_vma relocation; | 
|---|
| 1971 | struct external_reloc *adj_ext_rel; | 
|---|
| 1972 | unsigned int adj_i; | 
|---|
| 1973 | unsigned long ext_count; | 
|---|
| 1974 | struct ecoff_link_hash_entry **adj_h_ptr; | 
|---|
| 1975 | struct ecoff_link_hash_entry **adj_h_ptr_end; | 
|---|
| 1976 | struct ecoff_value_adjust *adjust; | 
|---|
| 1977 | bfd_size_type amt; | 
|---|
| 1978 |  | 
|---|
| 1979 | /* If we have already expanded this reloc, we certainly don't | 
|---|
| 1980 | need to do it again.  */ | 
|---|
| 1981 | if (offsets != (long *) NULL && offsets[i] == 1) | 
|---|
| 1982 | continue; | 
|---|
| 1983 |  | 
|---|
| 1984 | /* Quickly check that this reloc is external PCREL16.  */ | 
|---|
| 1985 | if (bfd_header_big_endian (abfd)) | 
|---|
| 1986 | { | 
|---|
| 1987 | if ((ext_rel->r_bits[3] & RELOC_BITS3_EXTERN_BIG) == 0 | 
|---|
| 1988 | || (((ext_rel->r_bits[3] & RELOC_BITS3_TYPE_BIG) | 
|---|
| 1989 | >> RELOC_BITS3_TYPE_SH_BIG) | 
|---|
| 1990 | != MIPS_R_PCREL16)) | 
|---|
| 1991 | continue; | 
|---|
| 1992 | } | 
|---|
| 1993 | else | 
|---|
| 1994 | { | 
|---|
| 1995 | if ((ext_rel->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) == 0 | 
|---|
| 1996 | || (((ext_rel->r_bits[3] & RELOC_BITS3_TYPE_LITTLE) | 
|---|
| 1997 | >> RELOC_BITS3_TYPE_SH_LITTLE) | 
|---|
| 1998 | != MIPS_R_PCREL16)) | 
|---|
| 1999 | continue; | 
|---|
| 2000 | } | 
|---|
| 2001 |  | 
|---|
| 2002 | mips_ecoff_swap_reloc_in (abfd, (PTR) ext_rel, &int_rel); | 
|---|
| 2003 |  | 
|---|
| 2004 | h = ecoff_data (abfd)->sym_hashes[int_rel.r_symndx]; | 
|---|
| 2005 | if (h == (struct ecoff_link_hash_entry *) NULL) | 
|---|
| 2006 | abort (); | 
|---|
| 2007 |  | 
|---|
| 2008 | if (h->root.type != bfd_link_hash_defined | 
|---|
| 2009 | && h->root.type != bfd_link_hash_defweak) | 
|---|
| 2010 | { | 
|---|
| 2011 | /* Just ignore undefined symbols.  These will presumably | 
|---|
| 2012 | generate an error later in the link.  */ | 
|---|
| 2013 | continue; | 
|---|
| 2014 | } | 
|---|
| 2015 |  | 
|---|
| 2016 | /* Get the value of the symbol.  */ | 
|---|
| 2017 | hsec = h->root.u.def.section; | 
|---|
| 2018 | relocation = (h->root.u.def.value | 
|---|
| 2019 | + hsec->output_section->vma | 
|---|
| 2020 | + hsec->output_offset); | 
|---|
| 2021 |  | 
|---|
| 2022 | /* Subtract out the current address.  */ | 
|---|
| 2023 | relocation -= (sec->output_section->vma | 
|---|
| 2024 | + sec->output_offset | 
|---|
| 2025 | + (int_rel.r_vaddr - sec->vma)); | 
|---|
| 2026 |  | 
|---|
| 2027 | /* The addend is stored in the object file.  In the normal case | 
|---|
| 2028 | of ``bal symbol'', the addend will be -4.  It will only be | 
|---|
| 2029 | different in the case of ``bal symbol+constant''.  To avoid | 
|---|
| 2030 | always reading in the section contents, we don't check the | 
|---|
| 2031 | addend in the object file (we could easily check the contents | 
|---|
| 2032 | if we happen to have already read them in, but I fear that | 
|---|
| 2033 | this could be confusing).  This means we will screw up if | 
|---|
| 2034 | there is a branch to a symbol that is in range, but added to | 
|---|
| 2035 | a constant which puts it out of range; in such a case the | 
|---|
| 2036 | link will fail with a reloc overflow error.  Since the | 
|---|
| 2037 | compiler will never generate such code, it should be easy | 
|---|
| 2038 | enough to work around it by changing the assembly code in the | 
|---|
| 2039 | source file.  */ | 
|---|
| 2040 | relocation -= 4; | 
|---|
| 2041 |  | 
|---|
| 2042 | /* Now RELOCATION is the number we want to put in the object | 
|---|
| 2043 | file.  See whether it fits.  */ | 
|---|
| 2044 | if (relocation >= -0x20000 && relocation < 0x20000) | 
|---|
| 2045 | continue; | 
|---|
| 2046 |  | 
|---|
| 2047 | /* Now that we know this reloc needs work, which will rarely | 
|---|
| 2048 | happen, go ahead and grab the section contents.  */ | 
|---|
| 2049 | if (contents == (bfd_byte *) NULL) | 
|---|
| 2050 | { | 
|---|
| 2051 | if (info->keep_memory) | 
|---|
| 2052 | contents = (bfd_byte *) bfd_alloc (abfd, sec->_raw_size); | 
|---|
| 2053 | else | 
|---|
| 2054 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | 
|---|
| 2055 | if (contents == (bfd_byte *) NULL) | 
|---|
| 2056 | goto error_return; | 
|---|
| 2057 | if (! bfd_get_section_contents (abfd, sec, (PTR) contents, | 
|---|
| 2058 | (file_ptr) 0, sec->_raw_size)) | 
|---|
| 2059 | goto error_return; | 
|---|
| 2060 | if (info->keep_memory) | 
|---|
| 2061 | section_tdata->contents = contents; | 
|---|
| 2062 | } | 
|---|
| 2063 |  | 
|---|
| 2064 | /* We only support changing the bal instruction.  It would be | 
|---|
| 2065 | possible to handle other PC relative branches, but some of | 
|---|
| 2066 | them (the conditional branches) would require a different | 
|---|
| 2067 | length instruction sequence which would complicate both this | 
|---|
| 2068 | routine and mips_relax_pcrel16.  It could be written if | 
|---|
| 2069 | somebody felt it were important.  Ignoring this reloc will | 
|---|
| 2070 | presumably cause a reloc overflow error later on.  */ | 
|---|
| 2071 | if (bfd_get_32 (abfd, contents + int_rel.r_vaddr - sec->vma) | 
|---|
| 2072 | != 0x0411ffff) /* bgezal $0,. == bal .  */ | 
|---|
| 2073 | continue; | 
|---|
| 2074 |  | 
|---|
| 2075 | /* Bother.  We need to expand this reloc, and we will need to | 
|---|
| 2076 | make another relaxation pass since this change may put other | 
|---|
| 2077 | relocs out of range.  We need to examine the local branches | 
|---|
| 2078 | and we need to allocate memory to hold the offsets we must | 
|---|
| 2079 | add to them.  We also need to adjust the values of all | 
|---|
| 2080 | symbols in the object file following this location.  */ | 
|---|
| 2081 |  | 
|---|
| 2082 | sec->_cooked_size += PCREL16_EXPANSION_ADJUSTMENT; | 
|---|
| 2083 | *again = TRUE; | 
|---|
| 2084 |  | 
|---|
| 2085 | if (offsets == (long *) NULL) | 
|---|
| 2086 | { | 
|---|
| 2087 | bfd_size_type size; | 
|---|
| 2088 |  | 
|---|
| 2089 | size = (bfd_size_type) sec->reloc_count * sizeof (long); | 
|---|
| 2090 | offsets = (long *) bfd_zalloc (abfd, size); | 
|---|
| 2091 | if (offsets == (long *) NULL) | 
|---|
| 2092 | goto error_return; | 
|---|
| 2093 | section_tdata->offsets = offsets; | 
|---|
| 2094 | } | 
|---|
| 2095 |  | 
|---|
| 2096 | offsets[i] = 1; | 
|---|
| 2097 |  | 
|---|
| 2098 | /* Now look for all PC relative references that cross this reloc | 
|---|
| 2099 | and adjust their offsets.  */ | 
|---|
| 2100 | adj_ext_rel = (struct external_reloc *) section_tdata->external_relocs; | 
|---|
| 2101 | for (adj_i = 0; adj_ext_rel < ext_rel_end; adj_ext_rel++, adj_i++) | 
|---|
| 2102 | { | 
|---|
| 2103 | struct internal_reloc adj_int_rel; | 
|---|
| 2104 | bfd_vma start, stop; | 
|---|
| 2105 | int change; | 
|---|
| 2106 |  | 
|---|
| 2107 | mips_ecoff_swap_reloc_in (abfd, (PTR) adj_ext_rel, &adj_int_rel); | 
|---|
| 2108 |  | 
|---|
| 2109 | if (adj_int_rel.r_type == MIPS_R_PCREL16) | 
|---|
| 2110 | { | 
|---|
| 2111 | unsigned long insn; | 
|---|
| 2112 |  | 
|---|
| 2113 | /* We only care about local references.  External ones | 
|---|
| 2114 | will be relocated correctly anyhow.  */ | 
|---|
| 2115 | if (adj_int_rel.r_extern) | 
|---|
| 2116 | continue; | 
|---|
| 2117 |  | 
|---|
| 2118 | /* We are only interested in a PC relative reloc within | 
|---|
| 2119 | this section.  FIXME: Cross section PC relative | 
|---|
| 2120 | relocs may not be handled correctly; does anybody | 
|---|
| 2121 | care?  */ | 
|---|
| 2122 | if (adj_int_rel.r_symndx != RELOC_SECTION_TEXT) | 
|---|
| 2123 | continue; | 
|---|
| 2124 |  | 
|---|
| 2125 | start = adj_int_rel.r_vaddr; | 
|---|
| 2126 |  | 
|---|
| 2127 | insn = bfd_get_32 (abfd, | 
|---|
| 2128 | contents + adj_int_rel.r_vaddr - sec->vma); | 
|---|
| 2129 |  | 
|---|
| 2130 | stop = (insn & 0xffff) << 2; | 
|---|
| 2131 | if ((stop & 0x20000) != 0) | 
|---|
| 2132 | stop -= 0x40000; | 
|---|
| 2133 | stop += adj_int_rel.r_vaddr + 4; | 
|---|
| 2134 | } | 
|---|
| 2135 | else if (adj_int_rel.r_type == MIPS_R_RELHI) | 
|---|
| 2136 | { | 
|---|
| 2137 | struct internal_reloc rello; | 
|---|
| 2138 | long addhi, addlo; | 
|---|
| 2139 |  | 
|---|
| 2140 | /* The next reloc must be MIPS_R_RELLO, and we handle | 
|---|
| 2141 | them together.  */ | 
|---|
| 2142 | BFD_ASSERT (adj_ext_rel + 1 < ext_rel_end); | 
|---|
| 2143 |  | 
|---|
| 2144 | mips_ecoff_swap_reloc_in (abfd, (PTR) (adj_ext_rel + 1), &rello); | 
|---|
| 2145 |  | 
|---|
| 2146 | BFD_ASSERT (rello.r_type == MIPS_R_RELLO); | 
|---|
| 2147 |  | 
|---|
| 2148 | addhi = bfd_get_32 (abfd, | 
|---|
| 2149 | contents + adj_int_rel.r_vaddr - sec->vma); | 
|---|
| 2150 | addhi &= 0xffff; | 
|---|
| 2151 | if (addhi & 0x8000) | 
|---|
| 2152 | addhi -= 0x10000; | 
|---|
| 2153 | addhi <<= 16; | 
|---|
| 2154 |  | 
|---|
| 2155 | addlo = bfd_get_32 (abfd, contents + rello.r_vaddr - sec->vma); | 
|---|
| 2156 | addlo &= 0xffff; | 
|---|
| 2157 | if (addlo & 0x8000) | 
|---|
| 2158 | addlo -= 0x10000; | 
|---|
| 2159 |  | 
|---|
| 2160 | if (adj_int_rel.r_extern) | 
|---|
| 2161 | { | 
|---|
| 2162 | /* The value we want here is | 
|---|
| 2163 | sym - RELLOaddr + addend | 
|---|
| 2164 | which we can express as | 
|---|
| 2165 | sym - (RELLOaddr - addend) | 
|---|
| 2166 | Therefore if we are expanding the area between | 
|---|
| 2167 | RELLOaddr and RELLOaddr - addend we must adjust | 
|---|
| 2168 | the addend.  This is admittedly ambiguous, since | 
|---|
| 2169 | we might mean (sym + addend) - RELLOaddr, but in | 
|---|
| 2170 | practice we don't, and there is no way to handle | 
|---|
| 2171 | that case correctly since at this point we have | 
|---|
| 2172 | no idea whether any reloc is being expanded | 
|---|
| 2173 | between sym and sym + addend.  */ | 
|---|
| 2174 | start = rello.r_vaddr - (addhi + addlo); | 
|---|
| 2175 | stop = rello.r_vaddr; | 
|---|
| 2176 | } | 
|---|
| 2177 | else | 
|---|
| 2178 | { | 
|---|
| 2179 | /* An internal RELHI/RELLO pair represents the | 
|---|
| 2180 | difference between two addresses, $LC0 - foo. | 
|---|
| 2181 | The symndx value is actually the difference | 
|---|
| 2182 | between the reloc address and $LC0.  This lets us | 
|---|
| 2183 | compute $LC0, and, by considering the addend, | 
|---|
| 2184 | foo.  If the reloc we are expanding falls between | 
|---|
| 2185 | those two relocs, we must adjust the addend.  At | 
|---|
| 2186 | this point, the symndx value is actually in the | 
|---|
| 2187 | r_offset field, where it was put by | 
|---|
| 2188 | mips_ecoff_swap_reloc_in.  */ | 
|---|
| 2189 | start = rello.r_vaddr - adj_int_rel.r_offset; | 
|---|
| 2190 | stop = start + addhi + addlo; | 
|---|
| 2191 | } | 
|---|
| 2192 | } | 
|---|
| 2193 | else if (adj_int_rel.r_type == MIPS_R_SWITCH) | 
|---|
| 2194 | { | 
|---|
| 2195 | /* A MIPS_R_SWITCH reloc represents a word of the form | 
|---|
| 2196 | .word $L3-$LS12 | 
|---|
| 2197 | The value in the object file is correct, assuming the | 
|---|
| 2198 | original value of $L3.  The symndx value is actually | 
|---|
| 2199 | the difference between the reloc address and $LS12. | 
|---|
| 2200 | This lets us compute the original value of $LS12 as | 
|---|
| 2201 | vaddr - symndx | 
|---|
| 2202 | and the original value of $L3 as | 
|---|
| 2203 | vaddr - symndx + addend | 
|---|
| 2204 | where addend is the value from the object file.  At | 
|---|
| 2205 | this point, the symndx value is actually found in the | 
|---|
| 2206 | r_offset field, since it was moved by | 
|---|
| 2207 | mips_ecoff_swap_reloc_in.  */ | 
|---|
| 2208 | start = adj_int_rel.r_vaddr - adj_int_rel.r_offset; | 
|---|
| 2209 | stop = start + bfd_get_32 (abfd, | 
|---|
| 2210 | (contents | 
|---|
| 2211 | + adj_int_rel.r_vaddr | 
|---|
| 2212 | - sec->vma)); | 
|---|
| 2213 | } | 
|---|
| 2214 | else | 
|---|
| 2215 | continue; | 
|---|
| 2216 |  | 
|---|
| 2217 | /* If the range expressed by this reloc, which is the | 
|---|
| 2218 | distance between START and STOP crosses the reloc we are | 
|---|
| 2219 | expanding, we must adjust the offset.  The sign of the | 
|---|
| 2220 | adjustment depends upon the direction in which the range | 
|---|
| 2221 | crosses the reloc being expanded.  */ | 
|---|
| 2222 | if (start <= int_rel.r_vaddr && stop > int_rel.r_vaddr) | 
|---|
| 2223 | change = PCREL16_EXPANSION_ADJUSTMENT; | 
|---|
| 2224 | else if (start > int_rel.r_vaddr && stop <= int_rel.r_vaddr) | 
|---|
| 2225 | change = - PCREL16_EXPANSION_ADJUSTMENT; | 
|---|
| 2226 | else | 
|---|
| 2227 | change = 0; | 
|---|
| 2228 |  | 
|---|
| 2229 | offsets[adj_i] += change; | 
|---|
| 2230 |  | 
|---|
| 2231 | if (adj_int_rel.r_type == MIPS_R_RELHI) | 
|---|
| 2232 | { | 
|---|
| 2233 | adj_ext_rel++; | 
|---|
| 2234 | adj_i++; | 
|---|
| 2235 | offsets[adj_i] += change; | 
|---|
| 2236 | } | 
|---|
| 2237 | } | 
|---|
| 2238 |  | 
|---|
| 2239 | /* Find all symbols in this section defined by this object file | 
|---|
| 2240 | and adjust their values.  Note that we decide whether to | 
|---|
| 2241 | adjust the value based on the value stored in the ECOFF EXTR | 
|---|
| 2242 | structure, because the value stored in the hash table may | 
|---|
| 2243 | have been changed by an earlier expanded reloc and thus may | 
|---|
| 2244 | no longer correctly indicate whether the symbol is before or | 
|---|
| 2245 | after the expanded reloc.  */ | 
|---|
| 2246 | ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax; | 
|---|
| 2247 | adj_h_ptr = ecoff_data (abfd)->sym_hashes; | 
|---|
| 2248 | adj_h_ptr_end = adj_h_ptr + ext_count; | 
|---|
| 2249 | for (; adj_h_ptr < adj_h_ptr_end; adj_h_ptr++) | 
|---|
| 2250 | { | 
|---|
| 2251 | struct ecoff_link_hash_entry *adj_h; | 
|---|
| 2252 |  | 
|---|
| 2253 | adj_h = *adj_h_ptr; | 
|---|
| 2254 | if (adj_h != (struct ecoff_link_hash_entry *) NULL | 
|---|
| 2255 | && (adj_h->root.type == bfd_link_hash_defined | 
|---|
| 2256 | || adj_h->root.type == bfd_link_hash_defweak) | 
|---|
| 2257 | && adj_h->root.u.def.section == sec | 
|---|
| 2258 | && adj_h->esym.asym.value > int_rel.r_vaddr) | 
|---|
| 2259 | adj_h->root.u.def.value += PCREL16_EXPANSION_ADJUSTMENT; | 
|---|
| 2260 | } | 
|---|
| 2261 |  | 
|---|
| 2262 | /* Add an entry to the symbol value adjust list.  This is used | 
|---|
| 2263 | by bfd_ecoff_debug_accumulate to adjust the values of | 
|---|
| 2264 | internal symbols and FDR's.  */ | 
|---|
| 2265 | amt = sizeof (struct ecoff_value_adjust); | 
|---|
| 2266 | adjust = (struct ecoff_value_adjust *) bfd_alloc (abfd, amt); | 
|---|
| 2267 | if (adjust == (struct ecoff_value_adjust *) NULL) | 
|---|
| 2268 | goto error_return; | 
|---|
| 2269 |  | 
|---|
| 2270 | adjust->start = int_rel.r_vaddr; | 
|---|
| 2271 | adjust->end = sec->vma + sec->_raw_size; | 
|---|
| 2272 | adjust->adjust = PCREL16_EXPANSION_ADJUSTMENT; | 
|---|
| 2273 |  | 
|---|
| 2274 | adjust->next = ecoff_data (abfd)->debug_info.adjust; | 
|---|
| 2275 | ecoff_data (abfd)->debug_info.adjust = adjust; | 
|---|
| 2276 | } | 
|---|
| 2277 |  | 
|---|
| 2278 | if (contents != (bfd_byte *) NULL && ! info->keep_memory) | 
|---|
| 2279 | free (contents); | 
|---|
| 2280 |  | 
|---|
| 2281 | return TRUE; | 
|---|
| 2282 |  | 
|---|
| 2283 | error_return: | 
|---|
| 2284 | if (contents != (bfd_byte *) NULL && ! info->keep_memory) | 
|---|
| 2285 | free (contents); | 
|---|
| 2286 | return FALSE; | 
|---|
| 2287 | } | 
|---|
| 2288 |  | 
|---|
| 2289 | /* This routine is called from mips_relocate_section when a PC | 
|---|
| 2290 | relative reloc must be expanded into the five instruction sequence. | 
|---|
| 2291 | It handles all the details of the expansion, including resolving | 
|---|
| 2292 | the reloc.  */ | 
|---|
| 2293 |  | 
|---|
| 2294 | static bfd_boolean | 
|---|
| 2295 | mips_relax_pcrel16 (info, input_bfd, input_section, h, location, address) | 
|---|
| 2296 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | 
|---|
| 2297 | bfd *input_bfd; | 
|---|
| 2298 | asection *input_section ATTRIBUTE_UNUSED; | 
|---|
| 2299 | struct ecoff_link_hash_entry *h; | 
|---|
| 2300 | bfd_byte *location; | 
|---|
| 2301 | bfd_vma address; | 
|---|
| 2302 | { | 
|---|
| 2303 | bfd_vma relocation; | 
|---|
| 2304 |  | 
|---|
| 2305 | /* 0x0411ffff is bgezal $0,. == bal .  */ | 
|---|
| 2306 | BFD_ASSERT (bfd_get_32 (input_bfd, location) == 0x0411ffff); | 
|---|
| 2307 |  | 
|---|
| 2308 | /* We need to compute the distance between the symbol and the | 
|---|
| 2309 | current address plus eight.  */ | 
|---|
| 2310 | relocation = (h->root.u.def.value | 
|---|
| 2311 | + h->root.u.def.section->output_section->vma | 
|---|
| 2312 | + h->root.u.def.section->output_offset); | 
|---|
| 2313 | relocation -= address + 8; | 
|---|
| 2314 |  | 
|---|
| 2315 | /* If the lower half is negative, increment the upper 16 half.  */ | 
|---|
| 2316 | if ((relocation & 0x8000) != 0) | 
|---|
| 2317 | relocation += 0x10000; | 
|---|
| 2318 |  | 
|---|
| 2319 | bfd_put_32 (input_bfd, (bfd_vma) 0x04110001, location); /* bal .+8 */ | 
|---|
| 2320 | bfd_put_32 (input_bfd, | 
|---|
| 2321 | 0x3c010000 | ((relocation >> 16) & 0xffff), /* lui $at,XX */ | 
|---|
| 2322 | location + 4); | 
|---|
| 2323 | bfd_put_32 (input_bfd, | 
|---|
| 2324 | 0x24210000 | (relocation & 0xffff), /* addiu $at,$at,XX */ | 
|---|
| 2325 | location + 8); | 
|---|
| 2326 | bfd_put_32 (input_bfd, | 
|---|
| 2327 | (bfd_vma) 0x003f0821, location + 12); /* addu $at,$at,$ra */ | 
|---|
| 2328 | bfd_put_32 (input_bfd, | 
|---|
| 2329 | (bfd_vma) 0x0020f809, location + 16); /* jalr $at */ | 
|---|
| 2330 |  | 
|---|
| 2331 | return TRUE; | 
|---|
| 2332 | } | 
|---|
| 2333 |  | 
|---|
| 2334 | /* Given a .sdata section and a .rel.sdata in-memory section, store | 
|---|
| 2335 | relocation information into the .rel.sdata section which can be | 
|---|
| 2336 | used at runtime to relocate the section.  This is called by the | 
|---|
| 2337 | linker when the --embedded-relocs switch is used.  This is called | 
|---|
| 2338 | after the add_symbols entry point has been called for all the | 
|---|
| 2339 | objects, and before the final_link entry point is called.  This | 
|---|
| 2340 | function presumes that the object was compiled using | 
|---|
| 2341 | -membedded-pic.  */ | 
|---|
| 2342 |  | 
|---|
| 2343 | bfd_boolean | 
|---|
| 2344 | bfd_mips_ecoff_create_embedded_relocs (abfd, info, datasec, relsec, errmsg) | 
|---|
| 2345 | bfd *abfd; | 
|---|
| 2346 | struct bfd_link_info *info; | 
|---|
| 2347 | asection *datasec; | 
|---|
| 2348 | asection *relsec; | 
|---|
| 2349 | char **errmsg; | 
|---|
| 2350 | { | 
|---|
| 2351 | struct ecoff_link_hash_entry **sym_hashes; | 
|---|
| 2352 | struct ecoff_section_tdata *section_tdata; | 
|---|
| 2353 | struct external_reloc *ext_rel; | 
|---|
| 2354 | struct external_reloc *ext_rel_end; | 
|---|
| 2355 | bfd_byte *p; | 
|---|
| 2356 | bfd_size_type amt; | 
|---|
| 2357 |  | 
|---|
| 2358 | BFD_ASSERT (! info->relocateable); | 
|---|
| 2359 |  | 
|---|
| 2360 | *errmsg = NULL; | 
|---|
| 2361 |  | 
|---|
| 2362 | if (datasec->reloc_count == 0) | 
|---|
| 2363 | return TRUE; | 
|---|
| 2364 |  | 
|---|
| 2365 | sym_hashes = ecoff_data (abfd)->sym_hashes; | 
|---|
| 2366 |  | 
|---|
| 2367 | if (! mips_read_relocs (abfd, datasec)) | 
|---|
| 2368 | return FALSE; | 
|---|
| 2369 |  | 
|---|
| 2370 | amt = (bfd_size_type) datasec->reloc_count * 4; | 
|---|
| 2371 | relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt); | 
|---|
| 2372 | if (relsec->contents == NULL) | 
|---|
| 2373 | return FALSE; | 
|---|
| 2374 |  | 
|---|
| 2375 | p = relsec->contents; | 
|---|
| 2376 |  | 
|---|
| 2377 | section_tdata = ecoff_section_data (abfd, datasec); | 
|---|
| 2378 | ext_rel = (struct external_reloc *) section_tdata->external_relocs; | 
|---|
| 2379 | ext_rel_end = ext_rel + datasec->reloc_count; | 
|---|
| 2380 | for (; ext_rel < ext_rel_end; ext_rel++, p += 4) | 
|---|
| 2381 | { | 
|---|
| 2382 | struct internal_reloc int_rel; | 
|---|
| 2383 | bfd_boolean text_relative; | 
|---|
| 2384 |  | 
|---|
| 2385 | mips_ecoff_swap_reloc_in (abfd, (PTR) ext_rel, &int_rel); | 
|---|
| 2386 |  | 
|---|
| 2387 | /* We are going to write a four byte word into the runtime reloc | 
|---|
| 2388 | section.  The word will be the address in the data section | 
|---|
| 2389 | which must be relocated.  This must be on a word boundary, | 
|---|
| 2390 | which means the lower two bits must be zero.  We use the | 
|---|
| 2391 | least significant bit to indicate how the value in the data | 
|---|
| 2392 | section must be relocated.  A 0 means that the value is | 
|---|
| 2393 | relative to the text section, while a 1 indicates that the | 
|---|
| 2394 | value is relative to the data section.  Given that we are | 
|---|
| 2395 | assuming the code was compiled using -membedded-pic, there | 
|---|
| 2396 | should not be any other possibilities.  */ | 
|---|
| 2397 |  | 
|---|
| 2398 | /* We can only relocate REFWORD relocs at run time.  */ | 
|---|
| 2399 | if (int_rel.r_type != MIPS_R_REFWORD) | 
|---|
| 2400 | { | 
|---|
| 2401 | *errmsg = _("unsupported reloc type"); | 
|---|
| 2402 | bfd_set_error (bfd_error_bad_value); | 
|---|
| 2403 | return FALSE; | 
|---|
| 2404 | } | 
|---|
| 2405 |  | 
|---|
| 2406 | if (int_rel.r_extern) | 
|---|
| 2407 | { | 
|---|
| 2408 | struct ecoff_link_hash_entry *h; | 
|---|
| 2409 |  | 
|---|
| 2410 | h = sym_hashes[int_rel.r_symndx]; | 
|---|
| 2411 | /* If h is NULL, that means that there is a reloc against an | 
|---|
| 2412 | external symbol which we thought was just a debugging | 
|---|
| 2413 | symbol.  This should not happen.  */ | 
|---|
| 2414 | if (h == (struct ecoff_link_hash_entry *) NULL) | 
|---|
| 2415 | abort (); | 
|---|
| 2416 | if ((h->root.type == bfd_link_hash_defined | 
|---|
| 2417 | || h->root.type == bfd_link_hash_defweak) | 
|---|
| 2418 | && (h->root.u.def.section->flags & SEC_CODE) != 0) | 
|---|
| 2419 | text_relative = TRUE; | 
|---|
| 2420 | else | 
|---|
| 2421 | text_relative = FALSE; | 
|---|
| 2422 | } | 
|---|
| 2423 | else | 
|---|
| 2424 | { | 
|---|
| 2425 | switch (int_rel.r_symndx) | 
|---|
| 2426 | { | 
|---|
| 2427 | case RELOC_SECTION_TEXT: | 
|---|
| 2428 | text_relative = TRUE; | 
|---|
| 2429 | break; | 
|---|
| 2430 | case RELOC_SECTION_SDATA: | 
|---|
| 2431 | case RELOC_SECTION_SBSS: | 
|---|
| 2432 | case RELOC_SECTION_LIT8: | 
|---|
| 2433 | text_relative = FALSE; | 
|---|
| 2434 | break; | 
|---|
| 2435 | default: | 
|---|
| 2436 | /* No other sections should appear in -membedded-pic | 
|---|
| 2437 | code.  */ | 
|---|
| 2438 | *errmsg = _("reloc against unsupported section"); | 
|---|
| 2439 | bfd_set_error (bfd_error_bad_value); | 
|---|
| 2440 | return FALSE; | 
|---|
| 2441 | } | 
|---|
| 2442 | } | 
|---|
| 2443 |  | 
|---|
| 2444 | if ((int_rel.r_offset & 3) != 0) | 
|---|
| 2445 | { | 
|---|
| 2446 | *errmsg = _("reloc not properly aligned"); | 
|---|
| 2447 | bfd_set_error (bfd_error_bad_value); | 
|---|
| 2448 | return FALSE; | 
|---|
| 2449 | } | 
|---|
| 2450 |  | 
|---|
| 2451 | bfd_put_32 (abfd, | 
|---|
| 2452 | (int_rel.r_vaddr - datasec->vma + datasec->output_offset | 
|---|
| 2453 | + (text_relative ? 0 : 1)), | 
|---|
| 2454 | p); | 
|---|
| 2455 | } | 
|---|
| 2456 |  | 
|---|
| 2457 | return TRUE; | 
|---|
| 2458 | } | 
|---|
| 2459 |  | 
|---|
| 2460 |  | 
|---|
| 2461 | /* This is the ECOFF backend structure.  The backend field of the | 
|---|
| 2462 | target vector points to this.  */ | 
|---|
| 2463 |  | 
|---|
| 2464 | static const struct ecoff_backend_data mips_ecoff_backend_data = | 
|---|
| 2465 | { | 
|---|
| 2466 | /* COFF backend structure.  */ | 
|---|
| 2467 | { | 
|---|
| 2468 | (void (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR))) bfd_void, /* aux_in */ | 
|---|
| 2469 | (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */ | 
|---|
| 2470 | (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */ | 
|---|
| 2471 | (unsigned (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR)))bfd_void,/*aux_out*/ | 
|---|
| 2472 | (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */ | 
|---|
| 2473 | (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */ | 
|---|
| 2474 | (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* reloc_out */ | 
|---|
| 2475 | mips_ecoff_swap_filehdr_out, mips_ecoff_swap_aouthdr_out, | 
|---|
| 2476 | mips_ecoff_swap_scnhdr_out, | 
|---|
| 2477 | FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, TRUE, FALSE, 4, FALSE, 2, | 
|---|
| 2478 | mips_ecoff_swap_filehdr_in, mips_ecoff_swap_aouthdr_in, | 
|---|
| 2479 | mips_ecoff_swap_scnhdr_in, NULL, | 
|---|
| 2480 | mips_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook, | 
|---|
| 2481 | _bfd_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags, | 
|---|
| 2482 | _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table, | 
|---|
| 2483 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | 
|---|
| 2484 | NULL, NULL | 
|---|
| 2485 | }, | 
|---|
| 2486 | /* Supported architecture.  */ | 
|---|
| 2487 | bfd_arch_mips, | 
|---|
| 2488 | /* Initial portion of armap string.  */ | 
|---|
| 2489 | "__________", | 
|---|
| 2490 | /* The page boundary used to align sections in a demand-paged | 
|---|
| 2491 | executable file.  E.g., 0x1000.  */ | 
|---|
| 2492 | 0x1000, | 
|---|
| 2493 | /* TRUE if the .rdata section is part of the text segment, as on the | 
|---|
| 2494 | Alpha.  FALSE if .rdata is part of the data segment, as on the | 
|---|
| 2495 | MIPS.  */ | 
|---|
| 2496 | FALSE, | 
|---|
| 2497 | /* Bitsize of constructor entries.  */ | 
|---|
| 2498 | 32, | 
|---|
| 2499 | /* Reloc to use for constructor entries.  */ | 
|---|
| 2500 | &mips_howto_table[MIPS_R_REFWORD], | 
|---|
| 2501 | { | 
|---|
| 2502 | /* Symbol table magic number.  */ | 
|---|
| 2503 | magicSym, | 
|---|
| 2504 | /* Alignment of debugging information.  E.g., 4.  */ | 
|---|
| 2505 | 4, | 
|---|
| 2506 | /* Sizes of external symbolic information.  */ | 
|---|
| 2507 | sizeof (struct hdr_ext), | 
|---|
| 2508 | sizeof (struct dnr_ext), | 
|---|
| 2509 | sizeof (struct pdr_ext), | 
|---|
| 2510 | sizeof (struct sym_ext), | 
|---|
| 2511 | sizeof (struct opt_ext), | 
|---|
| 2512 | sizeof (struct fdr_ext), | 
|---|
| 2513 | sizeof (struct rfd_ext), | 
|---|
| 2514 | sizeof (struct ext_ext), | 
|---|
| 2515 | /* Functions to swap in external symbolic data.  */ | 
|---|
| 2516 | ecoff_swap_hdr_in, | 
|---|
| 2517 | ecoff_swap_dnr_in, | 
|---|
| 2518 | ecoff_swap_pdr_in, | 
|---|
| 2519 | ecoff_swap_sym_in, | 
|---|
| 2520 | ecoff_swap_opt_in, | 
|---|
| 2521 | ecoff_swap_fdr_in, | 
|---|
| 2522 | ecoff_swap_rfd_in, | 
|---|
| 2523 | ecoff_swap_ext_in, | 
|---|
| 2524 | _bfd_ecoff_swap_tir_in, | 
|---|
| 2525 | _bfd_ecoff_swap_rndx_in, | 
|---|
| 2526 | /* Functions to swap out external symbolic data.  */ | 
|---|
| 2527 | ecoff_swap_hdr_out, | 
|---|
| 2528 | ecoff_swap_dnr_out, | 
|---|
| 2529 | ecoff_swap_pdr_out, | 
|---|
| 2530 | ecoff_swap_sym_out, | 
|---|
| 2531 | ecoff_swap_opt_out, | 
|---|
| 2532 | ecoff_swap_fdr_out, | 
|---|
| 2533 | ecoff_swap_rfd_out, | 
|---|
| 2534 | ecoff_swap_ext_out, | 
|---|
| 2535 | _bfd_ecoff_swap_tir_out, | 
|---|
| 2536 | _bfd_ecoff_swap_rndx_out, | 
|---|
| 2537 | /* Function to read in symbolic data.  */ | 
|---|
| 2538 | _bfd_ecoff_slurp_symbolic_info | 
|---|
| 2539 | }, | 
|---|
| 2540 | /* External reloc size.  */ | 
|---|
| 2541 | RELSZ, | 
|---|
| 2542 | /* Reloc swapping functions.  */ | 
|---|
| 2543 | mips_ecoff_swap_reloc_in, | 
|---|
| 2544 | mips_ecoff_swap_reloc_out, | 
|---|
| 2545 | /* Backend reloc tweaking.  */ | 
|---|
| 2546 | mips_adjust_reloc_in, | 
|---|
| 2547 | mips_adjust_reloc_out, | 
|---|
| 2548 | /* Relocate section contents while linking.  */ | 
|---|
| 2549 | mips_relocate_section, | 
|---|
| 2550 | /* Do final adjustments to filehdr and aouthdr.  */ | 
|---|
| 2551 | NULL, | 
|---|
| 2552 | /* Read an element from an archive at a given file position.  */ | 
|---|
| 2553 | _bfd_get_elt_at_filepos | 
|---|
| 2554 | }; | 
|---|
| 2555 |  | 
|---|
| 2556 | /* Looking up a reloc type is MIPS specific.  */ | 
|---|
| 2557 | #define _bfd_ecoff_bfd_reloc_type_lookup mips_bfd_reloc_type_lookup | 
|---|
| 2558 |  | 
|---|
| 2559 | /* Getting relocated section contents is generic.  */ | 
|---|
| 2560 | #define _bfd_ecoff_bfd_get_relocated_section_contents \ | 
|---|
| 2561 | bfd_generic_get_relocated_section_contents | 
|---|
| 2562 |  | 
|---|
| 2563 | /* Handling file windows is generic.  */ | 
|---|
| 2564 | #define _bfd_ecoff_get_section_contents_in_window \ | 
|---|
| 2565 | _bfd_generic_get_section_contents_in_window | 
|---|
| 2566 |  | 
|---|
| 2567 | /* Relaxing sections is MIPS specific.  */ | 
|---|
| 2568 | #define _bfd_ecoff_bfd_relax_section mips_relax_section | 
|---|
| 2569 |  | 
|---|
| 2570 | /* GC of sections is not done.  */ | 
|---|
| 2571 | #define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections | 
|---|
| 2572 |  | 
|---|
| 2573 | /* Merging of sections is not done.  */ | 
|---|
| 2574 | #define _bfd_ecoff_bfd_merge_sections bfd_generic_merge_sections | 
|---|
| 2575 |  | 
|---|
| 2576 | #define _bfd_ecoff_bfd_discard_group bfd_generic_discard_group | 
|---|
| 2577 |  | 
|---|
| 2578 | extern const bfd_target ecoff_big_vec; | 
|---|
| 2579 |  | 
|---|
| 2580 | const bfd_target ecoff_little_vec = | 
|---|
| 2581 | { | 
|---|
| 2582 | "ecoff-littlemips",           /* name */ | 
|---|
| 2583 | bfd_target_ecoff_flavour, | 
|---|
| 2584 | BFD_ENDIAN_LITTLE,            /* data byte order is little */ | 
|---|
| 2585 | BFD_ENDIAN_LITTLE,            /* header byte order is little */ | 
|---|
| 2586 |  | 
|---|
| 2587 | (HAS_RELOC | EXEC_P |         /* object flags */ | 
|---|
| 2588 | HAS_LINENO | HAS_DEBUG | | 
|---|
| 2589 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), | 
|---|
| 2590 |  | 
|---|
| 2591 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA), | 
|---|
| 2592 | 0,                            /* leading underscore */ | 
|---|
| 2593 | ' ',                          /* ar_pad_char */ | 
|---|
| 2594 | 15,                           /* ar_max_namelen */ | 
|---|
| 2595 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | 
|---|
| 2596 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | 
|---|
| 2597 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ | 
|---|
| 2598 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | 
|---|
| 2599 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | 
|---|
| 2600 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ | 
|---|
| 2601 |  | 
|---|
| 2602 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */ | 
|---|
| 2603 | _bfd_ecoff_archive_p, _bfd_dummy_target}, | 
|---|
| 2604 | {bfd_false, _bfd_ecoff_mkobject,  /* bfd_set_format */ | 
|---|
| 2605 | _bfd_generic_mkarchive, bfd_false}, | 
|---|
| 2606 | {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */ | 
|---|
| 2607 | _bfd_write_archive_contents, bfd_false}, | 
|---|
| 2608 |  | 
|---|
| 2609 | BFD_JUMP_TABLE_GENERIC (_bfd_ecoff), | 
|---|
| 2610 | BFD_JUMP_TABLE_COPY (_bfd_ecoff), | 
|---|
| 2611 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | 
|---|
| 2612 | BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff), | 
|---|
| 2613 | BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff), | 
|---|
| 2614 | BFD_JUMP_TABLE_RELOCS (_bfd_ecoff), | 
|---|
| 2615 | BFD_JUMP_TABLE_WRITE (_bfd_ecoff), | 
|---|
| 2616 | BFD_JUMP_TABLE_LINK (_bfd_ecoff), | 
|---|
| 2617 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | 
|---|
| 2618 |  | 
|---|
| 2619 | & ecoff_big_vec, | 
|---|
| 2620 |  | 
|---|
| 2621 | (PTR) &mips_ecoff_backend_data | 
|---|
| 2622 | }; | 
|---|
| 2623 |  | 
|---|
| 2624 | const bfd_target ecoff_big_vec = | 
|---|
| 2625 | { | 
|---|
| 2626 | "ecoff-bigmips",              /* name */ | 
|---|
| 2627 | bfd_target_ecoff_flavour, | 
|---|
| 2628 | BFD_ENDIAN_BIG,               /* data byte order is big */ | 
|---|
| 2629 | BFD_ENDIAN_BIG,               /* header byte order is big */ | 
|---|
| 2630 |  | 
|---|
| 2631 | (HAS_RELOC | EXEC_P |         /* object flags */ | 
|---|
| 2632 | HAS_LINENO | HAS_DEBUG | | 
|---|
| 2633 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), | 
|---|
| 2634 |  | 
|---|
| 2635 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA), | 
|---|
| 2636 | 0,                            /* leading underscore */ | 
|---|
| 2637 | ' ',                          /* ar_pad_char */ | 
|---|
| 2638 | 15,                           /* ar_max_namelen */ | 
|---|
| 2639 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | 
|---|
| 2640 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | 
|---|
| 2641 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, | 
|---|
| 2642 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | 
|---|
| 2643 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | 
|---|
| 2644 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, | 
|---|
| 2645 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */ | 
|---|
| 2646 | _bfd_ecoff_archive_p, _bfd_dummy_target}, | 
|---|
| 2647 | {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */ | 
|---|
| 2648 | _bfd_generic_mkarchive, bfd_false}, | 
|---|
| 2649 | {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */ | 
|---|
| 2650 | _bfd_write_archive_contents, bfd_false}, | 
|---|
| 2651 |  | 
|---|
| 2652 | BFD_JUMP_TABLE_GENERIC (_bfd_ecoff), | 
|---|
| 2653 | BFD_JUMP_TABLE_COPY (_bfd_ecoff), | 
|---|
| 2654 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | 
|---|
| 2655 | BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff), | 
|---|
| 2656 | BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff), | 
|---|
| 2657 | BFD_JUMP_TABLE_RELOCS (_bfd_ecoff), | 
|---|
| 2658 | BFD_JUMP_TABLE_WRITE (_bfd_ecoff), | 
|---|
| 2659 | BFD_JUMP_TABLE_LINK (_bfd_ecoff), | 
|---|
| 2660 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | 
|---|
| 2661 |  | 
|---|
| 2662 | & ecoff_little_vec, | 
|---|
| 2663 |  | 
|---|
| 2664 | (PTR) &mips_ecoff_backend_data | 
|---|
| 2665 | }; | 
|---|
| 2666 |  | 
|---|
| 2667 | const bfd_target ecoff_biglittle_vec = | 
|---|
| 2668 | { | 
|---|
| 2669 | "ecoff-biglittlemips",                /* name */ | 
|---|
| 2670 | bfd_target_ecoff_flavour, | 
|---|
| 2671 | BFD_ENDIAN_LITTLE,            /* data byte order is little */ | 
|---|
| 2672 | BFD_ENDIAN_BIG,               /* header byte order is big */ | 
|---|
| 2673 |  | 
|---|
| 2674 | (HAS_RELOC | EXEC_P |         /* object flags */ | 
|---|
| 2675 | HAS_LINENO | HAS_DEBUG | | 
|---|
| 2676 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), | 
|---|
| 2677 |  | 
|---|
| 2678 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA), | 
|---|
| 2679 | 0,                            /* leading underscore */ | 
|---|
| 2680 | ' ',                          /* ar_pad_char */ | 
|---|
| 2681 | 15,                           /* ar_max_namelen */ | 
|---|
| 2682 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | 
|---|
| 2683 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | 
|---|
| 2684 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ | 
|---|
| 2685 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | 
|---|
| 2686 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | 
|---|
| 2687 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ | 
|---|
| 2688 |  | 
|---|
| 2689 | {_bfd_dummy_target, coff_object_p, /* bfd_check_format */ | 
|---|
| 2690 | _bfd_ecoff_archive_p, _bfd_dummy_target}, | 
|---|
| 2691 | {bfd_false, _bfd_ecoff_mkobject,  /* bfd_set_format */ | 
|---|
| 2692 | _bfd_generic_mkarchive, bfd_false}, | 
|---|
| 2693 | {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */ | 
|---|
| 2694 | _bfd_write_archive_contents, bfd_false}, | 
|---|
| 2695 |  | 
|---|
| 2696 | BFD_JUMP_TABLE_GENERIC (_bfd_ecoff), | 
|---|
| 2697 | BFD_JUMP_TABLE_COPY (_bfd_ecoff), | 
|---|
| 2698 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | 
|---|
| 2699 | BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff), | 
|---|
| 2700 | BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff), | 
|---|
| 2701 | BFD_JUMP_TABLE_RELOCS (_bfd_ecoff), | 
|---|
| 2702 | BFD_JUMP_TABLE_WRITE (_bfd_ecoff), | 
|---|
| 2703 | BFD_JUMP_TABLE_LINK (_bfd_ecoff), | 
|---|
| 2704 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | 
|---|
| 2705 |  | 
|---|
| 2706 | NULL, | 
|---|
| 2707 |  | 
|---|
| 2708 | (PTR) &mips_ecoff_backend_data | 
|---|
| 2709 | }; | 
|---|