pe-mips.c revision 179407
1327Sjkh/* BFD back-end for MIPS PE COFF files.
2327Sjkh   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3327Sjkh   2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4327Sjkh   Modified from coff-i386.c by DJ Delorie, dj@cygnus.com
5327Sjkh
6327SjkhThis file is part of BFD, the Binary File Descriptor library.
7327Sjkh
8327SjkhThis program is free software; you can redistribute it and/or modify
9327Sjkhit under the terms of the GNU General Public License as published by
10327Sjkhthe Free Software Foundation; either version 2 of the License, or
11327Sjkh(at your option) any later version.
12327Sjkh
13327SjkhThis program is distributed in the hope that it will be useful,
14327Sjkhbut WITHOUT ANY WARRANTY; without even the implied warranty of
15327SjkhMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16327SjkhGNU General Public License for more details.
17327Sjkh
18327SjkhYou should have received a copy of the GNU General Public License
19327Sjkhalong with this program; if not, write to the Free Software
20327SjkhFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2193520Sobrien
2293520Sobrien#define COFF_WITH_PE
2393520Sobrien#define COFF_LONG_SECTION_NAMES
24327Sjkh#define PCRELOFFSET TRUE
2530221Scharnier
2617338Sjkh#include "bfd.h"
27327Sjkh#include "sysdep.h"
28327Sjkh#include "libbfd.h"
29327Sjkh
3084745Ssobomax#include "coff/mipspe.h"
31327Sjkh
32327Sjkh#include "coff/internal.h"
33327Sjkh
34327Sjkh#include "coff/pe.h"
35327Sjkh
36327Sjkh#include "libcoff.h"
37327Sjkh
38327Sjkhstatic bfd_reloc_status_type coff_mips_reloc
39327Sjkh  PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
40327Sjkhstatic reloc_howto_type *coff_mips_rtype_to_howto
41327Sjkh  PARAMS ((bfd *, asection *, struct internal_reloc *,
42327Sjkh	   struct coff_link_hash_entry *, struct internal_syment *,
43327Sjkh	   bfd_vma *));
44327Sjkh#if 0
4596030Ssobomaxstatic void mips_ecoff_swap_reloc_in
4696030Ssobomax  PARAMS ((bfd *, PTR, struct internal_reloc *));
4796030Ssobomaxstatic void mips_ecoff_swap_reloc_out
4896030Ssobomax  PARAMS ((bfd *, const struct internal_reloc *, PTR));
4996030Ssobomaxstatic void mips_adjust_reloc_in
5096030Ssobomax  PARAMS ((bfd *, const struct internal_reloc *, arelent *));
5196030Ssobomaxstatic void mips_adjust_reloc_out
5296030Ssobomax  PARAMS ((bfd *, const arelent *, struct internal_reloc *));
5396030Ssobomax#endif
5496030Ssobomax
5596030Ssobomaxstatic bfd_boolean in_reloc_p
5696030Ssobomax  PARAMS ((bfd *, reloc_howto_type *));
57327Sjkhstatic reloc_howto_type * coff_mips_reloc_type_lookup
58327Sjkh  PARAMS ((bfd *, bfd_reloc_code_real_type));
59379Sjkhstatic void mips_swap_reloc_in
6084745Ssobomax  PARAMS ((bfd *, PTR, PTR));
61379Sjkhstatic unsigned int mips_swap_reloc_out
62379Sjkh  PARAMS ((bfd *, PTR, PTR));
63379Sjkhstatic bfd_boolean coff_pe_mips_relocate_section
64379Sjkh  PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
65379Sjkh	   struct internal_reloc *, struct internal_syment *, asection **));
66379Sjkh
67379Sjkh#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
68379Sjkh/* The page size is a guess based on ELF.  */
69379Sjkh
70379Sjkh#define COFF_PAGE_SIZE 0x1000
71379Sjkh
72379Sjkh/* For some reason when using mips COFF the value stored in the .text
73379Sjkh   section for a reference to a common symbol is the value itself plus
74379Sjkh   any desired offset.  Ian Taylor, Cygnus Support.  */
75379Sjkh
76379Sjkh/* If we are producing relocatable output, we need to do some
77327Sjkh   adjustments to the object file that are not done by the
78327Sjkh   bfd_perform_relocation function.  This function is called by every
79327Sjkh   reloc type to make any required adjustments.  */
80327Sjkh
81327Sjkhstatic bfd_reloc_status_type
82327Sjkhcoff_mips_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
83327Sjkh		 error_message)
84327Sjkh     bfd *abfd;
85327Sjkh     arelent *reloc_entry;
86327Sjkh     asymbol *symbol;
87327Sjkh     PTR data;
88327Sjkh     asection *input_section ATTRIBUTE_UNUSED;
89327Sjkh     bfd *output_bfd;
90327Sjkh     char **error_message ATTRIBUTE_UNUSED;
91327Sjkh{
92327Sjkh  symvalue diff;
93327Sjkh
94327Sjkh  if (output_bfd == (bfd *) NULL)
95327Sjkh    return bfd_reloc_continue;
961547Sjkh
971547Sjkh  if (bfd_is_com_section (symbol->section))
981547Sjkh    {
99379Sjkh#ifndef COFF_WITH_PE
100379Sjkh      /* We are relocating a common symbol.  The current value in the
101379Sjkh	 object file is ORIG + OFFSET, where ORIG is the value of the
102379Sjkh	 common symbol as seen by the object file when it was compiled
103379Sjkh	 (this may be zero if the symbol was undefined) and OFFSET is
1041547Sjkh	 the offset into the common symbol (normally zero, but may be
105379Sjkh	 non-zero when referring to a field in a common structure).
106379Sjkh	 ORIG is the negative of reloc_entry->addend, which is set by
1071547Sjkh	 the CALC_ADDEND macro below.  We want to replace the value in
108379Sjkh	 the object file with NEW + OFFSET, where NEW is the value of
1098857Srgrimes	 the common symbol which we are going to put in the final
1107996Sjkh	 object file.  NEW is symbol->value.  */
1117996Sjkh      diff = symbol->value + reloc_entry->addend;
11284745Ssobomax#else
1137996Sjkh      /* In PE mode, we do not offset the common symbol.  */
1147996Sjkh      diff = reloc_entry->addend;
1157996Sjkh#endif
1167996Sjkh    }
1177996Sjkh  else
1187996Sjkh    {
1197996Sjkh      /* For some reason bfd_perform_relocation always effectively
1207996Sjkh	 ignores the addend for a COFF target when producing
1217996Sjkh	 relocatable output.  This seems to be always wrong for 386
1227996Sjkh	 COFF, so we handle the addend here instead.  */
1237996Sjkh      diff = reloc_entry->addend;
124383Sjkh    }
125383Sjkh
126383Sjkh#ifdef COFF_WITH_PE
127383Sjkh#if 0
128383Sjkh  /* dj - handle it like any other reloc? */
12984745Ssobomax  /* FIXME: How should this case be handled?  */
130383Sjkh  if (reloc_entry->howto->type == MIPS_R_RVA && diff != 0)
131383Sjkh    abort ();
132383Sjkh#endif
133383Sjkh#endif
134383Sjkh
135383Sjkh#define DOIT(x) \
136383Sjkh  x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + (diff >> howto->rightshift)) & howto->dst_mask))
137383Sjkh
138383Sjkh    if (diff != 0)
139383Sjkh      {
140383Sjkh	reloc_howto_type *howto = reloc_entry->howto;
141383Sjkh	unsigned char *addr = (unsigned char *) data + reloc_entry->address;
142383Sjkh
143383Sjkh	switch (howto->size)
144383Sjkh	  {
145383Sjkh	  case 0:
146383Sjkh	    {
147383Sjkh	      char x = bfd_get_8 (abfd, addr);
148383Sjkh	      DOIT (x);
149383Sjkh	      bfd_put_8 (abfd, x, addr);
150383Sjkh	    }
151383Sjkh	    break;
152383Sjkh
153383Sjkh	  case 1:
154383Sjkh	    {
1558857Srgrimes	      short x = bfd_get_16 (abfd, addr);
156327Sjkh	      DOIT (x);
157327Sjkh	      bfd_put_16 (abfd, (bfd_vma) x, addr);
158327Sjkh	    }
159327Sjkh	    break;
160327Sjkh
161327Sjkh	  case 2:
162327Sjkh	    {
163327Sjkh	      long x = bfd_get_32 (abfd, addr);
164327Sjkh	      DOIT (x);
165327Sjkh	      bfd_put_32 (abfd, (bfd_vma) x, addr);
166327Sjkh	    }
167327Sjkh	    break;
168327Sjkh
169327Sjkh	  default:
170327Sjkh	    abort ();
171327Sjkh	  }
172327Sjkh      }
173327Sjkh
174327Sjkh  /* Now let bfd_perform_relocation finish everything up.  */
175327Sjkh  return bfd_reloc_continue;
176327Sjkh}
177327Sjkh
178327Sjkh#ifdef COFF_WITH_PE
179327Sjkh/* Return TRUE if this relocation should
180327Sjkh   appear in the output .reloc section.  */
181327Sjkh
182327Sjkhstatic bfd_boolean
183327Sjkhin_reloc_p (abfd, howto)
184327Sjkh     bfd * abfd ATTRIBUTE_UNUSED;
185327Sjkh     reloc_howto_type *howto;
186327Sjkh{
187327Sjkh  return ! howto->pc_relative && howto->type != MIPS_R_RVA;
18884745Ssobomax}
189327Sjkh#endif
190327Sjkh
19184745Ssobomax#ifndef PCRELOFFSET
19284745Ssobomax#define PCRELOFFSET FALSE
193327Sjkh#endif
194327Sjkh
195327Sjkhstatic reloc_howto_type howto_table[] =
196327Sjkh{
197327Sjkh  /* Reloc type 0 is ignored.  The reloc reading code ensures that
198327Sjkh     this is a reference to the .abs section, which will cause
199327Sjkh     bfd_perform_relocation to do nothing.  */
200327Sjkh  HOWTO (MIPS_R_ABSOLUTE,	/* type */
201327Sjkh	 0,			/* rightshift */
202327Sjkh	 0,			/* size (0 = byte, 1 = short, 2 = long) */
203327Sjkh	 8,			/* bitsize */
204327Sjkh	 FALSE,			/* pc_relative */
205327Sjkh	 0,			/* bitpos */
206327Sjkh	 complain_overflow_dont, /* complain_on_overflow */
207327Sjkh	 0,			/* special_function */
20884745Ssobomax	 "IGNORE",		/* name */
209327Sjkh	 FALSE,			/* partial_inplace */
210327Sjkh	 0,			/* src_mask */
2112331Sjkh	 0,			/* dst_mask */
2122331Sjkh	 FALSE),		/* pcrel_offset */
213379Sjkh
214379Sjkh  /* A 16 bit reference to a symbol, normally from a data section.  */
215327Sjkh  HOWTO (MIPS_R_REFHALF,	/* type */
216327Sjkh	 0,			/* rightshift */
217479Sjkh	 1,			/* size (0 = byte, 1 = short, 2 = long) */
218479Sjkh	 16,			/* bitsize */
219327Sjkh	 FALSE,			/* pc_relative */
220327Sjkh	 0,			/* bitpos */
221327Sjkh	 complain_overflow_bitfield, /* complain_on_overflow */
222327Sjkh	 coff_mips_reloc,	/* special_function */
223327Sjkh	 "REFHALF",		/* name */
224327Sjkh	 TRUE,			/* partial_inplace */
22596030Ssobomax	 0xffff,		/* src_mask */
22696030Ssobomax	 0xffff,		/* dst_mask */
22796030Ssobomax	 FALSE),		/* pcrel_offset */
22896030Ssobomax
22996076Ssobomax  /* A 32 bit reference to a symbol, normally from a data section.  */
23096076Ssobomax  HOWTO (MIPS_R_REFWORD,	/* type */
23196076Ssobomax	 0,			/* rightshift */
23296030Ssobomax	 2,			/* size (0 = byte, 1 = short, 2 = long) */
233327Sjkh	 32,			/* bitsize */
23496030Ssobomax	 FALSE,			/* pc_relative */
235327Sjkh	 0,			/* bitpos */
2364996Sjkh	 complain_overflow_bitfield, /* complain_on_overflow */
2374996Sjkh	 coff_mips_reloc,	/* special_function */
238327Sjkh	 "REFWORD",		/* name */
239327Sjkh	 TRUE,			/* partial_inplace */
2404996Sjkh	 0xffffffff,		/* src_mask */
2414996Sjkh	 0xffffffff,		/* dst_mask */
2424996Sjkh	 FALSE),		/* pcrel_offset */
2434996Sjkh
2444996Sjkh  /* A 26 bit absolute jump address.  */
2454996Sjkh  HOWTO (MIPS_R_JMPADDR,	/* type */
2464996Sjkh	 2,			/* rightshift */
2474996Sjkh	 2,			/* size (0 = byte, 1 = short, 2 = long) */
2487996Sjkh	 26,			/* bitsize */
2497996Sjkh	 FALSE,			/* pc_relative */
250327Sjkh	 0,			/* bitpos */
251327Sjkh	 complain_overflow_dont, /* complain_on_overflow */
252327Sjkh	 			/* This needs complex overflow
253327Sjkh				   detection, because the upper four
254327Sjkh				   bits must match the PC.  */
255327Sjkh	 coff_mips_reloc,	/* special_function */
256327Sjkh	 "JMPADDR",		/* name */
257327Sjkh	 TRUE,			/* partial_inplace */
258327Sjkh	 0x3ffffff,		/* src_mask */
25984750Ssobomax	 0x3ffffff,		/* dst_mask */
260327Sjkh	 FALSE),		/* pcrel_offset */
26184750Ssobomax
26284750Ssobomax  /* The high 16 bits of a symbol value.  Handled by the function
263327Sjkh     mips_refhi_reloc.  */
26417338Sjkh  HOWTO (MIPS_R_REFHI,		/* type */
265327Sjkh	 16,			/* rightshift */
26617338Sjkh	 2,			/* size (0 = byte, 1 = short, 2 = long) */
26717338Sjkh	 16,			/* bitsize */
26817338Sjkh	 FALSE,			/* pc_relative */
269327Sjkh	 0,			/* bitpos */
270327Sjkh	 complain_overflow_bitfield, /* complain_on_overflow */
27184750Ssobomax	 coff_mips_reloc,	/* special_function */
27284750Ssobomax	 "REFHI",		/* name */
27384750Ssobomax	 TRUE,			/* partial_inplace */
27484750Ssobomax	 0xffff,		/* src_mask */
27584750Ssobomax	 0xffff,		/* dst_mask */
27684750Ssobomax	 FALSE),		/* pcrel_offset */
27784750Ssobomax
27884750Ssobomax  /* The low 16 bits of a symbol value.  */
27984750Ssobomax  HOWTO (MIPS_R_REFLO,		/* type */
28084750Ssobomax	 0,			/* rightshift */
28184750Ssobomax	 2,			/* size (0 = byte, 1 = short, 2 = long) */
28284750Ssobomax	 16,			/* bitsize */
28384750Ssobomax	 FALSE,			/* pc_relative */
28484750Ssobomax	 0,			/* bitpos */
28584750Ssobomax	 complain_overflow_dont, /* complain_on_overflow */
28684750Ssobomax	 coff_mips_reloc,	/* special_function */
28784750Ssobomax	 "REFLO",		/* name */
28884750Ssobomax	 TRUE,			/* partial_inplace */
28984750Ssobomax	 0xffff,		/* src_mask */
29084750Ssobomax	 0xffff,		/* dst_mask */
29184750Ssobomax	 FALSE),		/* pcrel_offset */
29284750Ssobomax
29384750Ssobomax  /* A reference to an offset from the gp register.  Handled by the
29484750Ssobomax     function mips_gprel_reloc.  */
29539068Sjkh  HOWTO (MIPS_R_GPREL,		/* type */
29684750Ssobomax	 0,			/* rightshift */
29739068Sjkh	 2,			/* size (0 = byte, 1 = short, 2 = long) */
298327Sjkh	 16,			/* bitsize */
29984750Ssobomax	 FALSE,			/* pc_relative */
300327Sjkh	 0,			/* bitpos */
301327Sjkh	 complain_overflow_signed, /* complain_on_overflow */
302327Sjkh	 coff_mips_reloc,	/* special_function */
303327Sjkh	 "GPREL",		/* name */
304327Sjkh	 TRUE,			/* partial_inplace */
305327Sjkh	 0xffff,		/* src_mask */
306327Sjkh	 0xffff,		/* dst_mask */
307327Sjkh	 FALSE),		/* pcrel_offset */
308327Sjkh
309327Sjkh  /* A reference to a literal using an offset from the gp register.
310327Sjkh     Handled by the function mips_gprel_reloc.  */
311327Sjkh  HOWTO (MIPS_R_LITERAL,	/* type */
312327Sjkh	 0,			/* rightshift */
313327Sjkh	 2,			/* size (0 = byte, 1 = short, 2 = long) */
314327Sjkh	 16,			/* bitsize */
315327Sjkh	 FALSE,			/* pc_relative */
316327Sjkh	 0,			/* bitpos */
317327Sjkh	 complain_overflow_signed, /* complain_on_overflow */
318327Sjkh	 coff_mips_reloc,	/* special_function */
319327Sjkh	 "LITERAL",		/* name */
3202331Sjkh	 TRUE,			/* partial_inplace */
3212331Sjkh	 0xffff,		/* src_mask */
3222331Sjkh	 0xffff,		/* dst_mask */
3232331Sjkh	 FALSE),		/* pcrel_offset */
324327Sjkh
325327Sjkh  EMPTY_HOWTO (8),
326327Sjkh  EMPTY_HOWTO (9),
327327Sjkh  EMPTY_HOWTO (10),
328479Sjkh  EMPTY_HOWTO (11),
329479Sjkh  EMPTY_HOWTO (12),
330479Sjkh  EMPTY_HOWTO (13),
331479Sjkh  EMPTY_HOWTO (14),
332327Sjkh  EMPTY_HOWTO (15),
33312219Sjkh  EMPTY_HOWTO (16),
334327Sjkh  EMPTY_HOWTO (17),
335327Sjkh  EMPTY_HOWTO (18),
336327Sjkh  EMPTY_HOWTO (19),
33712219Sjkh  EMPTY_HOWTO (20),
338327Sjkh  EMPTY_HOWTO (21),
339327Sjkh  EMPTY_HOWTO (22),
340327Sjkh  EMPTY_HOWTO (23),
34112219Sjkh  EMPTY_HOWTO (24),
342327Sjkh  EMPTY_HOWTO (25),
343327Sjkh  EMPTY_HOWTO (26),
344327Sjkh  EMPTY_HOWTO (27),
345327Sjkh  EMPTY_HOWTO (28),
346327Sjkh  EMPTY_HOWTO (29),
347327Sjkh  EMPTY_HOWTO (30),
348327Sjkh  EMPTY_HOWTO (31),
3494996Sjkh  EMPTY_HOWTO (32),
350327Sjkh  EMPTY_HOWTO (33),
351327Sjkh  HOWTO (MIPS_R_RVA,            /* type */
352327Sjkh	 0,	                /* rightshift */
353327Sjkh	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
354327Sjkh	 32,	                /* bitsize */
355327Sjkh	 FALSE,	                /* pc_relative */
356327Sjkh	 0,	                /* bitpos */
3574996Sjkh	 complain_overflow_bitfield, /* complain_on_overflow */
3584996Sjkh	 coff_mips_reloc,       /* special_function */
3594996Sjkh	 "rva32",	        /* name */
3604996Sjkh	 TRUE,	                /* partial_inplace */
3614996Sjkh	 0xffffffff,            /* src_mask */
3624996Sjkh	 0xffffffff,            /* dst_mask */
3634996Sjkh	 FALSE),                /* pcrel_offset */
3644996Sjkh  EMPTY_HOWTO (35),
3654996Sjkh  EMPTY_HOWTO (36),
3664996Sjkh  HOWTO (MIPS_R_PAIR,           /* type */
3674996Sjkh	 0,	                /* rightshift */
3684996Sjkh	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
3694996Sjkh	 32,	                /* bitsize */
3704996Sjkh	 FALSE,	                /* pc_relative */
3714996Sjkh	 0,	                /* bitpos */
3724996Sjkh	 complain_overflow_bitfield, /* complain_on_overflow */
3737996Sjkh	 coff_mips_reloc,       /* special_function */
3747996Sjkh	 "PAIR",	        /* name */
3757996Sjkh	 TRUE,	                /* partial_inplace */
3767996Sjkh	 0xffffffff,            /* src_mask */
37796065Ssobomax	 0xffffffff,            /* dst_mask */
37896065Ssobomax	 FALSE),                /* pcrel_offset */
37996065Ssobomax};
38096065Ssobomax
38196076Ssobomax/* Turn a howto into a reloc  nunmber */
38296076Ssobomax
38396076Ssobomax#define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
38496076Ssobomax#define BADMAG(x) MIPSBADMAG(x)
385327Sjkh#define MIPS 1			/* Customize coffcode.h */
38630221Scharnier
38767429Sjkh#define RTYPE2HOWTO(cache_ptr, dst) \
388327Sjkh	    (cache_ptr)->howto = howto_table + (dst)->r_type;
389327Sjkh
390327Sjkh/* Compute the addend of a reloc.  If the reloc is to a common symbol,
391327Sjkh   the object file contains the value of the common symbol.  By the
392327Sjkh   time this is called, the linker may be using a different symbol
393327Sjkh   from a different object file with a different value.  Therefore, we
3947996Sjkh   hack wildly to locate the original symbol from this file so that we
3957996Sjkh   can make the correct adjustment.  This macro sets coffsym to the
3967996Sjkh   symbol from the original file, and uses it to set the addend value
3977996Sjkh   correctly.  If this is not a common symbol, the usual addend
3987996Sjkh   calculation is done, except that an additional tweak is needed for
3997996Sjkh   PC relative relocs.
4003198Sjkh   FIXME: This macro refers to symbols and asect; these are from the
4014996Sjkh   calling function, not the macro arguments.  */
402327Sjkh
40317338Sjkh#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)		\
40484745Ssobomax  {								\
4053198Sjkh    coff_symbol_type *coffsym = (coff_symbol_type *) NULL;	\
40627056Sjkh    if (ptr && bfd_asymbol_bfd (ptr) != abfd)			\
40727056Sjkh      coffsym = (obj_symbols (abfd)				\
408327Sjkh	         + (cache_ptr->sym_ptr_ptr - symbols));		\
40927056Sjkh    else if (ptr)						\
41017338Sjkh      coffsym = coff_symbol_from (abfd, ptr);			\
41117338Sjkh    if (coffsym != (coff_symbol_type *) NULL			\
41227056Sjkh	&& coffsym->native->u.syment.n_scnum == 0)		\
41327056Sjkh      cache_ptr->addend = - coffsym->native->u.syment.n_value;	\
41427056Sjkh    else if (ptr && bfd_asymbol_bfd (ptr) == abfd		\
41527056Sjkh	     && ptr->section != (asection *) NULL)		\
41617338Sjkh      cache_ptr->addend = - (ptr->section->vma + ptr->value);	\
41717338Sjkh    else							\
41817338Sjkh      cache_ptr->addend = 0;					\
41917338Sjkh    if (ptr && howto_table[reloc.r_type].pc_relative)		\
42017338Sjkh      cache_ptr->addend += asect->vma;				\
421327Sjkh  }
422327Sjkh
4239743Sache/* Convert an rtype to howto for the COFF backend linker.  */
42417338Sjkh
425479Sjkhstatic reloc_howto_type *
42617338Sjkhcoff_mips_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
42717338Sjkh     bfd *abfd ATTRIBUTE_UNUSED;
428479Sjkh     asection *sec;
42981046Ssobomax     struct internal_reloc *rel;
43017338Sjkh     struct coff_link_hash_entry *h;
43181046Ssobomax     struct internal_syment *sym;
4323198Sjkh     bfd_vma *addendp;
4333198Sjkh{
43417338Sjkh
435327Sjkh  reloc_howto_type *howto;
43617338Sjkh
43727092Sjkh  howto = howto_table + rel->r_type;
43817338Sjkh
43966021Ssobomax#ifdef COFF_WITH_PE
44081046Ssobomax  *addendp = 0;
44130221Scharnier#endif
44212219Sjkh
44312219Sjkh  if (howto->pc_relative)
44417338Sjkh    *addendp += sec->vma;
44584750Ssobomax
44617338Sjkh  if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
44784750Ssobomax    {
44884750Ssobomax      /* This is a common symbol.  The section contents include the
44984750Ssobomax	 size (sym->n_value) as an addend.  The relocate_section
45084750Ssobomax	 function will be adding in the final value of the symbol.  We
45184750Ssobomax	 need to subtract out the current size in order to get the
45284750Ssobomax	 correct result.  */
45384750Ssobomax
45484750Ssobomax      BFD_ASSERT (h != NULL);
45584750Ssobomax
45684750Ssobomax#ifndef COFF_WITH_PE
45784750Ssobomax      /* I think we *do* want to bypass this.  If we don't, I have
45884750Ssobomax	 seen some data parameters get the wrong relocation address.
45984750Ssobomax	 If I link two versions with and without this section bypassed
46084750Ssobomax	 and then do a binary comparison, the addresses which are
46184750Ssobomax	 different can be looked up in the map.  The case in which
46217338Sjkh	 this section has been bypassed has addresses which correspond
46317338Sjkh	 to values I can find in the map.  */
46481046Ssobomax      *addendp -= sym->n_value;
46517338Sjkh#endif
46617338Sjkh    }
46717338Sjkh
46817338Sjkh#ifndef COFF_WITH_PE
46917338Sjkh  /* If the output symbol is common (in which case this must be a
47017338Sjkh     relocatable link), we need to add in the final size of the
47117338Sjkh     common symbol.  */
47217338Sjkh  if (h != NULL && h->root.type == bfd_link_hash_common)
47312219Sjkh    *addendp += h->root.u.c.size;
47417338Sjkh#endif
47527092Sjkh
47629032Sjkh#ifdef COFF_WITH_PE
47727092Sjkh  if (howto->pc_relative)
47827137Sjkh    {
47927137Sjkh      *addendp -= 4;
48027092Sjkh
48127137Sjkh      /* If the symbol is defined, then the generic code is going to
48227092Sjkh         add back the symbol value in order to cancel out an
48327092Sjkh         adjustment it made to the addend.  However, we set the addend
48430221Scharnier         to 0 at the start of this function.  We need to adjust here,
48530221Scharnier         to avoid the adjustment the generic code will make.  FIXME:
48627092Sjkh         This is getting a bit hackish.  */
48727056Sjkh      if (sym != NULL && sym->n_scnum != 0)
48827056Sjkh	*addendp -= sym->n_value;
48927056Sjkh    }
4903198Sjkh
49117338Sjkh  if (rel->r_type == MIPS_R_RVA)
49217338Sjkh    {
49317338Sjkh      *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
49417338Sjkh    }
49538723Sjkh#endif
49681046Ssobomax
49730221Scharnier  return howto;
49817338Sjkh}
49917338Sjkh
50017338Sjkh#define coff_rtype_to_howto coff_mips_rtype_to_howto
50117338Sjkh
50217338Sjkh#define coff_bfd_reloc_type_lookup coff_mips_reloc_type_lookup
50330221Scharnier
50417338Sjkh/* Get the howto structure for a generic reloc type.  */
50517338Sjkh
50617338Sjkhstatic reloc_howto_type *
50717338Sjkhcoff_mips_reloc_type_lookup (abfd, code)
50817338Sjkh     bfd *abfd ATTRIBUTE_UNUSED;
50971373Ssobomax     bfd_reloc_code_real_type code;
51071373Ssobomax{
51171373Ssobomax  int mips_type;
512327Sjkh
513327Sjkh  switch (code)
5143198Sjkh    {
515327Sjkh    case BFD_RELOC_16:
516327Sjkh      mips_type = MIPS_R_REFHALF;
5174996Sjkh      break;
5184996Sjkh    case BFD_RELOC_32:
5194996Sjkh    case BFD_RELOC_CTOR:
5204996Sjkh      mips_type = MIPS_R_REFWORD;
5214996Sjkh      break;
5227989Sjkh    case BFD_RELOC_MIPS_JMP:
5234996Sjkh      mips_type = MIPS_R_JMPADDR;
5244996Sjkh      break;
525327Sjkh    case BFD_RELOC_HI16_S:
526327Sjkh      mips_type = MIPS_R_REFHI;
52784745Ssobomax      break;
528327Sjkh    case BFD_RELOC_LO16:
529327Sjkh      mips_type = MIPS_R_REFLO;
5308857Srgrimes      break;
53184745Ssobomax    case BFD_RELOC_GPREL16:
5327989Sjkh      mips_type = MIPS_R_GPREL;
5337989Sjkh      break;
53481046Ssobomax    case BFD_RELOC_MIPS_LITERAL:
53530221Scharnier      mips_type = MIPS_R_LITERAL;
53627092Sjkh      break;
53727092Sjkh/* FIXME?
53827092Sjkh    case BFD_RELOC_16_PCREL_S2:
5394996Sjkh      mips_type = MIPS_R_PCREL16;
5404996Sjkh      break;
54127092Sjkh    case BFD_RELOC_PCREL_HI16_S:
54266021Ssobomax      mips_type = MIPS_R_RELHI;
5437989Sjkh      break;
5444996Sjkh    case BFD_RELOC_PCREL_LO16:
54527092Sjkh      mips_type = MIPS_R_RELLO;
54627092Sjkh      break;
5474996Sjkh    case BFD_RELOC_GPREL32:
5484996Sjkh      mips_type = MIPS_R_SWITCH;
5494996Sjkh      break;
5504996Sjkh*/
5514996Sjkh    case BFD_RELOC_RVA:
5524996Sjkh      mips_type = MIPS_R_RVA;
553327Sjkh      break;
55467429Sjkh    default:
555327Sjkh      return (reloc_howto_type *) NULL;
5564996Sjkh    }
557327Sjkh
55849637Sbillf  return &howto_table[mips_type];
5597989Sjkh}
56081046Ssobomax
5617989Sjkhstatic void
5627989Sjkhmips_swap_reloc_in (abfd, src, dst)
56349637Sbillf     bfd *abfd;
5644996Sjkh     PTR src;
565327Sjkh     PTR dst;
56684745Ssobomax{
567327Sjkh  static struct internal_reloc pair_prev;
568327Sjkh  RELOC *reloc_src = (RELOC *) src;
569327Sjkh  struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
570327Sjkh
571  reloc_dst->r_vaddr = H_GET_32 (abfd, reloc_src->r_vaddr);
572  reloc_dst->r_symndx = H_GET_S32 (abfd, reloc_src->r_symndx);
573  reloc_dst->r_type = H_GET_16 (abfd, reloc_src->r_type);
574  reloc_dst->r_size = 0;
575  reloc_dst->r_extern = 0;
576  reloc_dst->r_offset = 0;
577
578  switch (reloc_dst->r_type)
579  {
580  case MIPS_R_REFHI:
581    pair_prev = *reloc_dst;
582    break;
583  case MIPS_R_PAIR:
584    reloc_dst->r_offset = reloc_dst->r_symndx;
585    if (reloc_dst->r_offset & 0x8000)
586      reloc_dst->r_offset -= 0x10000;
587    /*printf ("dj: pair offset is %08x\n", reloc_dst->r_offset);*/
588    reloc_dst->r_symndx = pair_prev.r_symndx;
589    break;
590  }
591}
592
593static unsigned int
594mips_swap_reloc_out (abfd, src, dst)
595     bfd       *abfd;
596     PTR	src;
597     PTR	dst;
598{
599  static int prev_offset = 1;
600  static bfd_vma prev_addr = 0;
601  struct internal_reloc *reloc_src = (struct internal_reloc *)src;
602  struct external_reloc *reloc_dst = (struct external_reloc *)dst;
603
604  switch (reloc_src->r_type)
605    {
606    case MIPS_R_REFHI:
607      prev_addr = reloc_src->r_vaddr;
608      prev_offset = reloc_src->r_offset;
609      break;
610    case MIPS_R_REFLO:
611      if (reloc_src->r_vaddr == prev_addr)
612	{
613	  /* FIXME: only slightly hackish.  If we see a REFLO pointing to
614	     the same address as a REFHI, we assume this is the matching
615	     PAIR reloc and output it accordingly.  The symndx is really
616	     the low 16 bits of the addend */
617	  H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr);
618	  H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx);
619	  H_PUT_16 (abfd, MIPS_R_PAIR, reloc_dst->r_type);
620	  return RELSZ;
621	}
622      break;
623    }
624
625  H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr);
626  H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx);
627
628  H_PUT_16 (abfd, reloc_src->r_type, reloc_dst->r_type);
629  return RELSZ;
630}
631
632#define coff_swap_reloc_in mips_swap_reloc_in
633#define coff_swap_reloc_out mips_swap_reloc_out
634#define NO_COFF_RELOCS
635
636static bfd_boolean
637coff_pe_mips_relocate_section (output_bfd, info, input_bfd,
638			       input_section, contents, relocs, syms,
639			       sections)
640     bfd *output_bfd;
641     struct bfd_link_info *info;
642     bfd *input_bfd;
643     asection *input_section;
644     bfd_byte *contents;
645     struct internal_reloc *relocs;
646     struct internal_syment *syms;
647     asection **sections;
648{
649  bfd_vma gp;
650  bfd_boolean gp_undefined;
651  size_t adjust;
652  struct internal_reloc *rel;
653  struct internal_reloc *rel_end;
654  unsigned int i;
655  bfd_boolean got_lo;
656
657  if (info->relocatable)
658  {
659    (*_bfd_error_handler) (_("\
660%s: `ld -r' not supported with PE MIPS objects\n"),
661			   bfd_archive_filename (input_bfd));
662    bfd_set_error (bfd_error_bad_value);
663    return FALSE;
664  }
665
666  BFD_ASSERT (input_bfd->xvec->byteorder
667	      == output_bfd->xvec->byteorder);
668
669#if 0
670  printf ("dj: relocate %s(%s) %08x\n",
671	 input_bfd->filename, input_section->name,
672	 input_section->output_section->vma + input_section->output_offset);
673#endif
674
675  gp = _bfd_get_gp_value (output_bfd);
676  if (gp == 0)
677    gp_undefined = TRUE;
678  else
679    gp_undefined = FALSE;
680
681  got_lo = FALSE;
682
683  adjust = 0;
684
685  rel = relocs;
686  rel_end = rel + input_section->reloc_count;
687  for (i = 0; rel < rel_end; rel++, i++)
688    {
689      long symndx;
690      struct coff_link_hash_entry *h;
691      struct internal_syment *sym;
692      bfd_vma addend = 0;
693      bfd_vma val, tmp, targ, src, low;
694      reloc_howto_type *howto;
695      unsigned char *mem = contents + rel->r_vaddr;
696
697      symndx = rel->r_symndx;
698
699      if (symndx == -1)
700	{
701	  h = NULL;
702	  sym = NULL;
703	}
704      else
705	{
706	  h = obj_coff_sym_hashes (input_bfd)[symndx];
707	  sym = syms + symndx;
708	}
709
710      /* COFF treats common symbols in one of two ways.  Either the
711         size of the symbol is included in the section contents, or it
712         is not.  We assume that the size is not included, and force
713         the rtype_to_howto function to adjust the addend as needed.  */
714
715      if (sym != NULL && sym->n_scnum != 0)
716	addend = - sym->n_value;
717      else
718	addend = 0;
719
720      howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
721				       sym, &addend);
722      if (howto == NULL)
723	return FALSE;
724
725      /* If we are doing a relocatable link, then we can just ignore
726         a PC relative reloc that is pcrel_offset.  It will already
727         have the correct value.  If this is not a relocatable link,
728         then we should ignore the symbol value.  */
729      if (howto->pc_relative && howto->pcrel_offset)
730	{
731	  if (info->relocatable)
732	    continue;
733	  if (sym != NULL && sym->n_scnum != 0)
734	    addend += sym->n_value;
735	}
736
737      val = 0;
738
739      if (h == NULL)
740	{
741	  asection *sec;
742
743	  if (symndx == -1)
744	    {
745	      sec = bfd_abs_section_ptr;
746	      val = 0;
747	    }
748	  else
749	    {
750	      sec = sections[symndx];
751              val = (sec->output_section->vma
752		     + sec->output_offset
753		     + sym->n_value);
754	      if (! obj_pe (input_bfd))
755		val -= sec->vma;
756	    }
757	}
758      else
759	{
760	  if (h->root.type == bfd_link_hash_defined
761	      || h->root.type == bfd_link_hash_defweak)
762	    {
763	      asection *sec;
764
765	      sec = h->root.u.def.section;
766	      val = (h->root.u.def.value
767		     + sec->output_section->vma
768		     + sec->output_offset);
769	      }
770
771	  else if (! info->relocatable)
772	    {
773	      if (! ((*info->callbacks->undefined_symbol)
774		     (info, h->root.root.string, input_bfd, input_section,
775		      rel->r_vaddr - input_section->vma, TRUE)))
776		return FALSE;
777	    }
778	}
779
780      src = rel->r_vaddr + input_section->output_section->vma
781	+ input_section->output_offset;
782#if 0
783      printf ("dj: reloc %02x %-8s a=%08x/%08x(%08x) v=%08x+%08x %s\n",
784	     rel->r_type, howto_table[rel->r_type].name,
785	     src, rel->r_vaddr, *(unsigned long *)mem, val, rel->r_offset,
786	     h?h->root.root.string:"(none)");
787#endif
788
789      /* OK, at this point the following variables are set up:
790	   src = VMA of the memory we're fixing up
791	   mem = pointer to memory we're fixing up
792	   val = VMA of what we need to refer to
793      */
794
795#define UI(x) (*_bfd_error_handler) (_("%s: unimplemented %s\n"), \
796				     bfd_archive_filename (input_bfd), x); \
797	      bfd_set_error (bfd_error_bad_value);
798
799      switch (rel->r_type)
800	{
801	case MIPS_R_ABSOLUTE:
802	  /* ignore these */
803	  break;
804
805	case MIPS_R_REFHALF:
806	  UI("refhalf");
807	  break;
808
809	case MIPS_R_REFWORD:
810	  tmp = bfd_get_32(input_bfd, mem);
811	  /* printf ("refword: src=%08x targ=%08x+%08x\n", src, tmp, val); */
812	  tmp += val;
813	  bfd_put_32(input_bfd, tmp, mem);
814	  break;
815
816	case MIPS_R_JMPADDR:
817	  tmp = bfd_get_32(input_bfd, mem);
818	  targ = val + (tmp&0x03ffffff)*4;
819	  if ((src & 0xf0000000) != (targ & 0xf0000000))
820	    {
821	      (*_bfd_error_handler) (_("%s: jump too far away\n"),
822				     bfd_archive_filename (input_bfd));
823	      bfd_set_error (bfd_error_bad_value);
824	      return FALSE;
825	    }
826	  tmp &= 0xfc000000;
827	  tmp |= (targ/4) & 0x3ffffff;
828	  bfd_put_32(input_bfd, tmp, mem);
829	  break;
830
831	case MIPS_R_REFHI:
832	  tmp = bfd_get_32(input_bfd, mem);
833	  switch (rel[1].r_type)
834	    {
835	    case MIPS_R_PAIR:
836	      /* MS PE object */
837	      targ = val + rel[1].r_offset + ((tmp & 0xffff) << 16);
838	      break;
839	    case MIPS_R_REFLO:
840	      /* GNU COFF object */
841	      low = bfd_get_32(input_bfd, contents + rel[1].r_vaddr);
842	      low &= 0xffff;
843	      if (low & 0x8000)
844		low -= 0x10000;
845	      targ = val + low + ((tmp & 0xffff) << 16);
846	      break;
847	    default:
848	      (*_bfd_error_handler) (_("%s: bad pair/reflo after refhi\n"),
849				     bfd_archive_filename (input_bfd));
850	      bfd_set_error (bfd_error_bad_value);
851	      return FALSE;
852	    }
853	  tmp &= 0xffff0000;
854	  tmp |= (targ >> 16) & 0xffff;
855	  bfd_put_32(input_bfd, tmp, mem);
856	  break;
857
858	case MIPS_R_REFLO:
859	  tmp = bfd_get_32(input_bfd, mem);
860	  targ = val + (tmp & 0xffff);
861	  /* printf ("refword: src=%08x targ=%08x\n", src, targ); */
862	  tmp &= 0xffff0000;
863	  tmp |= targ & 0xffff;
864	  bfd_put_32(input_bfd, tmp, mem);
865	  break;
866
867	case MIPS_R_GPREL:
868	case MIPS_R_LITERAL:
869	  UI("gprel");
870	  break;
871
872	case MIPS_R_SECTION:
873	  UI("section");
874	  break;
875
876	case MIPS_R_SECREL:
877	  UI("secrel");
878	  break;
879
880	case MIPS_R_SECRELLO:
881	  UI("secrello");
882	  break;
883
884	case MIPS_R_SECRELHI:
885	  UI("secrelhi");
886	  break;
887
888	case MIPS_R_RVA:
889	  tmp = bfd_get_32 (input_bfd, mem);
890	  /* printf ("rva: src=%08x targ=%08x+%08x\n", src, tmp, val); */
891	  tmp += val
892	    - pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase;
893	  bfd_put_32 (input_bfd, tmp, mem);
894	  break;
895
896	case MIPS_R_PAIR:
897	  /* ignore these */
898	  break;
899	}
900    }
901
902  return TRUE;
903}
904
905#define coff_relocate_section coff_pe_mips_relocate_section
906
907#ifdef TARGET_UNDERSCORE
908
909/* If mips gcc uses underscores for symbol names, then it does not use
910   a leading dot for local labels, so if TARGET_UNDERSCORE is defined
911   we treat all symbols starting with L as local.  */
912
913static bfd_boolean coff_mips_is_local_label_name
914   PARAMS ((bfd *, const char *));
915
916static bfd_boolean
917coff_mips_is_local_label_name (abfd, name)
918     bfd *abfd;
919     const char *name;
920{
921  if (name[0] == 'L')
922    return TRUE;
923
924  return _bfd_coff_is_local_label_name (abfd, name);
925}
926
927#define coff_bfd_is_local_label_name coff_mips_is_local_label_name
928
929#endif /* TARGET_UNDERSCORE */
930
931#define COFF_NO_HACK_SCNHDR_SIZE
932
933#include "coffcode.h"
934
935const bfd_target
936#ifdef TARGET_SYM
937  TARGET_SYM =
938#else
939  mipslpe_vec =
940#endif
941{
942#ifdef TARGET_NAME
943  TARGET_NAME,
944#else
945  "pe-mips",			/* name */
946#endif
947  bfd_target_coff_flavour,
948  BFD_ENDIAN_LITTLE,		/* data byte order is little */
949  BFD_ENDIAN_LITTLE,		/* header byte order is little */
950
951  (HAS_RELOC | EXEC_P |		/* object flags */
952   HAS_LINENO | HAS_DEBUG |
953   HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
954
955#ifndef COFF_WITH_PE
956  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
957   | SEC_CODE | SEC_DATA),
958#else
959  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
960   | SEC_CODE | SEC_DATA
961   | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
962#endif
963
964#ifdef TARGET_UNDERSCORE
965  TARGET_UNDERSCORE,		/* leading underscore */
966#else
967  0,				/* leading underscore */
968#endif
969  '/',				/* ar_pad_char */
970  15,				/* ar_max_namelen */
971
972  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
973     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
974     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
975  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
976     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
977     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
978
979/* Note that we allow an object file to be treated as a core file as well.  */
980    {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
981       bfd_generic_archive_p, coff_object_p},
982    {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
983       bfd_false},
984    {bfd_false, coff_write_object_contents, /* bfd_write_contents */
985       _bfd_write_archive_contents, bfd_false},
986
987     BFD_JUMP_TABLE_GENERIC (coff),
988     BFD_JUMP_TABLE_COPY (coff),
989     BFD_JUMP_TABLE_CORE (_bfd_nocore),
990     BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
991     BFD_JUMP_TABLE_SYMBOLS (coff),
992     BFD_JUMP_TABLE_RELOCS (coff),
993     BFD_JUMP_TABLE_WRITE (coff),
994     BFD_JUMP_TABLE_LINK (coff),
995     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
996
997  NULL,
998
999  COFF_SWAP_TABLE
1000};
1001