1 /* BFD back-end for Intel 386 COFF files.
2 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "coff/i386.h"
26 #include "coff/internal.h"
29 static bfd_reloc_status_type coff_i386_reloc PARAMS ((bfd *abfd,
33 asection *input_section,
35 char **error_message));
37 /* For some reason when using i386 COFF the value stored in the .text
38 section for a reference to a common symbol is the value itself plus
39 any desired offset. Ian Taylor, Cygnus Support. */
41 /* If we are producing relocateable output, we need to do some
42 adjustments to the object file that are not done by the
43 bfd_perform_relocation function. This function is called by every
44 reloc type to make any required adjustments. */
46 static bfd_reloc_status_type
47 coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
53 asection *input_section;
59 if (output_bfd == (bfd *) NULL)
60 return bfd_reloc_continue;
62 if (bfd_is_com_section (symbol->section))
64 /* We are relocating a common symbol. The current value in the
65 object file is ORIG + OFFSET, where ORIG is the value of the
66 common symbol as seen by the object file when it was compiled
67 (this may be zero if the symbol was undefined) and OFFSET is
68 the offset into the common symbol (normally zero, but may be
69 non-zero when referring to a field in a common structure).
70 ORIG is the negative of reloc_entry->addend, which is set by
71 the CALC_ADDEND macro below. We want to replace the value in
72 the object file with NEW + OFFSET, where NEW is the value of
73 the common symbol which we are going to put in the final
74 object file. NEW is symbol->value. */
75 diff = symbol->value + reloc_entry->addend;
79 /* For some reason bfd_perform_relocation always effectively
80 ignores the addend for a COFF target when producing
81 relocateable output. This seems to be always wrong for 386
82 COFF, so we handle the addend here instead. */
83 diff = reloc_entry->addend;
87 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
91 const reloc_howto_type *howto = reloc_entry->howto;
92 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
98 char x = bfd_get_8 (abfd, addr);
100 bfd_put_8 (abfd, x, addr);
106 short x = bfd_get_16 (abfd, addr);
108 bfd_put_16 (abfd, x, addr);
114 long x = bfd_get_32 (abfd, addr);
116 bfd_put_32 (abfd, x, addr);
125 /* Now let bfd_perform_relocation finish everything up. */
126 return bfd_reloc_continue;
129 static reloc_howto_type howto_table[] =
137 HOWTO (R_DIR32, /* type */
139 2, /* size (0 = byte, 1 = short, 2 = long) */
141 false, /* pc_relative */
143 complain_overflow_bitfield, /* complain_on_overflow */
144 coff_i386_reloc, /* special_function */
146 true, /* partial_inplace */
147 0xffffffff, /* src_mask */
148 0xffffffff, /* dst_mask */
149 false), /* pcrel_offset */
158 HOWTO (R_RELBYTE, /* type */
160 0, /* size (0 = byte, 1 = short, 2 = long) */
162 false, /* pc_relative */
164 complain_overflow_bitfield, /* complain_on_overflow */
165 coff_i386_reloc, /* special_function */
167 true, /* partial_inplace */
168 0x000000ff, /* src_mask */
169 0x000000ff, /* dst_mask */
170 false), /* pcrel_offset */
171 HOWTO (R_RELWORD, /* type */
173 1, /* size (0 = byte, 1 = short, 2 = long) */
175 false, /* pc_relative */
177 complain_overflow_bitfield, /* complain_on_overflow */
178 coff_i386_reloc, /* special_function */
180 true, /* partial_inplace */
181 0x0000ffff, /* src_mask */
182 0x0000ffff, /* dst_mask */
183 false), /* pcrel_offset */
184 HOWTO (R_RELLONG, /* type */
186 2, /* size (0 = byte, 1 = short, 2 = long) */
188 false, /* pc_relative */
190 complain_overflow_bitfield, /* complain_on_overflow */
191 coff_i386_reloc, /* special_function */
193 true, /* partial_inplace */
194 0xffffffff, /* src_mask */
195 0xffffffff, /* dst_mask */
196 false), /* pcrel_offset */
197 HOWTO (R_PCRBYTE, /* type */
199 0, /* size (0 = byte, 1 = short, 2 = long) */
201 true, /* pc_relative */
203 complain_overflow_signed, /* complain_on_overflow */
204 coff_i386_reloc, /* special_function */
206 true, /* partial_inplace */
207 0x000000ff, /* src_mask */
208 0x000000ff, /* dst_mask */
209 false), /* pcrel_offset */
210 HOWTO (R_PCRWORD, /* type */
212 1, /* size (0 = byte, 1 = short, 2 = long) */
214 true, /* pc_relative */
216 complain_overflow_signed, /* complain_on_overflow */
217 coff_i386_reloc, /* special_function */
219 true, /* partial_inplace */
220 0x0000ffff, /* src_mask */
221 0x0000ffff, /* dst_mask */
222 false), /* pcrel_offset */
223 HOWTO (R_PCRLONG, /* type */
225 2, /* size (0 = byte, 1 = short, 2 = long) */
227 true, /* pc_relative */
229 complain_overflow_signed, /* complain_on_overflow */
230 coff_i386_reloc, /* special_function */
232 true, /* partial_inplace */
233 0xffffffff, /* src_mask */
234 0xffffffff, /* dst_mask */
235 false) /* pcrel_offset */
238 /* Turn a howto into a reloc nunmber */
240 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
241 #define BADMAG(x) I386BADMAG(x)
242 #define I386 1 /* Customize coffcode.h */
244 #define RTYPE2HOWTO(cache_ptr, dst) \
245 cache_ptr->howto = howto_table + (dst)->r_type;
247 /* On SCO Unix 3.2.2 the native assembler generates two .data
248 sections. We handle that by renaming the second one to .data2. It
249 does no harm to do this for any 386 COFF target. */
250 #define TWO_DATA_SECS
252 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
253 library. On some other COFF targets STYP_BSS is normally
255 #define BSS_NOLOAD_IS_SHARED_LIBRARY
257 /* Compute the addend of a reloc. If the reloc is to a common symbol,
258 the object file contains the value of the common symbol. By the
259 time this is called, the linker may be using a different symbol
260 from a different object file with a different value. Therefore, we
261 hack wildly to locate the original symbol from this file so that we
262 can make the correct adjustment. This macro sets coffsym to the
263 symbol from the original file, and uses it to set the addend value
264 correctly. If this is not a common symbol, the usual addend
265 calculation is done, except that an additional tweak is needed for
267 FIXME: This macro refers to symbols and asect; these are from the
268 calling function, not the macro arguments. */
270 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
272 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
273 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
274 coffsym = (obj_symbols (abfd) \
275 + (cache_ptr->sym_ptr_ptr - symbols)); \
277 coffsym = coff_symbol_from (abfd, ptr); \
278 if (coffsym != (coff_symbol_type *) NULL \
279 && coffsym->native->u.syment.n_scnum == 0) \
280 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
281 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
282 && ptr->section != (asection *) NULL) \
283 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
285 cache_ptr->addend = 0; \
286 if (ptr && howto_table[reloc.r_type].pc_relative) \
287 cache_ptr->addend += asect->vma; \
290 #include "coffcode.h"
296 return coff_object_p(a);
309 "coff-i386", /* name */
311 bfd_target_coff_flavour,
312 false, /* data byte order is little */
313 false, /* header byte order is little */
315 (HAS_RELOC | EXEC_P | /* object flags */
316 HAS_LINENO | HAS_DEBUG |
317 HAS_SYMS | HAS_LOCALS | WP_TEXT),
319 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
320 0, /* leading underscore */
321 '/', /* ar_pad_char */
322 15, /* ar_max_namelen */
324 2, /* minimum alignment power */
325 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
326 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
327 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
328 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
329 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
330 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
332 /* Note that we allow an object file to be treated as a core file as well. */
333 {_bfd_dummy_target, i3coff_object_p, /* bfd_check_format */
334 bfd_generic_archive_p, i3coff_object_p},
335 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
337 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
338 _bfd_write_archive_contents, bfd_false},
340 BFD_JUMP_TABLE_GENERIC (coff),
341 BFD_JUMP_TABLE_COPY (coff),
342 BFD_JUMP_TABLE_CORE (_bfd_nocore),
343 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
344 BFD_JUMP_TABLE_SYMBOLS (coff),
345 BFD_JUMP_TABLE_RELOCS (coff),
346 BFD_JUMP_TABLE_WRITE (coff),
347 BFD_JUMP_TABLE_LINK (coff),