xref: /openbsd-src/gnu/usr.bin/binutils-2.17/bfd/coff-i386.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* BFD back-end for Intel 386 COFF files.
2*3d8817e4Smiod    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3*3d8817e4Smiod    2000, 2001, 2002, 2003, 2004
4*3d8817e4Smiod    Free Software Foundation, Inc.
5*3d8817e4Smiod    Written by Cygnus Support.
6*3d8817e4Smiod 
7*3d8817e4Smiod    This file is part of BFD, the Binary File Descriptor library.
8*3d8817e4Smiod 
9*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
10*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
11*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
12*3d8817e4Smiod    (at your option) any later version.
13*3d8817e4Smiod 
14*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
15*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*3d8817e4Smiod    GNU General Public License for more details.
18*3d8817e4Smiod 
19*3d8817e4Smiod    You should have received a copy of the GNU General Public License
20*3d8817e4Smiod    along with this program; if not, write to the Free Software
21*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22*3d8817e4Smiod 
23*3d8817e4Smiod #include "bfd.h"
24*3d8817e4Smiod #include "sysdep.h"
25*3d8817e4Smiod #include "libbfd.h"
26*3d8817e4Smiod 
27*3d8817e4Smiod #include "coff/i386.h"
28*3d8817e4Smiod 
29*3d8817e4Smiod #include "coff/internal.h"
30*3d8817e4Smiod 
31*3d8817e4Smiod #ifdef COFF_WITH_PE
32*3d8817e4Smiod #include "coff/pe.h"
33*3d8817e4Smiod #endif
34*3d8817e4Smiod 
35*3d8817e4Smiod #ifdef COFF_GO32_EXE
36*3d8817e4Smiod #include "coff/go32exe.h"
37*3d8817e4Smiod #endif
38*3d8817e4Smiod 
39*3d8817e4Smiod #include "libcoff.h"
40*3d8817e4Smiod 
41*3d8817e4Smiod static bfd_reloc_status_type coff_i386_reloc
42*3d8817e4Smiod   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43*3d8817e4Smiod static reloc_howto_type *coff_i386_rtype_to_howto
44*3d8817e4Smiod   PARAMS ((bfd *, asection *, struct internal_reloc *,
45*3d8817e4Smiod 	   struct coff_link_hash_entry *, struct internal_syment *,
46*3d8817e4Smiod 	   bfd_vma *));
47*3d8817e4Smiod static reloc_howto_type *coff_i386_reloc_type_lookup
48*3d8817e4Smiod   PARAMS ((bfd *, bfd_reloc_code_real_type));
49*3d8817e4Smiod 
50*3d8817e4Smiod #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
51*3d8817e4Smiod /* The page size is a guess based on ELF.  */
52*3d8817e4Smiod 
53*3d8817e4Smiod #define COFF_PAGE_SIZE 0x1000
54*3d8817e4Smiod 
55*3d8817e4Smiod /* For some reason when using i386 COFF the value stored in the .text
56*3d8817e4Smiod    section for a reference to a common symbol is the value itself plus
57*3d8817e4Smiod    any desired offset.  Ian Taylor, Cygnus Support.  */
58*3d8817e4Smiod 
59*3d8817e4Smiod /* If we are producing relocatable output, we need to do some
60*3d8817e4Smiod    adjustments to the object file that are not done by the
61*3d8817e4Smiod    bfd_perform_relocation function.  This function is called by every
62*3d8817e4Smiod    reloc type to make any required adjustments.  */
63*3d8817e4Smiod 
64*3d8817e4Smiod static bfd_reloc_status_type
coff_i386_reloc(abfd,reloc_entry,symbol,data,input_section,output_bfd,error_message)65*3d8817e4Smiod coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
66*3d8817e4Smiod 		 error_message)
67*3d8817e4Smiod      bfd *abfd;
68*3d8817e4Smiod      arelent *reloc_entry;
69*3d8817e4Smiod      asymbol *symbol;
70*3d8817e4Smiod      PTR data;
71*3d8817e4Smiod      asection *input_section ATTRIBUTE_UNUSED;
72*3d8817e4Smiod      bfd *output_bfd;
73*3d8817e4Smiod      char **error_message ATTRIBUTE_UNUSED;
74*3d8817e4Smiod {
75*3d8817e4Smiod   symvalue diff;
76*3d8817e4Smiod 
77*3d8817e4Smiod #ifndef COFF_WITH_PE
78*3d8817e4Smiod   if (output_bfd == (bfd *) NULL)
79*3d8817e4Smiod     return bfd_reloc_continue;
80*3d8817e4Smiod #endif
81*3d8817e4Smiod 
82*3d8817e4Smiod   if (bfd_is_com_section (symbol->section))
83*3d8817e4Smiod     {
84*3d8817e4Smiod #ifndef COFF_WITH_PE
85*3d8817e4Smiod       /* We are relocating a common symbol.  The current value in the
86*3d8817e4Smiod 	 object file is ORIG + OFFSET, where ORIG is the value of the
87*3d8817e4Smiod 	 common symbol as seen by the object file when it was compiled
88*3d8817e4Smiod 	 (this may be zero if the symbol was undefined) and OFFSET is
89*3d8817e4Smiod 	 the offset into the common symbol (normally zero, but may be
90*3d8817e4Smiod 	 non-zero when referring to a field in a common structure).
91*3d8817e4Smiod 	 ORIG is the negative of reloc_entry->addend, which is set by
92*3d8817e4Smiod 	 the CALC_ADDEND macro below.  We want to replace the value in
93*3d8817e4Smiod 	 the object file with NEW + OFFSET, where NEW is the value of
94*3d8817e4Smiod 	 the common symbol which we are going to put in the final
95*3d8817e4Smiod 	 object file.  NEW is symbol->value.  */
96*3d8817e4Smiod       diff = symbol->value + reloc_entry->addend;
97*3d8817e4Smiod #else
98*3d8817e4Smiod       /* In PE mode, we do not offset the common symbol.  */
99*3d8817e4Smiod       diff = reloc_entry->addend;
100*3d8817e4Smiod #endif
101*3d8817e4Smiod     }
102*3d8817e4Smiod   else
103*3d8817e4Smiod     {
104*3d8817e4Smiod       /* For some reason bfd_perform_relocation always effectively
105*3d8817e4Smiod 	 ignores the addend for a COFF target when producing
106*3d8817e4Smiod 	 relocatable output.  This seems to be always wrong for 386
107*3d8817e4Smiod 	 COFF, so we handle the addend here instead.  */
108*3d8817e4Smiod #ifdef COFF_WITH_PE
109*3d8817e4Smiod       if (output_bfd == (bfd *) NULL)
110*3d8817e4Smiod 	{
111*3d8817e4Smiod 	  reloc_howto_type *howto = reloc_entry->howto;
112*3d8817e4Smiod 
113*3d8817e4Smiod 	  /* Although PC relative relocations are very similar between
114*3d8817e4Smiod 	     PE and non-PE formats, but they are off by 1 << howto->size
115*3d8817e4Smiod 	     bytes. For the external relocation, PE is very different
116*3d8817e4Smiod 	     from others. See md_apply_fix3 () in gas/config/tc-i386.c.
117*3d8817e4Smiod 	     When we link PE and non-PE object files together to
118*3d8817e4Smiod 	     generate a non-PE executable, we have to compensate it
119*3d8817e4Smiod 	     here.  */
120*3d8817e4Smiod 	  if (howto->pc_relative && howto->pcrel_offset)
121*3d8817e4Smiod 	    diff = -(1 << howto->size);
122*3d8817e4Smiod 	  else if (symbol->flags & BSF_WEAK)
123*3d8817e4Smiod 	    diff = reloc_entry->addend - symbol->value;
124*3d8817e4Smiod 	  else
125*3d8817e4Smiod 	    diff = -reloc_entry->addend;
126*3d8817e4Smiod 	}
127*3d8817e4Smiod       else
128*3d8817e4Smiod #endif
129*3d8817e4Smiod 	diff = reloc_entry->addend;
130*3d8817e4Smiod     }
131*3d8817e4Smiod 
132*3d8817e4Smiod #ifdef COFF_WITH_PE
133*3d8817e4Smiod   /* FIXME: How should this case be handled?  */
134*3d8817e4Smiod   if (reloc_entry->howto->type == R_IMAGEBASE
135*3d8817e4Smiod       && output_bfd != NULL
136*3d8817e4Smiod       && bfd_get_flavour(output_bfd) == bfd_target_coff_flavour)
137*3d8817e4Smiod     diff -= pe_data (output_bfd)->pe_opthdr.ImageBase;
138*3d8817e4Smiod #endif
139*3d8817e4Smiod 
140*3d8817e4Smiod #define DOIT(x) \
141*3d8817e4Smiod   x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
142*3d8817e4Smiod 
143*3d8817e4Smiod     if (diff != 0)
144*3d8817e4Smiod       {
145*3d8817e4Smiod 	reloc_howto_type *howto = reloc_entry->howto;
146*3d8817e4Smiod 	unsigned char *addr = (unsigned char *) data + reloc_entry->address;
147*3d8817e4Smiod 
148*3d8817e4Smiod 	switch (howto->size)
149*3d8817e4Smiod 	  {
150*3d8817e4Smiod 	  case 0:
151*3d8817e4Smiod 	    {
152*3d8817e4Smiod 	      char x = bfd_get_8 (abfd, addr);
153*3d8817e4Smiod 	      DOIT (x);
154*3d8817e4Smiod 	      bfd_put_8 (abfd, x, addr);
155*3d8817e4Smiod 	    }
156*3d8817e4Smiod 	    break;
157*3d8817e4Smiod 
158*3d8817e4Smiod 	  case 1:
159*3d8817e4Smiod 	    {
160*3d8817e4Smiod 	      short x = bfd_get_16 (abfd, addr);
161*3d8817e4Smiod 	      DOIT (x);
162*3d8817e4Smiod 	      bfd_put_16 (abfd, (bfd_vma) x, addr);
163*3d8817e4Smiod 	    }
164*3d8817e4Smiod 	    break;
165*3d8817e4Smiod 
166*3d8817e4Smiod 	  case 2:
167*3d8817e4Smiod 	    {
168*3d8817e4Smiod 	      long x = bfd_get_32 (abfd, addr);
169*3d8817e4Smiod 	      DOIT (x);
170*3d8817e4Smiod 	      bfd_put_32 (abfd, (bfd_vma) x, addr);
171*3d8817e4Smiod 	    }
172*3d8817e4Smiod 	    break;
173*3d8817e4Smiod 
174*3d8817e4Smiod 	  default:
175*3d8817e4Smiod 	    abort ();
176*3d8817e4Smiod 	  }
177*3d8817e4Smiod       }
178*3d8817e4Smiod 
179*3d8817e4Smiod   /* Now let bfd_perform_relocation finish everything up.  */
180*3d8817e4Smiod   return bfd_reloc_continue;
181*3d8817e4Smiod }
182*3d8817e4Smiod 
183*3d8817e4Smiod #ifdef COFF_WITH_PE
184*3d8817e4Smiod /* Return TRUE if this relocation should appear in the output .reloc
185*3d8817e4Smiod    section.  */
186*3d8817e4Smiod 
187*3d8817e4Smiod static bfd_boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
188*3d8817e4Smiod 
in_reloc_p(abfd,howto)189*3d8817e4Smiod static bfd_boolean in_reloc_p (abfd, howto)
190*3d8817e4Smiod      bfd * abfd ATTRIBUTE_UNUSED;
191*3d8817e4Smiod      reloc_howto_type *howto;
192*3d8817e4Smiod {
193*3d8817e4Smiod   return ! howto->pc_relative && howto->type != R_IMAGEBASE;
194*3d8817e4Smiod }
195*3d8817e4Smiod #endif /* COFF_WITH_PE */
196*3d8817e4Smiod 
197*3d8817e4Smiod #ifndef PCRELOFFSET
198*3d8817e4Smiod #define PCRELOFFSET FALSE
199*3d8817e4Smiod #endif
200*3d8817e4Smiod 
201*3d8817e4Smiod static reloc_howto_type howto_table[] =
202*3d8817e4Smiod {
203*3d8817e4Smiod   EMPTY_HOWTO (0),
204*3d8817e4Smiod   EMPTY_HOWTO (1),
205*3d8817e4Smiod   EMPTY_HOWTO (2),
206*3d8817e4Smiod   EMPTY_HOWTO (3),
207*3d8817e4Smiod   EMPTY_HOWTO (4),
208*3d8817e4Smiod   EMPTY_HOWTO (5),
209*3d8817e4Smiod   HOWTO (R_DIR32,		/* type */
210*3d8817e4Smiod 	 0,			/* rightshift */
211*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
212*3d8817e4Smiod 	 32,			/* bitsize */
213*3d8817e4Smiod 	 FALSE,			/* pc_relative */
214*3d8817e4Smiod 	 0,			/* bitpos */
215*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
216*3d8817e4Smiod 	 coff_i386_reloc,	/* special_function */
217*3d8817e4Smiod 	 "dir32",		/* name */
218*3d8817e4Smiod 	 TRUE,			/* partial_inplace */
219*3d8817e4Smiod 	 0xffffffff,		/* src_mask */
220*3d8817e4Smiod 	 0xffffffff,		/* dst_mask */
221*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
222*3d8817e4Smiod   /* PE IMAGE_REL_I386_DIR32NB relocation (7).	*/
223*3d8817e4Smiod   HOWTO (R_IMAGEBASE,		/* type */
224*3d8817e4Smiod 	 0,			/* rightshift */
225*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
226*3d8817e4Smiod 	 32,			/* bitsize */
227*3d8817e4Smiod 	 FALSE,			/* pc_relative */
228*3d8817e4Smiod 	 0,			/* bitpos */
229*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
230*3d8817e4Smiod 	 coff_i386_reloc,	/* special_function */
231*3d8817e4Smiod 	 "rva32",		/* name */
232*3d8817e4Smiod 	 TRUE,			/* partial_inplace */
233*3d8817e4Smiod 	 0xffffffff,		/* src_mask */
234*3d8817e4Smiod 	 0xffffffff,		/* dst_mask */
235*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
236*3d8817e4Smiod   EMPTY_HOWTO (010),
237*3d8817e4Smiod   EMPTY_HOWTO (011),
238*3d8817e4Smiod   EMPTY_HOWTO (012),
239*3d8817e4Smiod #ifdef COFF_WITH_PE
240*3d8817e4Smiod   /* 32-bit longword section relative relocation (013).  */
241*3d8817e4Smiod   HOWTO (R_SECREL32,		/* type */
242*3d8817e4Smiod 	 0,			/* rightshift */
243*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
244*3d8817e4Smiod 	 32,			/* bitsize */
245*3d8817e4Smiod 	 FALSE,			/* pc_relative */
246*3d8817e4Smiod 	 0,			/* bitpos */
247*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
248*3d8817e4Smiod 	 coff_i386_reloc,	/* special_function */
249*3d8817e4Smiod 	 "secrel32",		/* name */
250*3d8817e4Smiod 	 TRUE,			/* partial_inplace */
251*3d8817e4Smiod 	 0xffffffff,		/* src_mask */
252*3d8817e4Smiod 	 0xffffffff,		/* dst_mask */
253*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
254*3d8817e4Smiod #else
255*3d8817e4Smiod   EMPTY_HOWTO (013),
256*3d8817e4Smiod #endif
257*3d8817e4Smiod   EMPTY_HOWTO (014),
258*3d8817e4Smiod   EMPTY_HOWTO (015),
259*3d8817e4Smiod   EMPTY_HOWTO (016),
260*3d8817e4Smiod   /* Byte relocation (017).  */
261*3d8817e4Smiod   HOWTO (R_RELBYTE,		/* type */
262*3d8817e4Smiod 	 0,			/* rightshift */
263*3d8817e4Smiod 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
264*3d8817e4Smiod 	 8,			/* bitsize */
265*3d8817e4Smiod 	 FALSE,			/* pc_relative */
266*3d8817e4Smiod 	 0,			/* bitpos */
267*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
268*3d8817e4Smiod 	 coff_i386_reloc,	/* special_function */
269*3d8817e4Smiod 	 "8",			/* name */
270*3d8817e4Smiod 	 TRUE,			/* partial_inplace */
271*3d8817e4Smiod 	 0x000000ff,		/* src_mask */
272*3d8817e4Smiod 	 0x000000ff,		/* dst_mask */
273*3d8817e4Smiod 	 PCRELOFFSET),		/* pcrel_offset */
274*3d8817e4Smiod   /* 16-bit word relocation (020).  */
275*3d8817e4Smiod   HOWTO (R_RELWORD,		/* type */
276*3d8817e4Smiod 	 0,			/* rightshift */
277*3d8817e4Smiod 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
278*3d8817e4Smiod 	 16,			/* bitsize */
279*3d8817e4Smiod 	 FALSE,			/* pc_relative */
280*3d8817e4Smiod 	 0,			/* bitpos */
281*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
282*3d8817e4Smiod 	 coff_i386_reloc,	/* special_function */
283*3d8817e4Smiod 	 "16",			/* name */
284*3d8817e4Smiod 	 TRUE,			/* partial_inplace */
285*3d8817e4Smiod 	 0x0000ffff,		/* src_mask */
286*3d8817e4Smiod 	 0x0000ffff,		/* dst_mask */
287*3d8817e4Smiod 	 PCRELOFFSET),		/* pcrel_offset */
288*3d8817e4Smiod   /* 32-bit longword relocation (021).	*/
289*3d8817e4Smiod   HOWTO (R_RELLONG,		/* type */
290*3d8817e4Smiod 	 0,			/* rightshift */
291*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
292*3d8817e4Smiod 	 32,			/* bitsize */
293*3d8817e4Smiod 	 FALSE,			/* pc_relative */
294*3d8817e4Smiod 	 0,			/* bitpos */
295*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
296*3d8817e4Smiod 	 coff_i386_reloc,	/* special_function */
297*3d8817e4Smiod 	 "32",			/* name */
298*3d8817e4Smiod 	 TRUE,			/* partial_inplace */
299*3d8817e4Smiod 	 0xffffffff,		/* src_mask */
300*3d8817e4Smiod 	 0xffffffff,		/* dst_mask */
301*3d8817e4Smiod 	 PCRELOFFSET),		/* pcrel_offset */
302*3d8817e4Smiod   /* Byte PC relative relocation (022).	 */
303*3d8817e4Smiod   HOWTO (R_PCRBYTE,		/* type */
304*3d8817e4Smiod 	 0,			/* rightshift */
305*3d8817e4Smiod 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
306*3d8817e4Smiod 	 8,			/* bitsize */
307*3d8817e4Smiod 	 TRUE,			/* pc_relative */
308*3d8817e4Smiod 	 0,			/* bitpos */
309*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
310*3d8817e4Smiod 	 coff_i386_reloc,	/* special_function */
311*3d8817e4Smiod 	 "DISP8",		/* name */
312*3d8817e4Smiod 	 TRUE,			/* partial_inplace */
313*3d8817e4Smiod 	 0x000000ff,		/* src_mask */
314*3d8817e4Smiod 	 0x000000ff,		/* dst_mask */
315*3d8817e4Smiod 	 PCRELOFFSET),		/* pcrel_offset */
316*3d8817e4Smiod   /* 16-bit word PC relative relocation (023).	*/
317*3d8817e4Smiod   HOWTO (R_PCRWORD,		/* type */
318*3d8817e4Smiod 	 0,			/* rightshift */
319*3d8817e4Smiod 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
320*3d8817e4Smiod 	 16,			/* bitsize */
321*3d8817e4Smiod 	 TRUE,			/* pc_relative */
322*3d8817e4Smiod 	 0,			/* bitpos */
323*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
324*3d8817e4Smiod 	 coff_i386_reloc,	/* special_function */
325*3d8817e4Smiod 	 "DISP16",		/* name */
326*3d8817e4Smiod 	 TRUE,			/* partial_inplace */
327*3d8817e4Smiod 	 0x0000ffff,		/* src_mask */
328*3d8817e4Smiod 	 0x0000ffff,		/* dst_mask */
329*3d8817e4Smiod 	 PCRELOFFSET),		/* pcrel_offset */
330*3d8817e4Smiod   /* 32-bit longword PC relative relocation (024).  */
331*3d8817e4Smiod   HOWTO (R_PCRLONG,		/* type */
332*3d8817e4Smiod 	 0,			/* rightshift */
333*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
334*3d8817e4Smiod 	 32,			/* bitsize */
335*3d8817e4Smiod 	 TRUE,			/* pc_relative */
336*3d8817e4Smiod 	 0,			/* bitpos */
337*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
338*3d8817e4Smiod 	 coff_i386_reloc,	/* special_function */
339*3d8817e4Smiod 	 "DISP32",		/* name */
340*3d8817e4Smiod 	 TRUE,			/* partial_inplace */
341*3d8817e4Smiod 	 0xffffffff,		/* src_mask */
342*3d8817e4Smiod 	 0xffffffff,		/* dst_mask */
343*3d8817e4Smiod 	 PCRELOFFSET)		/* pcrel_offset */
344*3d8817e4Smiod };
345*3d8817e4Smiod 
346*3d8817e4Smiod /* Turn a howto into a reloc  nunmber */
347*3d8817e4Smiod 
348*3d8817e4Smiod #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
349*3d8817e4Smiod #define BADMAG(x) I386BADMAG(x)
350*3d8817e4Smiod #define I386 1			/* Customize coffcode.h */
351*3d8817e4Smiod 
352*3d8817e4Smiod #define RTYPE2HOWTO(cache_ptr, dst)					\
353*3d8817e4Smiod   ((cache_ptr)->howto =							\
354*3d8817e4Smiod    ((dst)->r_type < sizeof (howto_table) / sizeof (howto_table[0])	\
355*3d8817e4Smiod     ? howto_table + (dst)->r_type					\
356*3d8817e4Smiod     : NULL))
357*3d8817e4Smiod 
358*3d8817e4Smiod /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
359*3d8817e4Smiod    library.  On some other COFF targets STYP_BSS is normally
360*3d8817e4Smiod    STYP_NOLOAD.  */
361*3d8817e4Smiod #define BSS_NOLOAD_IS_SHARED_LIBRARY
362*3d8817e4Smiod 
363*3d8817e4Smiod /* Compute the addend of a reloc.  If the reloc is to a common symbol,
364*3d8817e4Smiod    the object file contains the value of the common symbol.  By the
365*3d8817e4Smiod    time this is called, the linker may be using a different symbol
366*3d8817e4Smiod    from a different object file with a different value.  Therefore, we
367*3d8817e4Smiod    hack wildly to locate the original symbol from this file so that we
368*3d8817e4Smiod    can make the correct adjustment.  This macro sets coffsym to the
369*3d8817e4Smiod    symbol from the original file, and uses it to set the addend value
370*3d8817e4Smiod    correctly.  If this is not a common symbol, the usual addend
371*3d8817e4Smiod    calculation is done, except that an additional tweak is needed for
372*3d8817e4Smiod    PC relative relocs.
373*3d8817e4Smiod    FIXME: This macro refers to symbols and asect; these are from the
374*3d8817e4Smiod    calling function, not the macro arguments.  */
375*3d8817e4Smiod 
376*3d8817e4Smiod #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)		\
377*3d8817e4Smiod   {								\
378*3d8817e4Smiod     coff_symbol_type *coffsym = (coff_symbol_type *) NULL;	\
379*3d8817e4Smiod     if (ptr && bfd_asymbol_bfd (ptr) != abfd)			\
380*3d8817e4Smiod       coffsym = (obj_symbols (abfd)				\
381*3d8817e4Smiod 	         + (cache_ptr->sym_ptr_ptr - symbols));		\
382*3d8817e4Smiod     else if (ptr)						\
383*3d8817e4Smiod       coffsym = coff_symbol_from (abfd, ptr);			\
384*3d8817e4Smiod     if (coffsym != (coff_symbol_type *) NULL			\
385*3d8817e4Smiod 	&& coffsym->native->u.syment.n_scnum == 0)		\
386*3d8817e4Smiod       cache_ptr->addend = - coffsym->native->u.syment.n_value;	\
387*3d8817e4Smiod     else if (ptr && bfd_asymbol_bfd (ptr) == abfd		\
388*3d8817e4Smiod 	     && ptr->section != (asection *) NULL)		\
389*3d8817e4Smiod       cache_ptr->addend = - (ptr->section->vma + ptr->value);	\
390*3d8817e4Smiod     else							\
391*3d8817e4Smiod       cache_ptr->addend = 0;					\
392*3d8817e4Smiod     if (ptr && howto_table[reloc.r_type].pc_relative)		\
393*3d8817e4Smiod       cache_ptr->addend += asect->vma;				\
394*3d8817e4Smiod   }
395*3d8817e4Smiod 
396*3d8817e4Smiod /* We use the special COFF backend linker.  For normal i386 COFF, we
397*3d8817e4Smiod    can use the generic relocate_section routine.  For PE, we need our
398*3d8817e4Smiod    own routine.  */
399*3d8817e4Smiod 
400*3d8817e4Smiod #ifndef COFF_WITH_PE
401*3d8817e4Smiod 
402*3d8817e4Smiod #define coff_relocate_section _bfd_coff_generic_relocate_section
403*3d8817e4Smiod 
404*3d8817e4Smiod #else /* COFF_WITH_PE */
405*3d8817e4Smiod 
406*3d8817e4Smiod /* The PE relocate section routine.  The only difference between this
407*3d8817e4Smiod    and the regular routine is that we don't want to do anything for a
408*3d8817e4Smiod    relocatable link.  */
409*3d8817e4Smiod 
410*3d8817e4Smiod static bfd_boolean coff_pe_i386_relocate_section
411*3d8817e4Smiod   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
412*3d8817e4Smiod 	   struct internal_reloc *, struct internal_syment *, asection **));
413*3d8817e4Smiod 
414*3d8817e4Smiod static bfd_boolean
coff_pe_i386_relocate_section(output_bfd,info,input_bfd,input_section,contents,relocs,syms,sections)415*3d8817e4Smiod coff_pe_i386_relocate_section (output_bfd, info, input_bfd,
416*3d8817e4Smiod 			       input_section, contents, relocs, syms,
417*3d8817e4Smiod 			       sections)
418*3d8817e4Smiod      bfd *output_bfd;
419*3d8817e4Smiod      struct bfd_link_info *info;
420*3d8817e4Smiod      bfd *input_bfd;
421*3d8817e4Smiod      asection *input_section;
422*3d8817e4Smiod      bfd_byte *contents;
423*3d8817e4Smiod      struct internal_reloc *relocs;
424*3d8817e4Smiod      struct internal_syment *syms;
425*3d8817e4Smiod      asection **sections;
426*3d8817e4Smiod {
427*3d8817e4Smiod   if (info->relocatable)
428*3d8817e4Smiod     return TRUE;
429*3d8817e4Smiod 
430*3d8817e4Smiod   return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
431*3d8817e4Smiod 					     input_section, contents,
432*3d8817e4Smiod 					     relocs, syms, sections);
433*3d8817e4Smiod }
434*3d8817e4Smiod 
435*3d8817e4Smiod #define coff_relocate_section coff_pe_i386_relocate_section
436*3d8817e4Smiod 
437*3d8817e4Smiod #endif /* COFF_WITH_PE */
438*3d8817e4Smiod 
439*3d8817e4Smiod /* Convert an rtype to howto for the COFF backend linker.  */
440*3d8817e4Smiod 
441*3d8817e4Smiod static reloc_howto_type *
coff_i386_rtype_to_howto(abfd,sec,rel,h,sym,addendp)442*3d8817e4Smiod coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
443*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
444*3d8817e4Smiod      asection *sec;
445*3d8817e4Smiod      struct internal_reloc *rel;
446*3d8817e4Smiod      struct coff_link_hash_entry *h;
447*3d8817e4Smiod      struct internal_syment *sym;
448*3d8817e4Smiod      bfd_vma *addendp;
449*3d8817e4Smiod {
450*3d8817e4Smiod   reloc_howto_type *howto;
451*3d8817e4Smiod 
452*3d8817e4Smiod   if (rel->r_type > sizeof (howto_table) / sizeof (howto_table[0]))
453*3d8817e4Smiod     {
454*3d8817e4Smiod       bfd_set_error (bfd_error_bad_value);
455*3d8817e4Smiod       return NULL;
456*3d8817e4Smiod     }
457*3d8817e4Smiod 
458*3d8817e4Smiod   howto = howto_table + rel->r_type;
459*3d8817e4Smiod 
460*3d8817e4Smiod #ifdef COFF_WITH_PE
461*3d8817e4Smiod   /* Cancel out code in _bfd_coff_generic_relocate_section.  */
462*3d8817e4Smiod   *addendp = 0;
463*3d8817e4Smiod #endif
464*3d8817e4Smiod 
465*3d8817e4Smiod   if (howto->pc_relative)
466*3d8817e4Smiod     *addendp += sec->vma;
467*3d8817e4Smiod 
468*3d8817e4Smiod   if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
469*3d8817e4Smiod     {
470*3d8817e4Smiod       /* This is a common symbol.  The section contents include the
471*3d8817e4Smiod 	 size (sym->n_value) as an addend.  The relocate_section
472*3d8817e4Smiod 	 function will be adding in the final value of the symbol.  We
473*3d8817e4Smiod 	 need to subtract out the current size in order to get the
474*3d8817e4Smiod 	 correct result.  */
475*3d8817e4Smiod 
476*3d8817e4Smiod       BFD_ASSERT (h != NULL);
477*3d8817e4Smiod 
478*3d8817e4Smiod #ifndef COFF_WITH_PE
479*3d8817e4Smiod       /* I think we *do* want to bypass this.  If we don't, I have
480*3d8817e4Smiod 	 seen some data parameters get the wrong relocation address.
481*3d8817e4Smiod 	 If I link two versions with and without this section bypassed
482*3d8817e4Smiod 	 and then do a binary comparison, the addresses which are
483*3d8817e4Smiod 	 different can be looked up in the map.  The case in which
484*3d8817e4Smiod 	 this section has been bypassed has addresses which correspond
485*3d8817e4Smiod 	 to values I can find in the map.  */
486*3d8817e4Smiod       *addendp -= sym->n_value;
487*3d8817e4Smiod #endif
488*3d8817e4Smiod     }
489*3d8817e4Smiod 
490*3d8817e4Smiod #ifndef COFF_WITH_PE
491*3d8817e4Smiod   /* If the output symbol is common (in which case this must be a
492*3d8817e4Smiod      relocatable link), we need to add in the final size of the
493*3d8817e4Smiod      common symbol.  */
494*3d8817e4Smiod   if (h != NULL && h->root.type == bfd_link_hash_common)
495*3d8817e4Smiod     *addendp += h->root.u.c.size;
496*3d8817e4Smiod #endif
497*3d8817e4Smiod 
498*3d8817e4Smiod #ifdef COFF_WITH_PE
499*3d8817e4Smiod   if (howto->pc_relative)
500*3d8817e4Smiod     {
501*3d8817e4Smiod       *addendp -= 4;
502*3d8817e4Smiod 
503*3d8817e4Smiod       /* If the symbol is defined, then the generic code is going to
504*3d8817e4Smiod          add back the symbol value in order to cancel out an
505*3d8817e4Smiod          adjustment it made to the addend.  However, we set the addend
506*3d8817e4Smiod          to 0 at the start of this function.  We need to adjust here,
507*3d8817e4Smiod          to avoid the adjustment the generic code will make.  FIXME:
508*3d8817e4Smiod          This is getting a bit hackish.  */
509*3d8817e4Smiod       if (sym != NULL && sym->n_scnum != 0)
510*3d8817e4Smiod 	*addendp -= sym->n_value;
511*3d8817e4Smiod     }
512*3d8817e4Smiod 
513*3d8817e4Smiod   if (rel->r_type == R_IMAGEBASE
514*3d8817e4Smiod       && (bfd_get_flavour(sec->output_section->owner)
515*3d8817e4Smiod 	  == bfd_target_coff_flavour))
516*3d8817e4Smiod     {
517*3d8817e4Smiod       *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
518*3d8817e4Smiod     }
519*3d8817e4Smiod 
520*3d8817e4Smiod   if (rel->r_type == R_SECREL32)
521*3d8817e4Smiod     {
522*3d8817e4Smiod       bfd_vma osect_vma;
523*3d8817e4Smiod 
524*3d8817e4Smiod       if (h && (h->type == bfd_link_hash_defined
525*3d8817e4Smiod 		|| h->type == bfd_link_hash_defweak))
526*3d8817e4Smiod 	osect_vma = h->root.u.def.section->output_section->vma;
527*3d8817e4Smiod       else
528*3d8817e4Smiod 	{
529*3d8817e4Smiod 	  asection *sec;
530*3d8817e4Smiod 	  int i;
531*3d8817e4Smiod 
532*3d8817e4Smiod 	  /* Sigh, the only way to get the section to offset against
533*3d8817e4Smiod 	     is to find it the hard way.  */
534*3d8817e4Smiod 
535*3d8817e4Smiod 	  for (sec = abfd->sections, i = 1; i < sym->n_scnum; i++)
536*3d8817e4Smiod 	    sec = sec->next;
537*3d8817e4Smiod 
538*3d8817e4Smiod 	  osect_vma = sec->output_section->vma;
539*3d8817e4Smiod 	}
540*3d8817e4Smiod 
541*3d8817e4Smiod       *addendp -= osect_vma;
542*3d8817e4Smiod     }
543*3d8817e4Smiod #endif
544*3d8817e4Smiod 
545*3d8817e4Smiod   return howto;
546*3d8817e4Smiod }
547*3d8817e4Smiod 
548*3d8817e4Smiod #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
549*3d8817e4Smiod 
550*3d8817e4Smiod static reloc_howto_type *
coff_i386_reloc_type_lookup(abfd,code)551*3d8817e4Smiod coff_i386_reloc_type_lookup (abfd, code)
552*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
553*3d8817e4Smiod      bfd_reloc_code_real_type code;
554*3d8817e4Smiod {
555*3d8817e4Smiod   switch (code)
556*3d8817e4Smiod     {
557*3d8817e4Smiod     case BFD_RELOC_RVA:
558*3d8817e4Smiod       return howto_table + R_IMAGEBASE;
559*3d8817e4Smiod     case BFD_RELOC_32:
560*3d8817e4Smiod       return howto_table + R_DIR32;
561*3d8817e4Smiod     case BFD_RELOC_32_PCREL:
562*3d8817e4Smiod       return howto_table + R_PCRLONG;
563*3d8817e4Smiod     case BFD_RELOC_16:
564*3d8817e4Smiod       return howto_table + R_RELWORD;
565*3d8817e4Smiod     case BFD_RELOC_16_PCREL:
566*3d8817e4Smiod       return howto_table + R_PCRWORD;
567*3d8817e4Smiod     case BFD_RELOC_8:
568*3d8817e4Smiod       return howto_table + R_RELBYTE;
569*3d8817e4Smiod     case BFD_RELOC_8_PCREL:
570*3d8817e4Smiod       return howto_table + R_PCRBYTE;
571*3d8817e4Smiod #ifdef COFF_WITH_PE
572*3d8817e4Smiod     case BFD_RELOC_32_SECREL:
573*3d8817e4Smiod       return howto_table + R_SECREL32;
574*3d8817e4Smiod #endif
575*3d8817e4Smiod     default:
576*3d8817e4Smiod       BFD_FAIL ();
577*3d8817e4Smiod       return 0;
578*3d8817e4Smiod     }
579*3d8817e4Smiod }
580*3d8817e4Smiod 
581*3d8817e4Smiod #define coff_rtype_to_howto coff_i386_rtype_to_howto
582*3d8817e4Smiod 
583*3d8817e4Smiod #ifdef TARGET_UNDERSCORE
584*3d8817e4Smiod 
585*3d8817e4Smiod /* If i386 gcc uses underscores for symbol names, then it does not use
586*3d8817e4Smiod    a leading dot for local labels, so if TARGET_UNDERSCORE is defined
587*3d8817e4Smiod    we treat all symbols starting with L as local.  */
588*3d8817e4Smiod 
589*3d8817e4Smiod static bfd_boolean coff_i386_is_local_label_name
590*3d8817e4Smiod   PARAMS ((bfd *, const char *));
591*3d8817e4Smiod 
592*3d8817e4Smiod static bfd_boolean
coff_i386_is_local_label_name(abfd,name)593*3d8817e4Smiod coff_i386_is_local_label_name (abfd, name)
594*3d8817e4Smiod      bfd *abfd;
595*3d8817e4Smiod      const char *name;
596*3d8817e4Smiod {
597*3d8817e4Smiod   if (name[0] == 'L')
598*3d8817e4Smiod     return TRUE;
599*3d8817e4Smiod 
600*3d8817e4Smiod   return _bfd_coff_is_local_label_name (abfd, name);
601*3d8817e4Smiod }
602*3d8817e4Smiod 
603*3d8817e4Smiod #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
604*3d8817e4Smiod 
605*3d8817e4Smiod #endif /* TARGET_UNDERSCORE */
606*3d8817e4Smiod 
607*3d8817e4Smiod #include "coffcode.h"
608*3d8817e4Smiod 
609*3d8817e4Smiod const bfd_target
610*3d8817e4Smiod #ifdef TARGET_SYM
611*3d8817e4Smiod   TARGET_SYM =
612*3d8817e4Smiod #else
613*3d8817e4Smiod   i386coff_vec =
614*3d8817e4Smiod #endif
615*3d8817e4Smiod {
616*3d8817e4Smiod #ifdef TARGET_NAME
617*3d8817e4Smiod   TARGET_NAME,
618*3d8817e4Smiod #else
619*3d8817e4Smiod   "coff-i386",			/* name */
620*3d8817e4Smiod #endif
621*3d8817e4Smiod   bfd_target_coff_flavour,
622*3d8817e4Smiod   BFD_ENDIAN_LITTLE,		/* data byte order is little */
623*3d8817e4Smiod   BFD_ENDIAN_LITTLE,		/* header byte order is little */
624*3d8817e4Smiod 
625*3d8817e4Smiod   (HAS_RELOC | EXEC_P |		/* object flags */
626*3d8817e4Smiod    HAS_LINENO | HAS_DEBUG |
627*3d8817e4Smiod    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
628*3d8817e4Smiod 
629*3d8817e4Smiod   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
630*3d8817e4Smiod #ifdef COFF_WITH_PE
631*3d8817e4Smiod    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY
632*3d8817e4Smiod #endif
633*3d8817e4Smiod    | SEC_CODE | SEC_DATA),
634*3d8817e4Smiod 
635*3d8817e4Smiod #ifdef TARGET_UNDERSCORE
636*3d8817e4Smiod   TARGET_UNDERSCORE,		/* leading underscore */
637*3d8817e4Smiod #else
638*3d8817e4Smiod   0,				/* leading underscore */
639*3d8817e4Smiod #endif
640*3d8817e4Smiod   '/',				/* ar_pad_char */
641*3d8817e4Smiod   15,				/* ar_max_namelen */
642*3d8817e4Smiod 
643*3d8817e4Smiod   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
644*3d8817e4Smiod      bfd_getl32, bfd_getl_signed_32, bfd_putl32,
645*3d8817e4Smiod      bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
646*3d8817e4Smiod   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
647*3d8817e4Smiod      bfd_getl32, bfd_getl_signed_32, bfd_putl32,
648*3d8817e4Smiod      bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
649*3d8817e4Smiod 
650*3d8817e4Smiod /* Note that we allow an object file to be treated as a core file as well.  */
651*3d8817e4Smiod     {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
652*3d8817e4Smiod        bfd_generic_archive_p, coff_object_p},
653*3d8817e4Smiod     {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
654*3d8817e4Smiod        bfd_false},
655*3d8817e4Smiod     {bfd_false, coff_write_object_contents, /* bfd_write_contents */
656*3d8817e4Smiod        _bfd_write_archive_contents, bfd_false},
657*3d8817e4Smiod 
658*3d8817e4Smiod      BFD_JUMP_TABLE_GENERIC (coff),
659*3d8817e4Smiod      BFD_JUMP_TABLE_COPY (coff),
660*3d8817e4Smiod      BFD_JUMP_TABLE_CORE (_bfd_nocore),
661*3d8817e4Smiod      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
662*3d8817e4Smiod      BFD_JUMP_TABLE_SYMBOLS (coff),
663*3d8817e4Smiod      BFD_JUMP_TABLE_RELOCS (coff),
664*3d8817e4Smiod      BFD_JUMP_TABLE_WRITE (coff),
665*3d8817e4Smiod      BFD_JUMP_TABLE_LINK (coff),
666*3d8817e4Smiod      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
667*3d8817e4Smiod 
668*3d8817e4Smiod   NULL,
669*3d8817e4Smiod 
670*3d8817e4Smiod   COFF_SWAP_TABLE
671*3d8817e4Smiod };
672