OSDN Git Service

Initial revision
[pf3gnuchains/pf3gnuchains3x.git] / bfd / coff-m88k.c
1 /* BFD back-end for Motorola 88000 COFF "Binary Compatability Standard" files.
2    Copyright 1990, 91, 92, 93, 94, 95, 97, 1998 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 #define M88 1           /* Customize various include files */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "coff/m88k.h"
26 #include "coff/internal.h"
27 #include "libcoff.h"
28
29 static boolean m88k_is_local_label_name PARAMS ((bfd *, const char *));
30 static bfd_reloc_status_type m88k_special_reloc
31   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
32 static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
33 static void reloc_processing
34   PARAMS ((arelent *, struct internal_reloc *, asymbol **, bfd *, asection *));
35
36 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
37
38 #define GET_SCNHDR_NRELOC bfd_h_get_32
39 #define GET_SCNHDR_NLNNO bfd_h_get_32
40
41 /* On coff-m88k, local labels start with '@'.  */
42
43 #define coff_bfd_is_local_label_name m88k_is_local_label_name
44
45 static boolean
46 m88k_is_local_label_name (abfd, name)
47      bfd *abfd;
48      const char *name;
49 {
50   return name[0] == '@';
51 }
52
53 static bfd_reloc_status_type 
54 m88k_special_reloc (abfd, reloc_entry, symbol, data,
55                     input_section, output_bfd, error_message)
56      bfd *abfd;
57      arelent *reloc_entry;
58      asymbol *symbol;
59      PTR data;
60      asection *input_section;
61      bfd *output_bfd;
62      char **error_message;
63 {
64   reloc_howto_type *howto = reloc_entry->howto;
65
66   switch (howto->type)
67     {
68     case R_HVRT16:
69     case R_LVRT16:
70       if (output_bfd != (bfd *) NULL)
71         {
72           /* This is a partial relocation, and we want to apply the
73              relocation to the reloc entry rather than the raw data.
74              Modify the reloc inplace to reflect what we now know.  */
75
76           reloc_entry->address += input_section->output_offset;
77         }
78       else
79         {
80           bfd_vma output_base = 0;
81           bfd_vma addr = reloc_entry->address;
82           bfd_vma x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
83           asection *reloc_target_output_section;
84           long relocation = 0;
85
86           /* Work out which section the relocation is targetted at and the
87              initial relocation command value.  */
88
89           /* Get symbol value.  (Common symbols are special.)  */
90           if (bfd_is_com_section (symbol->section))
91             relocation = 0;
92           else
93             relocation = symbol->value;
94
95           reloc_target_output_section = symbol->section->output_section;
96
97           /* Convert input-section-relative symbol value to absolute.  */
98           if (output_bfd)
99             output_base = 0;
100           else
101             output_base = reloc_target_output_section->vma;
102
103           relocation += output_base + symbol->section->output_offset;
104
105           /* Add in supplied addend.  */
106           relocation += ((reloc_entry->addend << howto->bitsize) + x);
107
108           reloc_entry->addend = 0;
109
110           relocation >>= (bfd_vma) howto->rightshift;
111
112           /* Shift everything up to where it's going to be used */
113
114           relocation <<= (bfd_vma) howto->bitpos;
115
116           if (relocation)
117               bfd_put_16 (abfd, relocation, (unsigned char *) data + addr);
118         }
119
120       /* If we are not producing relocateable output, return an error if
121          the symbol is not defined.  */
122       if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
123         return bfd_reloc_undefined;
124
125       return bfd_reloc_ok;
126
127     default:
128       if (output_bfd != (bfd *) NULL)
129         {
130           /* This is a partial relocation, and we want to apply the
131              relocation to the reloc entry rather than the raw data.
132              Modify the reloc inplace to reflect what we now know.  */
133
134           reloc_entry->address += input_section->output_offset;
135           return bfd_reloc_ok;
136         }
137       break;
138     }
139
140   if (output_bfd == (bfd *) NULL)
141     return bfd_reloc_continue;
142
143   return bfd_reloc_ok;
144 }
145
146 static reloc_howto_type howto_table[] = 
147 {
148   HOWTO (R_PCR16L,                      /* type */
149          02,                            /* rightshift */
150          1,                             /* size (0 = byte, 1 = short, 2 = long) */
151          16,                            /* bitsize */
152          true,                          /* pc_relative */
153          0,                             /* bitpos */
154          complain_overflow_signed,      /* complain_on_overflow */
155          m88k_special_reloc,            /* special_function */
156          "PCR16L",                      /* name */
157          false,                         /* partial_inplace */
158          0x0000ffff,                    /* src_mask */
159          0x0000ffff,                    /* dst_mask */
160          true),                         /* pcrel_offset */
161
162   HOWTO (R_PCR26L,                      /* type */
163          02,                            /* rightshift */
164          2,                             /* size (0 = byte, 1 = short, 2 = long) */
165          26,                            /* bitsize */
166          true,                          /* pc_relative */
167          0,                             /* bitpos */
168          complain_overflow_signed,      /* complain_on_overflow */
169          m88k_special_reloc,            /* special_function */
170          "PCR26L",                      /* name */
171          false,                         /* partial_inplace */
172          0x03ffffff,                    /* src_mask */
173          0x03ffffff,                    /* dst_mask */
174          true),                         /* pcrel_offset */
175
176   HOWTO (R_VRT16,                       /* type */
177          00,                            /* rightshift */
178          1,                             /* size (0 = byte, 1 = short, 2 = long) */
179          16,                            /* bitsize */
180          false,                         /* pc_relative */
181          0,                             /* bitpos */
182          complain_overflow_bitfield,    /* complain_on_overflow */
183          m88k_special_reloc,            /* special_function */
184          "VRT16",                       /* name */
185          false,                         /* partial_inplace */
186          0x0000ffff,                    /* src_mask */
187          0x0000ffff,                    /* dst_mask */
188          true),                         /* pcrel_offset */
189
190   HOWTO (R_HVRT16,                      /* type */
191          16,                            /* rightshift */
192          1,                             /* size (0 = byte, 1 = short, 2 = long) */
193          16,                            /* bitsize */
194          false,                         /* pc_relative */
195          0,                             /* bitpos */
196          complain_overflow_dont,        /* complain_on_overflow */
197          m88k_special_reloc,            /* special_function */
198          "HVRT16",                      /* name */
199          false,                         /* partial_inplace */
200          0x0000ffff,                    /* src_mask */
201          0x0000ffff,                    /* dst_mask */
202          true),                         /* pcrel_offset */
203
204   HOWTO (R_LVRT16,                      /* type */
205          00,                            /* rightshift */
206          1,                             /* size (0 = byte, 1 = short, 2 = long) */
207          16,                            /* bitsize */
208          false,                         /* pc_relative */
209          0,                             /* bitpos */
210          complain_overflow_dont,        /* complain_on_overflow */
211          m88k_special_reloc,            /* special_function */
212          "LVRT16",                      /* name */
213          false,                         /* partial_inplace */
214          0x0000ffff,                    /* src_mask */
215          0x0000ffff,                    /* dst_mask */
216          true),                         /* pcrel_offset */
217
218   HOWTO (R_VRT32,                       /* type */
219          00,                            /* rightshift */
220          2,                             /* size (0 = byte, 1 = short, 2 = long) */
221          32,                            /* bitsize */
222          false,                         /* pc_relative */
223          0,                             /* bitpos */
224          complain_overflow_bitfield,    /* complain_on_overflow */
225          m88k_special_reloc,            /* special_function */
226          "VRT32",                       /* name */
227          false,                         /* partial_inplace */
228          0xffffffff,                    /* src_mask */
229          0xffffffff,                    /* dst_mask */
230          true),                         /* pcrel_offset */
231 };
232
233 /* Code to turn an external r_type into a pointer to an entry in the
234    above howto table.  */
235 static void
236 rtype2howto (cache_ptr, dst)
237      arelent *cache_ptr;
238      struct internal_reloc *dst;
239 {
240   if (dst->r_type >= R_PCR16L && dst->r_type <= R_VRT32)
241     {
242       cache_ptr->howto = howto_table + dst->r_type - R_PCR16L;
243     }
244   else
245     {
246       BFD_ASSERT (0);
247     }
248 }
249
250 #define RTYPE2HOWTO(cache_ptr, dst) rtype2howto (cache_ptr, dst)
251
252
253 /* Code to swap in the reloc offset */
254 #define SWAP_IN_RELOC_OFFSET  bfd_h_get_16
255 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_16
256
257
258 #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section)     \
259   reloc_processing(relent, reloc, symbols, abfd, section)
260
261 static void
262 reloc_processing (relent, reloc, symbols, abfd, section)
263      arelent *relent;
264      struct internal_reloc *reloc;
265      asymbol **symbols;
266      bfd *abfd;
267      asection *section;
268 {
269   relent->address = reloc->r_vaddr;
270   rtype2howto (relent, reloc);
271
272   if (((int) reloc->r_symndx) > 0)
273     {
274       relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
275     }
276   else
277     {
278       relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
279     }
280
281   relent->addend = reloc->r_offset;
282   relent->address -= section->vma;
283 }
284
285 #define BADMAG(x) MC88BADMAG(x)
286 #include "coffcode.h"
287
288 #undef coff_write_armap
289
290 const bfd_target m88kbcs_vec =
291 {
292   "coff-m88kbcs",               /* name */
293   bfd_target_coff_flavour,
294   BFD_ENDIAN_BIG,               /* data byte order is big */
295   BFD_ENDIAN_BIG,               /* header byte order is big */
296
297   (HAS_RELOC | EXEC_P |         /* object flags */
298    HAS_LINENO | HAS_DEBUG |
299    HAS_SYMS | HAS_LOCALS | WP_TEXT),
300
301   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
302   '_',                          /* leading underscore */
303   '/',                          /* ar_pad_char */
304   15,                           /* ar_max_namelen */
305   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
306      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
307      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
308   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
309      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
310      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
311
312     {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
313        bfd_generic_archive_p, _bfd_dummy_target},
314     {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
315        bfd_false},
316     {bfd_false, coff_write_object_contents, /* bfd_write_contents */
317        _bfd_write_archive_contents, bfd_false},
318
319      BFD_JUMP_TABLE_GENERIC (coff),
320      BFD_JUMP_TABLE_COPY (coff),
321      BFD_JUMP_TABLE_CORE (_bfd_nocore),
322      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
323      BFD_JUMP_TABLE_SYMBOLS (coff),
324      BFD_JUMP_TABLE_RELOCS (coff),
325      BFD_JUMP_TABLE_WRITE (coff),
326      BFD_JUMP_TABLE_LINK (coff),
327      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
328
329   COFF_SWAP_TABLE,
330 };