Home | History | Annotate | Download | only in bfd
      1 /* BFD back-end for MIPS PE COFF files.
      2    Copyright (C) 1990-2014 Free Software Foundation, Inc.
      3    Modified from coff-i386.c by DJ Delorie, dj (at) cygnus.com
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 #define COFF_WITH_PE
     23 /* pei-mips.c may have defined this to default off (0) before
     24   including this file, so don't redefine if that's the case.
     25   Otherwise we're generating objects, not executable images,
     26   so we want to define it to default on.  */
     27 #ifndef COFF_LONG_SECTION_NAMES
     28 #define COFF_LONG_SECTION_NAMES
     29 #endif /* COFF_LONG_SECTION_NAMES */
     30 #define PCRELOFFSET TRUE
     31 
     32 #include "sysdep.h"
     33 #include "bfd.h"
     34 #include "libbfd.h"
     35 #include "coff/mipspe.h"
     36 #include "coff/internal.h"
     37 #include "coff/pe.h"
     38 #include "libcoff.h"
     39 
     40 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
     41 /* The page size is a guess based on ELF.  */
     42 
     43 #define COFF_PAGE_SIZE 0x1000
     44 
     45 /* For some reason when using mips COFF the value stored in the .text
     46    section for a reference to a common symbol is the value itself plus
     47    any desired offset.  Ian Taylor, Cygnus Support.  */
     48 
     49 /* If we are producing relocatable output, we need to do some
     50    adjustments to the object file that are not done by the
     51    bfd_perform_relocation function.  This function is called by every
     52    reloc type to make any required adjustments.  */
     53 
     54 static bfd_reloc_status_type
     55 coff_mips_reloc (bfd *abfd,
     56 		 arelent *reloc_entry,
     57 		 asymbol *symbol,
     58 		 void * data,
     59 		 asection *input_section ATTRIBUTE_UNUSED,
     60 		 bfd *output_bfd,
     61 		 char **error_message ATTRIBUTE_UNUSED)
     62 {
     63   symvalue diff;
     64 
     65   if (output_bfd == NULL)
     66     return bfd_reloc_continue;
     67 
     68   if (bfd_is_com_section (symbol->section))
     69     {
     70 #ifndef COFF_WITH_PE
     71       /* We are relocating a common symbol.  The current value in the
     72 	 object file is ORIG + OFFSET, where ORIG is the value of the
     73 	 common symbol as seen by the object file when it was compiled
     74 	 (this may be zero if the symbol was undefined) and OFFSET is
     75 	 the offset into the common symbol (normally zero, but may be
     76 	 non-zero when referring to a field in a common structure).
     77 	 ORIG is the negative of reloc_entry->addend, which is set by
     78 	 the CALC_ADDEND macro below.  We want to replace the value in
     79 	 the object file with NEW + OFFSET, where NEW is the value of
     80 	 the common symbol which we are going to put in the final
     81 	 object file.  NEW is symbol->value.  */
     82       diff = symbol->value + reloc_entry->addend;
     83 #else
     84       /* In PE mode, we do not offset the common symbol.  */
     85       diff = reloc_entry->addend;
     86 #endif
     87     }
     88   else
     89     /* For some reason bfd_perform_relocation always effectively
     90        ignores the addend for a COFF target when producing
     91        relocatable output.  This seems to be always wrong for 386
     92        COFF, so we handle the addend here instead.  */
     93     diff = reloc_entry->addend;
     94 
     95 #define DOIT(x) \
     96   x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + (diff >> howto->rightshift)) & howto->dst_mask))
     97 
     98     if (diff != 0)
     99       {
    100 	reloc_howto_type *howto = reloc_entry->howto;
    101 	unsigned char *addr = (unsigned char *) data + reloc_entry->address;
    102 
    103 	switch (howto->size)
    104 	  {
    105 	  case 0:
    106 	    {
    107 	      char x = bfd_get_8 (abfd, addr);
    108 
    109 	      DOIT (x);
    110 	      bfd_put_8 (abfd, x, addr);
    111 	    }
    112 	    break;
    113 
    114 	  case 1:
    115 	    {
    116 	      short x = bfd_get_16 (abfd, addr);
    117 
    118 	      DOIT (x);
    119 	      bfd_put_16 (abfd, (bfd_vma) x, addr);
    120 	    }
    121 	    break;
    122 
    123 	  case 2:
    124 	    {
    125 	      long x = bfd_get_32 (abfd, addr);
    126 
    127 	      DOIT (x);
    128 	      bfd_put_32 (abfd, (bfd_vma) x, addr);
    129 	    }
    130 	    break;
    131 
    132 	  default:
    133 	    abort ();
    134 	  }
    135       }
    136 
    137   /* Now let bfd_perform_relocation finish everything up.  */
    138   return bfd_reloc_continue;
    139 }
    140 
    141 #ifdef COFF_WITH_PE
    142 /* Return TRUE if this relocation should
    143    appear in the output .reloc section.  */
    144 
    145 static bfd_boolean
    146 in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED, reloc_howto_type *howto)
    147 {
    148   return ! howto->pc_relative && howto->type != MIPS_R_RVA;
    149 }
    150 #endif
    151 
    152 #ifndef PCRELOFFSET
    153 #define PCRELOFFSET FALSE
    154 #endif
    155 
    156 static reloc_howto_type howto_table[] =
    157 {
    158   /* Reloc type 0 is ignored.  The reloc reading code ensures that
    159      this is a reference to the .abs section, which will cause
    160      bfd_perform_relocation to do nothing.  */
    161   HOWTO (MIPS_R_ABSOLUTE,	/* Type.  */
    162 	 0,			/* Rightshift.  */
    163 	 0,			/* Size (0 = byte, 1 = short, 2 = long).  */
    164 	 8,			/* Bitsize.  */
    165 	 FALSE,			/* PC_relative.  */
    166 	 0,			/* Bitpos. */
    167 	 complain_overflow_dont, /* Complain_on_overflow. */
    168 	 0,			/* Special_function. */
    169 	 "IGNORE",		/* Name. */
    170 	 FALSE,			/* Partial_inplace. */
    171 	 0,			/* Src_mask. */
    172 	 0,			/* Dst_mask. */
    173 	 FALSE),		/* Pcrel_offset. */
    174 
    175   /* A 16 bit reference to a symbol, normally from a data section.  */
    176   HOWTO (MIPS_R_REFHALF,	/* Type.  */
    177 	 0,			/* Rightshift.  */
    178 	 1,			/* Size (0 = byte, 1 = short, 2 = long).  */
    179 	 16,			/* Bitsize.  */
    180 	 FALSE,			/* PC_relative.  */
    181 	 0,			/* Bitpos. */
    182 	 complain_overflow_bitfield, /* Complain_on_overflow. */
    183 	 coff_mips_reloc,	/* Special_function. */
    184 	 "REFHALF",		/* Name. */
    185 	 TRUE,			/* Partial_inplace. */
    186 	 0xffff,		/* Src_mask. */
    187 	 0xffff,		/* Dst_mask. */
    188 	 FALSE),		/* Pcrel_offset. */
    189 
    190   /* A 32 bit reference to a symbol, normally from a data section.  */
    191   HOWTO (MIPS_R_REFWORD,	/* Type.  */
    192 	 0,			/* Rightshift.  */
    193 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
    194 	 32,			/* Bitsize.  */
    195 	 FALSE,			/* PC_relative.  */
    196 	 0,			/* Bitpos. */
    197 	 complain_overflow_bitfield, /* Complain_on_overflow. */
    198 	 coff_mips_reloc,	/* Special_function. */
    199 	 "REFWORD",		/* Name. */
    200 	 TRUE,			/* Partial_inplace. */
    201 	 0xffffffff,		/* Src_mask. */
    202 	 0xffffffff,		/* Dst_mask. */
    203 	 FALSE),		/* Pcrel_offset. */
    204 
    205   /* A 26 bit absolute jump address.  */
    206   HOWTO (MIPS_R_JMPADDR,	/* Type.  */
    207 	 2,			/* Rightshift.  */
    208 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
    209 	 26,			/* Bitsize.  */
    210 	 FALSE,			/* PC_relative.  */
    211 	 0,			/* Bitpos. */
    212 	 complain_overflow_dont, /* Complain_on_overflow. */
    213 	 			/* This needs complex overflow
    214 				   detection, because the upper four
    215 				   bits must match the PC.  */
    216 	 coff_mips_reloc,	/* Special_function. */
    217 	 "JMPADDR",		/* Name. */
    218 	 TRUE,			/* Partial_inplace. */
    219 	 0x3ffffff,		/* Src_mask. */
    220 	 0x3ffffff,		/* Dst_mask. */
    221 	 FALSE),		/* Pcrel_offset. */
    222 
    223   /* The high 16 bits of a symbol value.  Handled by the function
    224      mips_refhi_reloc.  */
    225   HOWTO (MIPS_R_REFHI,		/* Type.  */
    226 	 16,			/* Rightshift.  */
    227 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
    228 	 16,			/* Bitsize.  */
    229 	 FALSE,			/* PC_relative.  */
    230 	 0,			/* Bitpos. */
    231 	 complain_overflow_bitfield, /* Complain_on_overflow. */
    232 	 coff_mips_reloc,	/* Special_function. */
    233 	 "REFHI",		/* Name. */
    234 	 TRUE,			/* Partial_inplace. */
    235 	 0xffff,		/* Src_mask. */
    236 	 0xffff,		/* Dst_mask. */
    237 	 FALSE),		/* Pcrel_offset. */
    238 
    239   /* The low 16 bits of a symbol value.  */
    240   HOWTO (MIPS_R_REFLO,		/* Type.  */
    241 	 0,			/* Rightshift.  */
    242 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
    243 	 16,			/* Bitsize.  */
    244 	 FALSE,			/* PC_relative.  */
    245 	 0,			/* Bitpos. */
    246 	 complain_overflow_dont, /* Complain_on_overflow. */
    247 	 coff_mips_reloc,	/* Special_function. */
    248 	 "REFLO",		/* Name. */
    249 	 TRUE,			/* Partial_inplace. */
    250 	 0xffff,		/* Src_mask. */
    251 	 0xffff,		/* Dst_mask. */
    252 	 FALSE),		/* Pcrel_offset. */
    253 
    254   /* A reference to an offset from the gp register.  Handled by the
    255      function mips_gprel_reloc.  */
    256   HOWTO (MIPS_R_GPREL,		/* Type.  */
    257 	 0,			/* Rightshift.  */
    258 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
    259 	 16,			/* Bitsize.  */
    260 	 FALSE,			/* PC_relative.  */
    261 	 0,			/* Bitpos. */
    262 	 complain_overflow_signed, /* Complain_on_overflow. */
    263 	 coff_mips_reloc,	/* Special_function. */
    264 	 "GPREL",		/* Name. */
    265 	 TRUE,			/* Partial_inplace. */
    266 	 0xffff,		/* Src_mask. */
    267 	 0xffff,		/* Dst_mask. */
    268 	 FALSE),		/* Pcrel_offset. */
    269 
    270   /* A reference to a literal using an offset from the gp register.
    271      Handled by the function mips_gprel_reloc.  */
    272   HOWTO (MIPS_R_LITERAL,	/* Type.  */
    273 	 0,			/* Rightshift.  */
    274 	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
    275 	 16,			/* Bitsize.  */
    276 	 FALSE,			/* PC_relative.  */
    277 	 0,			/* Bitpos. */
    278 	 complain_overflow_signed, /* Complain_on_overflow. */
    279 	 coff_mips_reloc,	/* Special_function. */
    280 	 "LITERAL",		/* Name. */
    281 	 TRUE,			/* Partial_inplace. */
    282 	 0xffff,		/* Src_mask. */
    283 	 0xffff,		/* Dst_mask. */
    284 	 FALSE),		/* Pcrel_offset. */
    285 
    286   EMPTY_HOWTO (8),
    287   EMPTY_HOWTO (9),
    288   EMPTY_HOWTO (10),
    289   EMPTY_HOWTO (11),
    290   EMPTY_HOWTO (12),
    291   EMPTY_HOWTO (13),
    292   EMPTY_HOWTO (14),
    293   EMPTY_HOWTO (15),
    294   EMPTY_HOWTO (16),
    295   EMPTY_HOWTO (17),
    296   EMPTY_HOWTO (18),
    297   EMPTY_HOWTO (19),
    298   EMPTY_HOWTO (20),
    299   EMPTY_HOWTO (21),
    300   EMPTY_HOWTO (22),
    301   EMPTY_HOWTO (23),
    302   EMPTY_HOWTO (24),
    303   EMPTY_HOWTO (25),
    304   EMPTY_HOWTO (26),
    305   EMPTY_HOWTO (27),
    306   EMPTY_HOWTO (28),
    307   EMPTY_HOWTO (29),
    308   EMPTY_HOWTO (30),
    309   EMPTY_HOWTO (31),
    310   EMPTY_HOWTO (32),
    311   EMPTY_HOWTO (33),
    312   HOWTO (MIPS_R_RVA,            /* Type.  */
    313 	 0,	                /* Rightshift.  */
    314 	 2,	                /* Size (0 = byte, 1 = short, 2 = long).  */
    315 	 32,	                /* Bitsize.  */
    316 	 FALSE,	                /* PC_relative.  */
    317 	 0,	                /* Bitpos. */
    318 	 complain_overflow_bitfield, /* Complain_on_overflow. */
    319 	 coff_mips_reloc,       /* Special_function. */
    320 	 "rva32",	        /* Name. */
    321 	 TRUE,	                /* Partial_inplace. */
    322 	 0xffffffff,            /* Src_mask. */
    323 	 0xffffffff,            /* Dst_mask. */
    324 	 FALSE),                /* Pcrel_offset. */
    325   EMPTY_HOWTO (35),
    326   EMPTY_HOWTO (36),
    327   HOWTO (MIPS_R_PAIR,           /* Type.  */
    328 	 0,	                /* Rightshift.  */
    329 	 2,	                /* Size (0 = byte, 1 = short, 2 = long).  */
    330 	 32,	                /* Bitsize.  */
    331 	 FALSE,	                /* PC_relative.  */
    332 	 0,	                /* Bitpos. */
    333 	 complain_overflow_bitfield, /* Complain_on_overflow. */
    334 	 coff_mips_reloc,       /* Special_function. */
    335 	 "PAIR",	        /* Name. */
    336 	 TRUE,	                /* Partial_inplace. */
    337 	 0xffffffff,            /* Src_mask. */
    338 	 0xffffffff,            /* Dst_mask. */
    339 	 FALSE),                /* Pcrel_offset. */
    340 };
    341 
    342 #define NUM_HOWTOS (sizeof (howto_table) / sizeof (howto_table[0]))
    343 
    344 /* Turn a howto into a reloc nunmber.  */
    345 
    346 #define SELECT_RELOC(x, howto) { x.r_type = howto->type; }
    347 #define BADMAG(x)              MIPSBADMAG (x)
    348 
    349 /* Customize coffcode.h.  */
    350 #define MIPS 1
    351 
    352 #define RTYPE2HOWTO(cache_ptr, dst) \
    353 	    (cache_ptr)->howto = howto_table + (dst)->r_type;
    354 
    355 /* Compute the addend of a reloc.  If the reloc is to a common symbol,
    356    the object file contains the value of the common symbol.  By the
    357    time this is called, the linker may be using a different symbol
    358    from a different object file with a different value.  Therefore, we
    359    hack wildly to locate the original symbol from this file so that we
    360    can make the correct adjustment.  This macro sets coffsym to the
    361    symbol from the original file, and uses it to set the addend value
    362    correctly.  If this is not a common symbol, the usual addend
    363    calculation is done, except that an additional tweak is needed for
    364    PC relative relocs.
    365    FIXME: This macro refers to symbols and asect; these are from the
    366    calling function, not the macro arguments.  */
    367 
    368 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)		\
    369   {								\
    370     coff_symbol_type *coffsym = NULL;				\
    371     if (ptr && bfd_asymbol_bfd (ptr) != abfd)			\
    372       coffsym = (obj_symbols (abfd)				\
    373 	         + (cache_ptr->sym_ptr_ptr - symbols));		\
    374     else if (ptr)						\
    375       coffsym = coff_symbol_from (abfd, ptr);			\
    376     if (coffsym != NULL						\
    377 	&& coffsym->native->u.syment.n_scnum == 0)		\
    378       cache_ptr->addend = - coffsym->native->u.syment.n_value;	\
    379     else if (ptr && bfd_asymbol_bfd (ptr) == abfd		\
    380 	     && ptr->section != NULL)				\
    381       cache_ptr->addend = - (ptr->section->vma + ptr->value);	\
    382     else							\
    383       cache_ptr->addend = 0;					\
    384     if (ptr && reloc.r_type < NUM_HOWTOS			\
    385 	&& howto_table[reloc.r_type].pc_relative)		\
    386       cache_ptr->addend += asect->vma;				\
    387   }
    388 
    389 /* Convert an rtype to howto for the COFF backend linker.  */
    390 
    391 static reloc_howto_type *
    392 coff_mips_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
    393 			  asection *sec,
    394 			  struct internal_reloc *rel,
    395 			  struct coff_link_hash_entry *h,
    396 			  struct internal_syment *sym,
    397 			  bfd_vma *addendp)
    398 {
    399 
    400   reloc_howto_type *howto;
    401 
    402   howto = howto_table + rel->r_type;
    403 
    404 #ifdef COFF_WITH_PE
    405   *addendp = 0;
    406 #endif
    407 
    408   if (howto->pc_relative)
    409     *addendp += sec->vma;
    410 
    411   if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
    412     {
    413       /* This is a common symbol.  The section contents include the
    414 	 size (sym->n_value) as an addend.  The relocate_section
    415 	 function will be adding in the final value of the symbol.  We
    416 	 need to subtract out the current size in order to get the
    417 	 correct result.  */
    418 
    419       BFD_ASSERT (h != NULL);
    420 
    421 #ifndef COFF_WITH_PE
    422       /* I think we *do* want to bypass this.  If we don't, I have
    423 	 seen some data parameters get the wrong relocation address.
    424 	 If I link two versions with and without this section bypassed
    425 	 and then do a binary comparison, the addresses which are
    426 	 different can be looked up in the map.  The case in which
    427 	 this section has been bypassed has addresses which correspond
    428 	 to values I can find in the map.  */
    429       *addendp -= sym->n_value;
    430 #endif
    431     }
    432 
    433 #ifndef COFF_WITH_PE
    434   /* If the output symbol is common (in which case this must be a
    435      relocatable link), we need to add in the final size of the
    436      common symbol.  */
    437   if (h != NULL && h->root.type == bfd_link_hash_common)
    438     *addendp += h->root.u.c.size;
    439 #endif
    440 
    441 #ifdef COFF_WITH_PE
    442   if (howto->pc_relative)
    443     {
    444       *addendp -= 4;
    445 
    446       /* If the symbol is defined, then the generic code is going to
    447          add back the symbol value in order to cancel out an
    448          adjustment it made to the addend.  However, we set the addend
    449          to 0 at the start of this function.  We need to adjust here,
    450          to avoid the adjustment the generic code will make.  FIXME:
    451          This is getting a bit hackish.  */
    452       if (sym != NULL && sym->n_scnum != 0)
    453 	*addendp -= sym->n_value;
    454     }
    455 
    456   if (rel->r_type == MIPS_R_RVA)
    457     *addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
    458 #endif
    459 
    460   return howto;
    461 }
    462 
    463 #define coff_rtype_to_howto         coff_mips_rtype_to_howto
    464 #define coff_bfd_reloc_type_lookup  coff_mips_reloc_type_lookup
    465 #define coff_bfd_reloc_name_lookup coff_mips_reloc_name_lookup
    466 
    467 /* Get the howto structure for a generic reloc type.  */
    468 
    469 static reloc_howto_type *
    470 coff_mips_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    471 			     bfd_reloc_code_real_type code)
    472 {
    473   int mips_type;
    474 
    475   switch (code)
    476     {
    477     case BFD_RELOC_16:
    478       mips_type = MIPS_R_REFHALF;
    479       break;
    480     case BFD_RELOC_32:
    481     case BFD_RELOC_CTOR:
    482       mips_type = MIPS_R_REFWORD;
    483       break;
    484     case BFD_RELOC_MIPS_JMP:
    485       mips_type = MIPS_R_JMPADDR;
    486       break;
    487     case BFD_RELOC_HI16_S:
    488       mips_type = MIPS_R_REFHI;
    489       break;
    490     case BFD_RELOC_LO16:
    491       mips_type = MIPS_R_REFLO;
    492       break;
    493     case BFD_RELOC_GPREL16:
    494       mips_type = MIPS_R_GPREL;
    495       break;
    496     case BFD_RELOC_MIPS_LITERAL:
    497       mips_type = MIPS_R_LITERAL;
    498       break;
    499     case BFD_RELOC_RVA:
    500       mips_type = MIPS_R_RVA;
    501       break;
    502     default:
    503       return NULL;
    504     }
    505 
    506   return & howto_table [mips_type];
    507 }
    508 
    509 static reloc_howto_type *
    510 coff_mips_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    511 			     const char *r_name)
    512 {
    513   unsigned int i;
    514 
    515   for (i = 0; i < NUM_HOWTOS; i++)
    516     if (howto_table[i].name != NULL
    517 	&& strcasecmp (howto_table[i].name, r_name) == 0)
    518       return &howto_table[i];
    519 
    520   return NULL;
    521 }
    522 
    523 static void
    524 mips_swap_reloc_in (bfd * abfd, void * src, void * dst)
    525 {
    526   static struct internal_reloc pair_prev;
    527   RELOC *reloc_src = (RELOC *) src;
    528   struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
    529 
    530   reloc_dst->r_vaddr = H_GET_32 (abfd, reloc_src->r_vaddr);
    531   reloc_dst->r_symndx = H_GET_S32 (abfd, reloc_src->r_symndx);
    532   reloc_dst->r_type = H_GET_16 (abfd, reloc_src->r_type);
    533   reloc_dst->r_size = 0;
    534   reloc_dst->r_extern = 0;
    535   reloc_dst->r_offset = 0;
    536 
    537   switch (reloc_dst->r_type)
    538   {
    539   case MIPS_R_REFHI:
    540     pair_prev = *reloc_dst;
    541     break;
    542   case MIPS_R_PAIR:
    543     reloc_dst->r_offset = reloc_dst->r_symndx;
    544     if (reloc_dst->r_offset & 0x8000)
    545       reloc_dst->r_offset -= 0x10000;
    546     reloc_dst->r_symndx = pair_prev.r_symndx;
    547     break;
    548   }
    549 }
    550 
    551 static unsigned int
    552 mips_swap_reloc_out (bfd * abfd, void * src, void * dst)
    553 {
    554   static bfd_vma prev_addr = 0;
    555   struct internal_reloc *reloc_src = (struct internal_reloc *)src;
    556   struct external_reloc *reloc_dst = (struct external_reloc *)dst;
    557 
    558   switch (reloc_src->r_type)
    559     {
    560     case MIPS_R_REFHI:
    561       prev_addr = reloc_src->r_vaddr;
    562       break;
    563     case MIPS_R_REFLO:
    564       if (reloc_src->r_vaddr == prev_addr)
    565 	{
    566 	  /* FIXME: only slightly hackish.  If we see a REFLO pointing to
    567 	     the same address as a REFHI, we assume this is the matching
    568 	     PAIR reloc and output it accordingly.  The symndx is really
    569 	     the low 16 bits of the addend */
    570 	  H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr);
    571 	  H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx);
    572 	  H_PUT_16 (abfd, MIPS_R_PAIR, reloc_dst->r_type);
    573 	  return RELSZ;
    574 	}
    575       break;
    576     }
    577 
    578   H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr);
    579   H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx);
    580 
    581   H_PUT_16 (abfd, reloc_src->r_type, reloc_dst->r_type);
    582   return RELSZ;
    583 }
    584 
    585 #define coff_swap_reloc_in   mips_swap_reloc_in
    586 #define coff_swap_reloc_out  mips_swap_reloc_out
    587 #define NO_COFF_RELOCS
    588 
    589 static bfd_boolean
    590 coff_pe_mips_relocate_section (bfd *output_bfd,
    591 			       struct bfd_link_info *info,
    592 			       bfd *input_bfd,
    593 			       asection *input_section,
    594 			       bfd_byte *contents,
    595 			       struct internal_reloc *relocs,
    596 			       struct internal_syment *syms,
    597 			       asection **sections)
    598 {
    599   struct internal_reloc *rel;
    600   struct internal_reloc *rel_end;
    601   unsigned int i;
    602 
    603   if (info->relocatable)
    604     {
    605       (*_bfd_error_handler)
    606 	(_("%B: `ld -r' not supported with PE MIPS objects\n"), input_bfd);
    607       bfd_set_error (bfd_error_bad_value);
    608       return FALSE;
    609     }
    610 
    611   BFD_ASSERT (input_bfd->xvec->byteorder
    612 	      == output_bfd->xvec->byteorder);
    613 
    614   rel = relocs;
    615   rel_end = rel + input_section->reloc_count;
    616 
    617   for (i = 0; rel < rel_end; rel++, i++)
    618     {
    619       long symndx;
    620       struct coff_link_hash_entry *h;
    621       struct internal_syment *sym;
    622       bfd_vma addend = 0;
    623       bfd_vma val, tmp, targ, src, low;
    624       reloc_howto_type *howto;
    625       unsigned char *mem = contents + rel->r_vaddr;
    626 
    627       symndx = rel->r_symndx;
    628 
    629       if (symndx == -1)
    630 	{
    631 	  h = NULL;
    632 	  sym = NULL;
    633 	}
    634       else
    635 	{
    636 	  h = obj_coff_sym_hashes (input_bfd)[symndx];
    637 	  sym = syms + symndx;
    638 	}
    639 
    640       /* COFF treats common symbols in one of two ways.  Either the
    641          size of the symbol is included in the section contents, or it
    642          is not.  We assume that the size is not included, and force
    643          the rtype_to_howto function to adjust the addend as needed.  */
    644 
    645       if (sym != NULL && sym->n_scnum != 0)
    646 	addend = - sym->n_value;
    647       else
    648 	addend = 0;
    649 
    650       howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
    651 				       sym, &addend);
    652       if (howto == NULL)
    653 	return FALSE;
    654 
    655       /* If we are doing a relocatable link, then we can just ignore
    656          a PC relative reloc that is pcrel_offset.  It will already
    657          have the correct value.  If this is not a relocatable link,
    658          then we should ignore the symbol value.  */
    659       if (howto->pc_relative && howto->pcrel_offset)
    660 	{
    661 	  if (info->relocatable)
    662 	    continue;
    663 	  if (sym != NULL && sym->n_scnum != 0)
    664 	    addend += sym->n_value;
    665 	}
    666 
    667       val = 0;
    668 
    669       if (h == NULL)
    670 	{
    671 	  asection *sec;
    672 
    673 	  if (symndx == -1)
    674 	    {
    675 	      sec = bfd_abs_section_ptr;
    676 	      val = 0;
    677 	    }
    678 	  else
    679 	    {
    680 	      sec = sections[symndx];
    681               val = (sec->output_section->vma
    682 		     + sec->output_offset
    683 		     + sym->n_value);
    684 	      if (! obj_pe (input_bfd))
    685 		val -= sec->vma;
    686 	    }
    687 	}
    688       else
    689 	{
    690 	  if (h->root.type == bfd_link_hash_defined
    691 	      || h->root.type == bfd_link_hash_defweak)
    692 	    {
    693 	      asection *sec;
    694 
    695 	      sec = h->root.u.def.section;
    696 	      val = (h->root.u.def.value
    697 		     + sec->output_section->vma
    698 		     + sec->output_offset);
    699 	      }
    700 
    701 	  else if (! info->relocatable)
    702 	    {
    703 	      if (! ((*info->callbacks->undefined_symbol)
    704 		     (info, h->root.root.string, input_bfd, input_section,
    705 		      rel->r_vaddr - input_section->vma, TRUE)))
    706 		return FALSE;
    707 	    }
    708 	}
    709 
    710       src = rel->r_vaddr + input_section->output_section->vma
    711 	+ input_section->output_offset;
    712 
    713       /* OK, at this point the following variables are set up:
    714 	   src = VMA of the memory we're fixing up
    715 	   mem = pointer to memory we're fixing up
    716 	   val = VMA of what we need to refer to.  */
    717 
    718 #define UI(x) (*_bfd_error_handler) (_("%B: unimplemented %s\n"), \
    719 				     input_bfd, x); \
    720 	      bfd_set_error (bfd_error_bad_value);
    721 
    722       switch (rel->r_type)
    723 	{
    724 	case MIPS_R_ABSOLUTE:
    725 	  /* Ignore these.  */
    726 	  break;
    727 
    728 	case MIPS_R_REFHALF:
    729 	  UI ("refhalf");
    730 	  break;
    731 
    732 	case MIPS_R_REFWORD:
    733 	  tmp = bfd_get_32 (input_bfd, mem);
    734 	  /* printf ("refword: src=%08x targ=%08x+%08x\n", src, tmp, val); */
    735 	  tmp += val;
    736 	  bfd_put_32 (input_bfd, tmp, mem);
    737 	  break;
    738 
    739 	case MIPS_R_JMPADDR:
    740 	  tmp = bfd_get_32 (input_bfd, mem);
    741 	  targ = val + (tmp & 0x03ffffff) * 4;
    742 	  if ((src & 0xf0000000) != (targ & 0xf0000000))
    743 	    {
    744 	      (*_bfd_error_handler) (_("%B: jump too far away\n"), input_bfd);
    745 	      bfd_set_error (bfd_error_bad_value);
    746 	      return FALSE;
    747 	    }
    748 	  tmp &= 0xfc000000;
    749 	  tmp |= (targ / 4) & 0x3ffffff;
    750 	  bfd_put_32 (input_bfd, tmp, mem);
    751 	  break;
    752 
    753 	case MIPS_R_REFHI:
    754 	  tmp = bfd_get_32 (input_bfd, mem);
    755 	  switch (rel[1].r_type)
    756 	    {
    757 	    case MIPS_R_PAIR:
    758 	      /* MS PE object */
    759 	      targ = val + rel[1].r_offset + ((tmp & 0xffff) << 16);
    760 	      break;
    761 	    case MIPS_R_REFLO:
    762 	      /* GNU COFF object */
    763 	      low = bfd_get_32 (input_bfd, contents + rel[1].r_vaddr);
    764 	      low &= 0xffff;
    765 	      if (low & 0x8000)
    766 		low -= 0x10000;
    767 	      targ = val + low + ((tmp & 0xffff) << 16);
    768 	      break;
    769 	    default:
    770 	      (*_bfd_error_handler) (_("%B: bad pair/reflo after refhi\n"),
    771 				     input_bfd);
    772 	      bfd_set_error (bfd_error_bad_value);
    773 	      return FALSE;
    774 	    }
    775 	  tmp &= 0xffff0000;
    776 	  tmp |= (targ >> 16) & 0xffff;
    777 	  bfd_put_32 (input_bfd, tmp, mem);
    778 	  break;
    779 
    780 	case MIPS_R_REFLO:
    781 	  tmp = bfd_get_32 (input_bfd, mem);
    782 	  targ = val + (tmp & 0xffff);
    783 	  /* printf ("refword: src=%08x targ=%08x\n", src, targ); */
    784 	  tmp &= 0xffff0000;
    785 	  tmp |= targ & 0xffff;
    786 	  bfd_put_32 (input_bfd, tmp, mem);
    787 	  break;
    788 
    789 	case MIPS_R_GPREL:
    790 	case MIPS_R_LITERAL:
    791 	  UI ("gprel");
    792 	  break;
    793 
    794 	case MIPS_R_SECTION:
    795 	  UI ("section");
    796 	  break;
    797 
    798 	case MIPS_R_SECREL:
    799 	  UI ("secrel");
    800 	  break;
    801 
    802 	case MIPS_R_SECRELLO:
    803 	  UI ("secrello");
    804 	  break;
    805 
    806 	case MIPS_R_SECRELHI:
    807 	  UI ("secrelhi");
    808 	  break;
    809 
    810 	case MIPS_R_RVA:
    811 	  tmp = bfd_get_32 (input_bfd, mem);
    812 	  /* printf ("rva: src=%08x targ=%08x+%08x\n", src, tmp, val); */
    813 	  tmp += val
    814 	    - pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase;
    815 	  bfd_put_32 (input_bfd, tmp, mem);
    816 	  break;
    817 
    818 	case MIPS_R_PAIR:
    819 	  /* ignore these */
    820 	  break;
    821 	}
    822     }
    823 
    824   return TRUE;
    825 }
    826 
    827 #define coff_relocate_section coff_pe_mips_relocate_section
    828 
    829 #ifdef TARGET_UNDERSCORE
    830 
    831 /* If mips gcc uses underscores for symbol names, then it does not use
    832    a leading dot for local labels, so if TARGET_UNDERSCORE is defined
    833    we treat all symbols starting with L as local.  */
    834 
    835 static bfd_boolean
    836 coff_mips_is_local_label_name (bfd *abfd, const char *name)
    837 {
    838   if (name[0] == 'L')
    839     return TRUE;
    840 
    841   return _bfd_coff_is_local_label_name (abfd, name);
    842 }
    843 
    844 #define coff_bfd_is_local_label_name coff_mips_is_local_label_name
    845 
    846 #endif /* TARGET_UNDERSCORE */
    847 
    848 #define COFF_NO_HACK_SCNHDR_SIZE
    849 
    850 #ifndef bfd_pe_print_pdata
    851 #define bfd_pe_print_pdata	NULL
    852 #endif
    853 
    854 #include "coffcode.h"
    855 
    856 const bfd_target
    857 #ifdef TARGET_SYM
    858   TARGET_SYM =
    859 #else
    860   mips_pe_le_vec =
    861 #endif
    862 {
    863 #ifdef TARGET_NAME
    864   TARGET_NAME,
    865 #else
    866   "pe-mips",			/* Name.  */
    867 #endif
    868   bfd_target_coff_flavour,
    869   BFD_ENDIAN_LITTLE,		/* Data byte order is little.  */
    870   BFD_ENDIAN_LITTLE,		/* Header byte order is little.  */
    871 
    872   (HAS_RELOC | EXEC_P |		/* Object flags.  */
    873    HAS_LINENO | HAS_DEBUG |
    874    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
    875 
    876 #ifndef COFF_WITH_PE
    877   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags.  */
    878    | SEC_CODE | SEC_DATA),
    879 #else
    880   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags.  */
    881    | SEC_CODE | SEC_DATA
    882    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
    883 #endif
    884 
    885 #ifdef TARGET_UNDERSCORE
    886   TARGET_UNDERSCORE,		/* Leading underscore.  */
    887 #else
    888   0,				/* leading underscore */
    889 #endif
    890   '/',				/* AR_pad_char.  */
    891   15,				/* AR_max_namelen.  */
    892   0,				/* match priority.  */
    893 
    894   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
    895      bfd_getl32, bfd_getl_signed_32, bfd_putl32,
    896      bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data.  */
    897   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
    898      bfd_getl32, bfd_getl_signed_32, bfd_putl32,
    899      bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Headers.  */
    900 
    901   /* Note that we allow an object file to be treated as a core file as well.  */
    902   {_bfd_dummy_target, coff_object_p, /* bfd_check_format.  */
    903    bfd_generic_archive_p, coff_object_p},
    904   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format.  */
    905    bfd_false},
    906   {bfd_false, coff_write_object_contents, /* bfd_write_contents.  */
    907    _bfd_write_archive_contents, bfd_false},
    908 
    909   BFD_JUMP_TABLE_GENERIC (coff),
    910   BFD_JUMP_TABLE_COPY (coff),
    911   BFD_JUMP_TABLE_CORE (_bfd_nocore),
    912   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
    913   BFD_JUMP_TABLE_SYMBOLS (coff),
    914   BFD_JUMP_TABLE_RELOCS (coff),
    915   BFD_JUMP_TABLE_WRITE (coff),
    916   BFD_JUMP_TABLE_LINK (coff),
    917   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
    918 
    919   NULL,
    920 
    921   COFF_SWAP_TABLE
    922 };
    923