daily update
[binutils-gdb.git] / bfd / coff-i860.c
1 /* BFD back-end for Intel 860 COFF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Created mostly by substituting "860" for "386" in coff-i386.c
5 Harry Dolan <dolan@ssd.intel.com>, October 1995
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 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/i860.h"
28
29 #include "coff/internal.h"
30
31 #include "libcoff.h"
32
33
34 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
35 /* The page size is a guess based on ELF. */
36
37 #define COFF_PAGE_SIZE 0x1000
38
39 /* For some reason when using i860 COFF the value stored in the .text
40 section for a reference to a common symbol is the value itself plus
41 any desired offset. Ian Taylor, Cygnus Support. */
42
43 /* If we are producing relocatable output, we need to do some
44 adjustments to the object file that are not done by the
45 bfd_perform_relocation function. This function is called by every
46 reloc type to make any required adjustments. */
47
48 static bfd_reloc_status_type
49 coff_i860_reloc (bfd *abfd,
50 arelent *reloc_entry,
51 asymbol *symbol,
52 void *data,
53 asection *input_section ATTRIBUTE_UNUSED,
54 bfd *output_bfd,
55 char **error_message ATTRIBUTE_UNUSED)
56 {
57 symvalue diff;
58
59 if (output_bfd == (bfd *) NULL)
60 return bfd_reloc_continue;
61
62 if (bfd_is_com_section (symbol->section))
63 {
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;
76 }
77 else
78 {
79 /* For some reason bfd_perform_relocation always effectively
80 ignores the addend for a COFF target when producing
81 relocatable output. This seems to be always wrong for 860
82 COFF, so we handle the addend here instead. */
83 diff = reloc_entry->addend;
84 }
85
86 #define DOIT(x) \
87 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
88
89 if (diff != 0)
90 {
91 reloc_howto_type *howto = reloc_entry->howto;
92 unsigned char *addr = (unsigned char *) data + reloc_entry->address;
93
94 switch (howto->size)
95 {
96 case 0:
97 {
98 char x = bfd_get_8 (abfd, addr);
99 DOIT (x);
100 bfd_put_8 (abfd, x, addr);
101 }
102 break;
103
104 case 1:
105 {
106 short x = bfd_get_16 (abfd, addr);
107 DOIT (x);
108 bfd_put_16 (abfd, (bfd_vma) x, addr);
109 }
110 break;
111
112 case 2:
113 {
114 long x = bfd_get_32 (abfd, addr);
115 DOIT (x);
116 bfd_put_32 (abfd, (bfd_vma) x, addr);
117 }
118 break;
119
120 default:
121 abort ();
122 }
123 }
124
125 /* Now let bfd_perform_relocation finish everything up. */
126 return bfd_reloc_continue;
127 }
128
129 #ifndef PCRELOFFSET
130 #define PCRELOFFSET FALSE
131 #endif
132
133 static reloc_howto_type howto_table[] =
134 {
135 EMPTY_HOWTO (0),
136 EMPTY_HOWTO (1),
137 EMPTY_HOWTO (2),
138 EMPTY_HOWTO (3),
139 EMPTY_HOWTO (4),
140 EMPTY_HOWTO (5),
141 HOWTO (R_DIR32, /* type */
142 0, /* rightshift */
143 2, /* size (0 = byte, 1 = short, 2 = long) */
144 32, /* bitsize */
145 FALSE, /* pc_relative */
146 0, /* bitpos */
147 complain_overflow_bitfield, /* complain_on_overflow */
148 coff_i860_reloc, /* special_function */
149 "dir32", /* name */
150 TRUE, /* partial_inplace */
151 0xffffffff, /* src_mask */
152 0xffffffff, /* dst_mask */
153 TRUE), /* pcrel_offset */
154 /* {7}, */
155 HOWTO (R_IMAGEBASE, /* type */
156 0, /* rightshift */
157 2, /* size (0 = byte, 1 = short, 2 = long) */
158 32, /* bitsize */
159 FALSE, /* pc_relative */
160 0, /* bitpos */
161 complain_overflow_bitfield, /* complain_on_overflow */
162 coff_i860_reloc, /* special_function */
163 "rva32", /* name */
164 TRUE, /* partial_inplace */
165 0xffffffff, /* src_mask */
166 0xffffffff, /* dst_mask */
167 FALSE), /* pcrel_offset */
168 EMPTY_HOWTO (010),
169 EMPTY_HOWTO (011),
170 EMPTY_HOWTO (012),
171 EMPTY_HOWTO (013),
172 EMPTY_HOWTO (014),
173 EMPTY_HOWTO (015),
174 EMPTY_HOWTO (016),
175 HOWTO (R_RELBYTE, /* type */
176 0, /* rightshift */
177 0, /* size (0 = byte, 1 = short, 2 = long) */
178 8, /* bitsize */
179 FALSE, /* pc_relative */
180 0, /* bitpos */
181 complain_overflow_bitfield, /* complain_on_overflow */
182 coff_i860_reloc, /* special_function */
183 "8", /* name */
184 TRUE, /* partial_inplace */
185 0x000000ff, /* src_mask */
186 0x000000ff, /* dst_mask */
187 PCRELOFFSET), /* pcrel_offset */
188 HOWTO (R_RELWORD, /* type */
189 0, /* rightshift */
190 1, /* size (0 = byte, 1 = short, 2 = long) */
191 16, /* bitsize */
192 FALSE, /* pc_relative */
193 0, /* bitpos */
194 complain_overflow_bitfield, /* complain_on_overflow */
195 coff_i860_reloc, /* special_function */
196 "16", /* name */
197 TRUE, /* partial_inplace */
198 0x0000ffff, /* src_mask */
199 0x0000ffff, /* dst_mask */
200 PCRELOFFSET), /* pcrel_offset */
201 HOWTO (R_RELLONG, /* type */
202 0, /* rightshift */
203 2, /* size (0 = byte, 1 = short, 2 = long) */
204 32, /* bitsize */
205 FALSE, /* pc_relative */
206 0, /* bitpos */
207 complain_overflow_bitfield, /* complain_on_overflow */
208 coff_i860_reloc, /* special_function */
209 "32", /* name */
210 TRUE, /* partial_inplace */
211 0xffffffff, /* src_mask */
212 0xffffffff, /* dst_mask */
213 PCRELOFFSET), /* pcrel_offset */
214 HOWTO (R_PCRBYTE, /* type */
215 0, /* rightshift */
216 0, /* size (0 = byte, 1 = short, 2 = long) */
217 8, /* bitsize */
218 TRUE, /* pc_relative */
219 0, /* bitpos */
220 complain_overflow_signed, /* complain_on_overflow */
221 coff_i860_reloc, /* special_function */
222 "DISP8", /* name */
223 TRUE, /* partial_inplace */
224 0x000000ff, /* src_mask */
225 0x000000ff, /* dst_mask */
226 PCRELOFFSET), /* pcrel_offset */
227 HOWTO (R_PCRWORD, /* type */
228 0, /* rightshift */
229 1, /* size (0 = byte, 1 = short, 2 = long) */
230 16, /* bitsize */
231 TRUE, /* pc_relative */
232 0, /* bitpos */
233 complain_overflow_signed, /* complain_on_overflow */
234 coff_i860_reloc, /* special_function */
235 "DISP16", /* name */
236 TRUE, /* partial_inplace */
237 0x0000ffff, /* src_mask */
238 0x0000ffff, /* dst_mask */
239 PCRELOFFSET), /* pcrel_offset */
240 HOWTO (R_PCRLONG, /* type */
241 0, /* rightshift */
242 2, /* size (0 = byte, 1 = short, 2 = long) */
243 32, /* bitsize */
244 TRUE, /* pc_relative */
245 0, /* bitpos */
246 complain_overflow_signed, /* complain_on_overflow */
247 coff_i860_reloc, /* special_function */
248 "DISP32", /* name */
249 TRUE, /* partial_inplace */
250 0xffffffff, /* src_mask */
251 0xffffffff, /* dst_mask */
252 PCRELOFFSET) /* pcrel_offset */
253 };
254
255 /* Turn a howto into a reloc nunmber */
256
257 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
258 #define BADMAG(x) I860BADMAG(x)
259 #define I860 1 /* Customize coffcode.h */
260
261 #define RTYPE2HOWTO(cache_ptr, dst) \
262 (cache_ptr)->howto = howto_table + (dst)->r_type;
263
264 /* For 860 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
265 library. On some other COFF targets STYP_BSS is normally
266 STYP_NOLOAD. */
267 #define BSS_NOLOAD_IS_SHARED_LIBRARY
268
269 /* Compute the addend of a reloc. If the reloc is to a common symbol,
270 the object file contains the value of the common symbol. By the
271 time this is called, the linker may be using a different symbol
272 from a different object file with a different value. Therefore, we
273 hack wildly to locate the original symbol from this file so that we
274 can make the correct adjustment. This macro sets coffsym to the
275 symbol from the original file, and uses it to set the addend value
276 correctly. If this is not a common symbol, the usual addend
277 calculation is done, except that an additional tweak is needed for
278 PC relative relocs.
279 FIXME: This macro refers to symbols and asect; these are from the
280 calling function, not the macro arguments. */
281
282 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
283 { \
284 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
285 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
286 coffsym = (obj_symbols (abfd) \
287 + (cache_ptr->sym_ptr_ptr - symbols)); \
288 else if (ptr) \
289 coffsym = coff_symbol_from (abfd, ptr); \
290 if (coffsym != (coff_symbol_type *) NULL \
291 && coffsym->native->u.syment.n_scnum == 0) \
292 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
293 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
294 && ptr->section != (asection *) NULL) \
295 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
296 else \
297 cache_ptr->addend = 0; \
298 if (ptr && howto_table[reloc.r_type].pc_relative) \
299 cache_ptr->addend += asect->vma; \
300 }
301
302 /* We use the special COFF backend linker. */
303 #define coff_relocate_section _bfd_coff_generic_relocate_section
304
305 static reloc_howto_type *
306 coff_i860_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
307 asection *sec,
308 struct internal_reloc *rel,
309 struct coff_link_hash_entry *h,
310 struct internal_syment *sym,
311 bfd_vma *addendp)
312 {
313
314 reloc_howto_type *howto;
315
316 howto = howto_table + rel->r_type;
317
318 if (howto->pc_relative)
319 *addendp += sec->vma;
320
321 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
322 {
323 /* This is a common symbol. The section contents include the
324 size (sym->n_value) as an addend. The relocate_section
325 function will be adding in the final value of the symbol. We
326 need to subtract out the current size in order to get the
327 correct result. */
328
329 BFD_ASSERT (h != NULL);
330
331 /* I think we *do* want to bypass this. If we don't, I have seen some data
332 parameters get the wrong relcation address. If I link two versions
333 with and without this section bypassed and then do a binary comparison,
334 the addresses which are different can be looked up in the map. The
335 case in which this section has been bypassed has addresses which correspond
336 to values I can find in the map. */
337 *addendp -= sym->n_value;
338 }
339
340 /* If the output symbol is common (in which case this must be a
341 relocatable link), we need to add in the final size of the
342 common symbol. */
343 if (h != NULL && h->root.type == bfd_link_hash_common)
344 *addendp += h->root.u.c.size;
345
346 return howto;
347 }
348
349 #define coff_rtype_to_howto coff_i860_rtype_to_howto
350
351 #include "coffcode.h"
352
353 static const bfd_target *
354 i3coff_object_p(bfd *a)
355 {
356 return coff_object_p (a);
357 }
358
359 const bfd_target
360 #ifdef TARGET_SYM
361 TARGET_SYM =
362 #else
363 i860coff_vec =
364 #endif
365 {
366 #ifdef TARGET_NAME
367 TARGET_NAME,
368 #else
369 "coff-i860", /* name */
370 #endif
371 bfd_target_coff_flavour,
372 BFD_ENDIAN_LITTLE, /* data byte order is little */
373 BFD_ENDIAN_LITTLE, /* header byte order is little */
374
375 (HAS_RELOC | EXEC_P | /* object flags */
376 HAS_LINENO | HAS_DEBUG |
377 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
378
379 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
380 '_', /* leading underscore */
381 '/', /* ar_pad_char */
382 15, /* ar_max_namelen */
383
384 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
385 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
386 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
387 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
388 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
389 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
390
391 /* Note that we allow an object file to be treated as a core file as well. */
392 {_bfd_dummy_target, i3coff_object_p, /* bfd_check_format */
393 bfd_generic_archive_p, i3coff_object_p},
394 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
395 bfd_false},
396 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
397 _bfd_write_archive_contents, bfd_false},
398
399 BFD_JUMP_TABLE_GENERIC (coff),
400 BFD_JUMP_TABLE_COPY (coff),
401 BFD_JUMP_TABLE_CORE (_bfd_nocore),
402 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
403 BFD_JUMP_TABLE_SYMBOLS (coff),
404 BFD_JUMP_TABLE_RELOCS (coff),
405 BFD_JUMP_TABLE_WRITE (coff),
406 BFD_JUMP_TABLE_LINK (coff),
407 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
408
409 NULL,
410
411 COFF_SWAP_TABLE
412 };