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