source: trunk/src/binutils/bfd/coff-i386.c@ 88

Last change on this file since 88 was 10, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 18.8 KB
Line 
1/* BFD back-end for Intel 386 COFF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
6
7This file is part of BFD, the Binary File Descriptor library.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23#include "bfd.h"
24#include "sysdep.h"
25#include "libbfd.h"
26
27#include "coff/i386.h"
28
29#include "coff/internal.h"
30
31#ifdef COFF_WITH_PE
32#include "coff/pe.h"
33#endif
34
35#ifdef COFF_GO32_EXE
36#include "coff/go32exe.h"
37#endif
38
39#include "libcoff.h"
40
41static bfd_reloc_status_type coff_i386_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43static reloc_howto_type *coff_i386_rtype_to_howto
44 PARAMS ((bfd *, asection *, struct internal_reloc *,
45 struct coff_link_hash_entry *, struct internal_syment *,
46 bfd_vma *));
47static reloc_howto_type *coff_i386_reloc_type_lookup
48 PARAMS ((bfd *, bfd_reloc_code_real_type));
49
50#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
51/* The page size is a guess based on ELF. */
52
53#define COFF_PAGE_SIZE 0x1000
54
55/* For some reason when using i386 COFF the value stored in the .text
56 section for a reference to a common symbol is the value itself plus
57 any desired offset. Ian Taylor, Cygnus Support. */
58
59/* If we are producing relocateable output, we need to do some
60 adjustments to the object file that are not done by the
61 bfd_perform_relocation function. This function is called by every
62 reloc type to make any required adjustments. */
63
64static bfd_reloc_status_type
65coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
66 error_message)
67 bfd *abfd;
68 arelent *reloc_entry;
69 asymbol *symbol;
70 PTR data;
71 asection *input_section ATTRIBUTE_UNUSED;
72 bfd *output_bfd;
73 char **error_message ATTRIBUTE_UNUSED;
74{
75 symvalue diff;
76
77#ifndef COFF_WITH_PE
78 if (output_bfd == (bfd *) NULL)
79 return bfd_reloc_continue;
80#endif
81
82 if (bfd_is_com_section (symbol->section))
83 {
84#ifndef COFF_WITH_PE
85 /* We are relocating a common symbol. The current value in the
86 object file is ORIG + OFFSET, where ORIG is the value of the
87 common symbol as seen by the object file when it was compiled
88 (this may be zero if the symbol was undefined) and OFFSET is
89 the offset into the common symbol (normally zero, but may be
90 non-zero when referring to a field in a common structure).
91 ORIG is the negative of reloc_entry->addend, which is set by
92 the CALC_ADDEND macro below. We want to replace the value in
93 the object file with NEW + OFFSET, where NEW is the value of
94 the common symbol which we are going to put in the final
95 object file. NEW is symbol->value. */
96 diff = symbol->value + reloc_entry->addend;
97#else
98 /* In PE mode, we do not offset the common symbol. */
99 diff = reloc_entry->addend;
100#endif
101 }
102 else
103 {
104 /* For some reason bfd_perform_relocation always effectively
105 ignores the addend for a COFF target when producing
106 relocateable output. This seems to be always wrong for 386
107 COFF, so we handle the addend here instead. */
108#ifdef COFF_WITH_PE
109 if (output_bfd == (bfd *) NULL)
110 {
111 reloc_howto_type *howto = reloc_entry->howto;
112
113 /* Although PC relative relocations are very similar between
114 PE and non-PE formats, but they are off by 1 << howto->size
115 bytes. For the external relocation, PE is very different
116 from others. See md_apply_fix3 () in gas/config/tc-i386.c.
117 When we link PE and non-PE object files together to
118 generate a non-PE executable, we have to compensate it
119 here. */
120 if (howto->pc_relative == true && howto->pcrel_offset == true)
121 diff = -(1 << howto->size);
122 else
123 diff = -reloc_entry->addend;
124 }
125 else
126#endif
127 diff = reloc_entry->addend;
128 }
129
130#ifdef COFF_WITH_PE
131 /* FIXME: How should this case be handled? */
132 if (reloc_entry->howto->type == R_IMAGEBASE
133 && output_bfd != NULL
134 && bfd_get_flavour(output_bfd) == bfd_target_coff_flavour)
135 diff -= pe_data (output_bfd)->pe_opthdr.ImageBase;
136#endif
137
138#define DOIT(x) \
139 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
140
141 if (diff != 0)
142 {
143 reloc_howto_type *howto = reloc_entry->howto;
144 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
145
146 switch (howto->size)
147 {
148 case 0:
149 {
150 char x = bfd_get_8 (abfd, addr);
151 DOIT (x);
152 bfd_put_8 (abfd, x, addr);
153 }
154 break;
155
156 case 1:
157 {
158 short x = bfd_get_16 (abfd, addr);
159 DOIT (x);
160 bfd_put_16 (abfd, x, addr);
161 }
162 break;
163
164 case 2:
165 {
166 long x = bfd_get_32 (abfd, addr);
167 DOIT (x);
168 bfd_put_32 (abfd, x, addr);
169 }
170 break;
171
172 default:
173 abort ();
174 }
175 }
176
177 /* Now let bfd_perform_relocation finish everything up. */
178 return bfd_reloc_continue;
179}
180
181#ifdef COFF_WITH_PE
182/* Return true if this relocation should appear in the output .reloc
183 section. */
184
185static boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
186
187static boolean in_reloc_p (abfd, howto)
188 bfd * abfd ATTRIBUTE_UNUSED;
189 reloc_howto_type *howto;
190{
191 return ! howto->pc_relative && howto->type != R_IMAGEBASE;
192}
193#endif /* COFF_WITH_PE */
194
195#ifndef PCRELOFFSET
196#define PCRELOFFSET false
197#endif
198
199static reloc_howto_type howto_table[] =
200{
201 EMPTY_HOWTO (0),
202 EMPTY_HOWTO (1),
203 EMPTY_HOWTO (2),
204 EMPTY_HOWTO (3),
205 EMPTY_HOWTO (4),
206 EMPTY_HOWTO (5),
207 HOWTO (R_DIR32, /* type */
208 0, /* rightshift */
209 2, /* size (0 = byte, 1 = short, 2 = long) */
210 32, /* bitsize */
211 false, /* pc_relative */
212 0, /* bitpos */
213 complain_overflow_bitfield, /* complain_on_overflow */
214 coff_i386_reloc, /* special_function */
215 "dir32", /* name */
216 true, /* partial_inplace */
217 0xffffffff, /* src_mask */
218 0xffffffff, /* dst_mask */
219 true), /* pcrel_offset */
220 /* PE IMAGE_REL_I386_DIR32NB relocation (7). */
221 HOWTO (R_IMAGEBASE, /* type */
222 0, /* rightshift */
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 32, /* bitsize */
225 false, /* pc_relative */
226 0, /* bitpos */
227 complain_overflow_bitfield, /* complain_on_overflow */
228 coff_i386_reloc, /* special_function */
229 "rva32", /* name */
230 true, /* partial_inplace */
231 0xffffffff, /* src_mask */
232 0xffffffff, /* dst_mask */
233 false), /* pcrel_offset */
234 EMPTY_HOWTO (010),
235 EMPTY_HOWTO (011),
236 EMPTY_HOWTO (012),
237 EMPTY_HOWTO (013),
238 EMPTY_HOWTO (014),
239 EMPTY_HOWTO (015),
240 EMPTY_HOWTO (016),
241 /* Byte relocation (017). */
242 HOWTO (R_RELBYTE, /* type */
243 0, /* rightshift */
244 0, /* size (0 = byte, 1 = short, 2 = long) */
245 8, /* bitsize */
246 false, /* pc_relative */
247 0, /* bitpos */
248 complain_overflow_bitfield, /* complain_on_overflow */
249 coff_i386_reloc, /* special_function */
250 "8", /* name */
251 true, /* partial_inplace */
252 0x000000ff, /* src_mask */
253 0x000000ff, /* dst_mask */
254 PCRELOFFSET), /* pcrel_offset */
255 /* 16-bit word relocation (020). */
256 HOWTO (R_RELWORD, /* type */
257 0, /* rightshift */
258 1, /* size (0 = byte, 1 = short, 2 = long) */
259 16, /* bitsize */
260 false, /* pc_relative */
261 0, /* bitpos */
262 complain_overflow_bitfield, /* complain_on_overflow */
263 coff_i386_reloc, /* special_function */
264 "16", /* name */
265 true, /* partial_inplace */
266 0x0000ffff, /* src_mask */
267 0x0000ffff, /* dst_mask */
268 PCRELOFFSET), /* pcrel_offset */
269 /* 32-bit longword relocation (021). */
270 HOWTO (R_RELLONG, /* type */
271 0, /* rightshift */
272 2, /* size (0 = byte, 1 = short, 2 = long) */
273 32, /* bitsize */
274 false, /* pc_relative */
275 0, /* bitpos */
276 complain_overflow_bitfield, /* complain_on_overflow */
277 coff_i386_reloc, /* special_function */
278 "32", /* name */
279 true, /* partial_inplace */
280 0xffffffff, /* src_mask */
281 0xffffffff, /* dst_mask */
282 PCRELOFFSET), /* pcrel_offset */
283 /* Byte PC relative relocation (022). */
284 HOWTO (R_PCRBYTE, /* type */
285 0, /* rightshift */
286 0, /* size (0 = byte, 1 = short, 2 = long) */
287 8, /* bitsize */
288 true, /* pc_relative */
289 0, /* bitpos */
290 complain_overflow_signed, /* complain_on_overflow */
291 coff_i386_reloc, /* special_function */
292 "DISP8", /* name */
293 true, /* partial_inplace */
294 0x000000ff, /* src_mask */
295 0x000000ff, /* dst_mask */
296 PCRELOFFSET), /* pcrel_offset */
297 /* 16-bit word PC relative relocation (023). */
298 HOWTO (R_PCRWORD, /* type */
299 0, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 16, /* bitsize */
302 true, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_signed, /* complain_on_overflow */
305 coff_i386_reloc, /* special_function */
306 "DISP16", /* name */
307 true, /* partial_inplace */
308 0x0000ffff, /* src_mask */
309 0x0000ffff, /* dst_mask */
310 PCRELOFFSET), /* pcrel_offset */
311 /* 32-bit longword PC relative relocation (024). */
312 HOWTO (R_PCRLONG, /* type */
313 0, /* rightshift */
314 2, /* size (0 = byte, 1 = short, 2 = long) */
315 32, /* bitsize */
316 true, /* pc_relative */
317 0, /* bitpos */
318 complain_overflow_signed, /* complain_on_overflow */
319 coff_i386_reloc, /* special_function */
320 "DISP32", /* name */
321 true, /* partial_inplace */
322 0xffffffff, /* src_mask */
323 0xffffffff, /* dst_mask */
324 PCRELOFFSET) /* pcrel_offset */
325};
326
327/* Turn a howto into a reloc nunmber */
328
329#define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
330#define BADMAG(x) I386BADMAG(x)
331#define I386 1 /* Customize coffcode.h */
332
333#define RTYPE2HOWTO(cache_ptr, dst) \