Add support for setting model name and other things
[deliverable/binutils-gdb.git] / bfd / coff-i386.c
1 /* BFD back-end for Intel 386 COFF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "obstack.h"
25
26 #include "coff/i386.h"
27
28 #include "coff/internal.h"
29
30 #ifdef COFF_WITH_PE
31 #include "coff/pe.h"
32 #endif
33
34 #include "libcoff.h"
35
36 static bfd_reloc_status_type coff_i386_reloc
37 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
38 static reloc_howto_type *coff_i386_rtype_to_howto
39 PARAMS ((bfd *, asection *, struct internal_reloc *,
40 struct coff_link_hash_entry *, struct internal_syment *,
41
42 bfd_vma *));
43
44 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
45 /* The page size is a guess based on ELF. */
46
47 #define COFF_PAGE_SIZE 0x1000
48
49 /* For some reason when using i386 COFF the value stored in the .text
50 section for a reference to a common symbol is the value itself plus
51 any desired offset. Ian Taylor, Cygnus Support. */
52
53 /* If we are producing relocateable output, we need to do some
54 adjustments to the object file that are not done by the
55 bfd_perform_relocation function. This function is called by every
56 reloc type to make any required adjustments. */
57
58 static bfd_reloc_status_type
59 coff_i386_reloc (abfd, reloc_entry, symbol, data, input_section, output_bfd,
60 error_message)
61 bfd *abfd;
62 arelent *reloc_entry;
63 asymbol *symbol;
64 PTR data;
65 asection *input_section;
66 bfd *output_bfd;
67 char **error_message;
68 {
69 symvalue diff;
70
71 if (output_bfd == (bfd *) NULL)
72 return bfd_reloc_continue;
73
74
75 if (bfd_is_com_section (symbol->section))
76 {
77 /* We are relocating a common symbol. The current value in the
78 object file is ORIG + OFFSET, where ORIG is the value of the
79 common symbol as seen by the object file when it was compiled
80 (this may be zero if the symbol was undefined) and OFFSET is
81 the offset into the common symbol (normally zero, but may be
82 non-zero when referring to a field in a common structure).
83 ORIG is the negative of reloc_entry->addend, which is set by
84 the CALC_ADDEND macro below. We want to replace the value in
85 the object file with NEW + OFFSET, where NEW is the value of
86 the common symbol which we are going to put in the final
87 object file. NEW is symbol->value. */
88 diff = symbol->value + reloc_entry->addend;
89 }
90 else
91 {
92 /* For some reason bfd_perform_relocation always effectively
93 ignores the addend for a COFF target when producing
94 relocateable output. This seems to be always wrong for 386
95 COFF, so we handle the addend here instead. */
96 diff = reloc_entry->addend;
97 }
98
99
100 #ifdef COFF_WITH_PE
101 if (reloc_entry->howto->type == 7)
102 {
103 /* diff -= coff_data(output_bfd)->link_info->pe_info.image_base.value;*/
104 exit(1);
105 }
106 #endif
107
108 #define DOIT(x) \
109 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
110
111 if (diff != 0)
112 {
113 reloc_howto_type *howto = reloc_entry->howto;
114 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
115
116 switch (howto->size)
117 {
118 case 0:
119 {
120 char x = bfd_get_8 (abfd, addr);
121 DOIT (x);
122 bfd_put_8 (abfd, x, addr);
123 }
124 break;
125
126 case 1:
127 {
128 short x = bfd_get_16 (abfd, addr);
129 DOIT (x);
130 bfd_put_16 (abfd, x, addr);
131 }
132 break;
133
134 case 2:
135 {
136 long x = bfd_get_32 (abfd, addr);
137 DOIT (x);
138 bfd_put_32 (abfd, x, addr);
139 }
140 break;
141
142 default:
143 abort ();
144 }
145 }
146
147 /* Now let bfd_perform_relocation finish everything up. */
148 return bfd_reloc_continue;
149 }
150
151
152
153 #ifndef PCRELOFFSET
154 #define PCRELOFFSET false
155 #endif
156
157 static reloc_howto_type howto_table[] =
158 {
159 {0},
160 {1},
161 {2},
162 {3},
163 {4},
164 {5},
165 HOWTO (R_DIR32, /* type */
166 0, /* rightshift */
167 2, /* size (0 = byte, 1 = short, 2 = long) */
168 32, /* bitsize */
169 false, /* pc_relative */
170 0, /* bitpos */
171 complain_overflow_bitfield, /* complain_on_overflow */
172 coff_i386_reloc, /* special_function */
173 "dir32", /* name */
174 true, /* partial_inplace */
175 0xffffffff, /* src_mask */
176 0xffffffff, /* dst_mask */
177 true), /* pcrel_offset */
178 /* {7}, */
179 HOWTO (R_IMAGEBASE, /* type */
180 0, /* rightshift */
181 2, /* size (0 = byte, 1 = short, 2 = long) */
182 32, /* bitsize */
183 false, /* pc_relative */
184 0, /* bitpos */
185 complain_overflow_bitfield, /* complain_on_overflow */
186 coff_i386_reloc, /* special_function */
187 "rva32", /* name */
188 true, /* partial_inplace */
189 0xffffffff, /* src_mask */
190 0xffffffff, /* dst_mask */
191 false), /* pcrel_offset */
192 {010},
193 {011},
194 {012},
195 {013},
196 {014},
197 {015},
198 {016},
199 HOWTO (R_RELBYTE, /* type */
200 0, /* rightshift */
201 0, /* size (0 = byte, 1 = short, 2 = long) */
202 8, /* bitsize */
203 false, /* pc_relative */
204 0, /* bitpos */
205 complain_overflow_bitfield, /* complain_on_overflow */
206 coff_i386_reloc, /* special_function */
207 "8", /* name */
208 true, /* partial_inplace */
209 0x000000ff, /* src_mask */
210 0x000000ff, /* dst_mask */
211 PCRELOFFSET), /* pcrel_offset */
212 HOWTO (R_RELWORD, /* type */
213 0, /* rightshift */
214 1, /* size (0 = byte, 1 = short, 2 = long) */
215 16, /* bitsize */
216 false, /* pc_relative */
217 0, /* bitpos */
218 complain_overflow_bitfield, /* complain_on_overflow */
219 coff_i386_reloc, /* special_function */
220 "16", /* name */
221 true, /* partial_inplace */
222 0x0000ffff, /* src_mask */
223 0x0000ffff, /* dst_mask */
224 PCRELOFFSET), /* pcrel_offset */
225 HOWTO (R_RELLONG, /* type */
226 0, /* rightshift */
227 2, /* size (0 = byte, 1 = short, 2 = long) */
228 32, /* bitsize */
229 false, /* pc_relative */
230 0, /* bitpos */
231 complain_overflow_bitfield, /* complain_on_overflow */
232 coff_i386_reloc, /* special_function */
233 "32", /* name */
234 true, /* partial_inplace */
235 0xffffffff, /* src_mask */
236 0xffffffff, /* dst_mask */
237 PCRELOFFSET), /* pcrel_offset */
238 HOWTO (R_PCRBYTE, /* type */
239 0, /* rightshift */
240 0, /* size (0 = byte, 1 = short, 2 = long) */
241 8, /* bitsize */
242 true, /* pc_relative */
243 0, /* bitpos */
244 complain_overflow_signed, /* complain_on_overflow */
245 coff_i386_reloc, /* special_function */
246 "DISP8", /* name */
247 true, /* partial_inplace */
248 0x000000ff, /* src_mask */
249 0x000000ff, /* dst_mask */
250 PCRELOFFSET), /* pcrel_offset */
251 HOWTO (R_PCRWORD, /* type */
252 0, /* rightshift */
253 1, /* size (0 = byte, 1 = short, 2 = long) */
254 16, /* bitsize */
255 true, /* pc_relative */
256 0, /* bitpos */
257 complain_overflow_signed, /* complain_on_overflow */
258 coff_i386_reloc, /* special_function */
259 "DISP16", /* name */
260 true, /* partial_inplace */
261 0x0000ffff, /* src_mask */
262 0x0000ffff, /* dst_mask */
263 PCRELOFFSET), /* pcrel_offset */
264 HOWTO (R_PCRLONG, /* type */
265 0, /* rightshift */
266 2, /* size (0 = byte, 1 = short, 2 = long) */
267 32, /* bitsize */
268 true, /* pc_relative */
269 0, /* bitpos */
270 complain_overflow_signed, /* complain_on_overflow */
271 coff_i386_reloc, /* special_function */
272 "DISP32", /* name */
273 true, /* partial_inplace */
274 0xffffffff, /* src_mask */
275 0xffffffff, /* dst_mask */
276 PCRELOFFSET) /* pcrel_offset */
277 };
278
279 /* Turn a howto into a reloc nunmber */
280
281 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
282 #define BADMAG(x) I386BADMAG(x)
283 #define I386 1 /* Customize coffcode.h */
284
285 #define RTYPE2HOWTO(cache_ptr, dst) \
286 (cache_ptr)->howto = howto_table + (dst)->r_type;
287
288 /* On SCO Unix 3.2.2 the native assembler generates two .data
289 sections. We handle that by renaming the second one to .data2. It
290 does no harm to do this for any 386 COFF target. */
291 #define TWO_DATA_SECS
292
293 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
294 library. On some other COFF targets STYP_BSS is normally
295 STYP_NOLOAD. */
296 #define BSS_NOLOAD_IS_SHARED_LIBRARY
297
298 /* Compute the addend of a reloc. If the reloc is to a common symbol,
299 the object file contains the value of the common symbol. By the
300 time this is called, the linker may be using a different symbol
301 from a different object file with a different value. Therefore, we
302 hack wildly to locate the original symbol from this file so that we
303 can make the correct adjustment. This macro sets coffsym to the
304 symbol from the original file, and uses it to set the addend value
305 correctly. If this is not a common symbol, the usual addend
306 calculation is done, except that an additional tweak is needed for
307 PC relative relocs.
308 FIXME: This macro refers to symbols and asect; these are from the
309 calling function, not the macro arguments. */
310
311 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
312 { \
313 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
314 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
315 coffsym = (obj_symbols (abfd) \
316 + (cache_ptr->sym_ptr_ptr - symbols)); \
317 else if (ptr) \
318 coffsym = coff_symbol_from (abfd, ptr); \
319 if (coffsym != (coff_symbol_type *) NULL \
320 && coffsym->native->u.syment.n_scnum == 0) \
321 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
322 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
323 && ptr->section != (asection *) NULL) \
324 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
325 else \
326 cache_ptr->addend = 0; \
327 if (ptr && howto_table[reloc.r_type].pc_relative) \
328 cache_ptr->addend += asect->vma; \
329 }
330
331 /* We use the special COFF backend linker. */
332 #define coff_relocate_section _bfd_coff_generic_relocate_section
333
334 static reloc_howto_type *
335 coff_i386_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
336 bfd *abfd;
337 asection *sec;
338 struct internal_reloc *rel;
339 struct coff_link_hash_entry *h;
340 struct internal_syment *sym;
341 bfd_vma *addendp;
342 {
343
344 reloc_howto_type *howto;
345
346 howto = howto_table + rel->r_type;
347
348 #ifdef COFF_WITH_PE
349 *addendp = 0;
350 #endif
351
352 if (howto->pc_relative)
353 *addendp += sec->vma;
354
355 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
356 {
357 /* This is a common symbol. The section contents include the
358 size (sym->n_value) as an addend. The relocate_section
359 function will be adding in the final value of the symbol. We
360 need to subtract out the current size in order to get the
361 correct result. */
362
363 BFD_ASSERT (h != NULL);
364
365
366 #ifndef COFF_WITH_PE
367 /* I think we *do* want to bypass this. If we don't, I have seen some data
368 parameters get the wrong relcation address. If I link two versions
369 with and without this section bypassed and then do a binary comparison,
370 the addresses which are different can be looked up in the map. The
371 case in which this section has been bypassed has addresses which correspond
372 to values I can find in the map */
373 *addendp -= sym->n_value;
374 #endif
375 }
376
377 /* If the output symbol is common (in which case this must be a
378 relocateable link), we need to add in the final size of the
379 common symbol. */
380 if (h != NULL && h->root.type == bfd_link_hash_common)
381 *addendp += h->root.u.c.size;
382
383
384 #ifdef COFF_WITH_PE
385 if (howto->pc_relative)
386 *addendp -= 4;
387
388 if (rel->r_type == R_IMAGEBASE)
389 {
390 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
391 }
392 #endif
393
394 return howto;
395 }
396
397 #define coff_rtype_to_howto coff_i386_rtype_to_howto
398
399 #include "coffcode.h"
400
401 static const bfd_target *
402 i3coff_object_p(a)
403 bfd *a;
404 {
405 return coff_object_p(a);
406 }
407
408 const bfd_target
409 #ifdef TARGET_SYM
410 TARGET_SYM =
411 #else
412 i386coff_vec =
413 #endif
414 {
415 #ifdef TARGET_NAME
416 TARGET_NAME,
417 #else
418 "coff-i386", /* name */
419 #endif
420 bfd_target_coff_flavour,
421 false, /* data byte order is little */
422 false, /* header byte order is little */
423
424 (HAS_RELOC | EXEC_P | /* object flags */
425 HAS_LINENO | HAS_DEBUG |
426 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
427
428 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
429 #ifdef TARGET_UNDERSCORE
430 TARGET_UNDERSCORE, /* leading underscore */
431 #else
432 0, /* leading underscore */
433 #endif
434 '/', /* ar_pad_char */
435 15, /* ar_max_namelen */
436
437 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
438 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
439 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
440 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
441 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
442 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
443
444 /* Note that we allow an object file to be treated as a core file as well. */
445 {_bfd_dummy_target, i3coff_object_p, /* bfd_check_format */
446 bfd_generic_archive_p, i3coff_object_p},
447 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
448 bfd_false},
449 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
450 _bfd_write_archive_contents, bfd_false},
451
452 BFD_JUMP_TABLE_GENERIC (coff),
453 BFD_JUMP_TABLE_COPY (coff),
454 BFD_JUMP_TABLE_CORE (_bfd_nocore),
455 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
456 BFD_JUMP_TABLE_SYMBOLS (coff),
457 BFD_JUMP_TABLE_RELOCS (coff),
458 BFD_JUMP_TABLE_WRITE (coff),
459 BFD_JUMP_TABLE_LINK (coff),
460 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
461
462 COFF_SWAP_TABLE,
463 };
This page took 0.040392 seconds and 4 git commands to generate.