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