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