* all files: update copyright notices for GPL version 2.
[external/binutils.git] / bfd / coff-a29k.c
1 /* AMD 29000 COFF back-end for BFD.
2    Copyright (C) 1990-1991 Free Software Foundation, Inc.
3    Contributed by David Wood at New York University 7/8/91.
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 /* $Id$ */
22
23 #define A29K 1
24
25 #include <ansidecl.h>
26 #include <sysdep.h>
27 #include "bfd.h"
28 #include "libbfd.h"
29 #include "obstack.h"
30 #include "amdcoff.h"
31 #include "internalcoff.h"
32 #include "libcoff.h"
33
34 #define INSERT_HWORD(WORD,HWORD)        \
35         (((WORD) & 0xff00ff00) | (((HWORD) & 0xff00) << 8) | ((HWORD)& 0xff))
36 #define EXTRACT_HWORD(WORD)     (((WORD) & 0x00ff0000) >> 8) | ((WORD) & 0xff)
37
38 /* Provided the symbol, returns the value reffed */
39 static  long
40 get_symbol_value(symbol)       
41 asymbol *symbol;
42 {                                             
43   long relocation = 0;
44
45   if (symbol != (asymbol *)NULL) {              
46     if (symbol->flags & BSF_FORT_COMM) {        
47       relocation = 0;                           
48     } else {                                      
49       relocation = symbol->value;               
50     }                                           
51     if (symbol->section != (asection *)NULL) {    
52       relocation += symbol->section->output_section->vma +
53                 symbol->section->output_offset;           
54     }                                             
55   }
56   return(relocation);
57 }
58
59 static bfd_reloc_status_enum_type
60 a29k_reloc(abfd, reloc_entry, symbol_in, data, input_section)
61 bfd *abfd;
62 arelent *reloc_entry;
63 asymbol *symbol_in;
64 unsigned char *data;
65 asection *input_section;
66 {
67         static unsigned long part1_consth_active=0;
68         static unsigned long part1_consth_value;
69         unsigned long insn, value, sym_value; 
70         unsigned short r_type;
71 /*      bfd_reloc_status_enum_type result;*/
72 /*      coff_symbol_type *cs = coffsymbol(symbol_in);*/
73         
74         r_type = reloc_entry->howto->type;
75
76         /* FIXME: Do we need to check for partial linking here */
77         if (symbol_in && (symbol_in->flags & BSF_UNDEFINED)) {
78                 /* Keep the state machine happy in case we're called again */
79                 if (r_type == R_IHIHALF) {
80                         part1_consth_active = 1;
81                         part1_consth_value  = 0;
82                 }
83                 return(bfd_reloc_undefined);
84         }
85
86         if ((part1_consth_active) && (r_type != R_IHCONST)) {
87                 fprintf(stderr,"Relocation problem : ");
88                 fprintf(stderr,"Missing IHCONST in module %s\n",abfd->filename);
89                 part1_consth_active = 0;
90                 return(bfd_reloc_dangerous);
91         }
92
93         insn = bfd_get_32(abfd, data + reloc_entry->address); 
94         sym_value = get_symbol_value(symbol_in);
95
96         switch (r_type) {
97         case R_IREL:    
98                 value = EXTRACT_HWORD(insn) << 2;
99                 value +=  sym_value + reloc_entry->addend;
100                 if (value <= 0x3ffff) {         /* Absolute jmp/call */
101                         insn |= 0x01000000;     /* Make it absolute */
102                         /* FIXME: Should we change r_type to R_IABS */
103                 } else {                        /* Relative jmp/call */
104                         value -= reloc_entry->address;
105                         if (value > 0x3ffff) {
106                                 fprintf(stderr,"Relocation problem : ");
107                                 fprintf(stderr,"Jmp/call too far; to %d from %s\n",
108                                         symbol_in->name,abfd->filename); 
109                                 return(bfd_reloc_outofrange);
110                         }
111                 }
112                 value >>= 2;
113                 insn = INSERT_HWORD(insn,value);
114                 break;
115         case R_ILOHALF: 
116                 value = EXTRACT_HWORD(insn);
117                 value +=  sym_value + reloc_entry->addend;
118                 insn = INSERT_HWORD(insn,value);
119                 break;
120         case R_IHIHALF:         /* consth, part 1 */
121                 /* Just get the symbol value that is referenced */
122                 part1_consth_active = 1;
123                 part1_consth_value = sym_value + reloc_entry->addend;
124                 return(bfd_reloc_ok);   /* Don't modify insn until R_IHCONST */
125                 break;
126         case R_IHCONST:         /* consth, part 2 */
127                 /* Now relocate the reference */
128                 if (!part1_consth_active) {
129                         fprintf(stderr,"Relocation problem : ");
130                         fprintf(stderr,"IHIHALF missing in module %s\n",
131                                 abfd->filename); 
132                         part1_consth_active = 0;
133                         return(bfd_reloc_dangerous);
134                 }
135                 /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
136                 value = (unsigned int)reloc_entry->addend; /* r_symndx */
137                 value += part1_consth_value;
138                 value >>= 16;
139                 insn = INSERT_HWORD(insn,value);
140                 part1_consth_active = 0;
141                 break;
142         case R_BYTE:
143                 value = (insn >> 24) + sym_value + reloc_entry->addend; 
144                 if (value & 0xffffff00) {
145                         fprintf(stderr,"Relocation problem : ");
146                         fprintf(stderr,"byte value too large in module %s\n",
147                                 abfd->filename); 
148                         return(bfd_reloc_overflow);
149                 }
150                 insn = (insn & 0x00ffffff) | (value << 24);
151                 break;
152         case R_HWORD:
153                 value = (insn >> 16) + sym_value + reloc_entry->addend; 
154                 if (value & 0xffff0000) {
155                         fprintf(stderr,"Relocation problem : ");
156                         fprintf(stderr,"hword value too large in module %s\n",
157                                 abfd->filename); 
158                         return(bfd_reloc_overflow);
159                 }
160                 insn = (insn & 0x0000ffff) | (value<<16); 
161                 break;
162         case R_WORD:
163                 insn += sym_value + reloc_entry->addend;  
164                 break;
165         default:
166                 fprintf(stderr,"Relocation problem : ");
167                 fprintf(stderr,"Unrecognized reloc type %d, in module %s\n",
168                                 r_type,abfd->filename); 
169                 return (bfd_reloc_dangerous);
170         }
171
172         bfd_put_32(abfd, insn, data+reloc_entry->address);
173         return(bfd_reloc_ok);   
174 }
175
176 /*      type       rightshift
177                        size
178                           bitsize
179                                pc-relative
180                                      bitpos
181                                          absolute
182                                              complain_on_overflow
183                                                   special_function
184                                                     relocation name
185                                                                partial_inplace 
186                                                                       src_mask
187 */
188
189 /*FIXME: I'm not real sure about this table */
190 #define NA      0       /* Obsolete fields, via the documentation */
191 static reloc_howto_type howto_table[] = 
192 {
193   {R_ABS,     0, 3, NA, false, NA, NA, true,a29k_reloc,"ABS",     true, 0xffffffff,0xffffffff, false},
194   {1},  {2},  {3},   {4},  {5},  {6},  {7},  {8},  {9}, {10},
195   {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20},
196   {21}, {22}, {23},
197   {R_IREL,    0, 3, NA, true,  NA, NA, true,a29k_reloc,"IREL",    true, 0xffffffff,0xffffffff, false},
198   {R_IABS,    0, 3, NA, false, NA, NA, true,a29k_reloc,"IABS",    true, 0xffffffff,0xffffffff, false},
199   {R_ILOHALF, 0, 3, NA, true,  NA, NA, true,a29k_reloc,"ILOHALF", true, 0x0000ffff,0x0000ffff, false},
200   {R_IHIHALF, 0, 3, NA, true,  NA, NA, true,a29k_reloc,"IHIHALF", true, 0xffff0000,0xffff0000, false},
201   {R_IHCONST, 0, 3, NA, true,  NA, NA, true,a29k_reloc,"IHCONST", true, 0xffff0000,0xffff0000, false},
202   {R_BYTE,    0, 0, NA, false, NA, NA, true,a29k_reloc,"BYTE",    true, 0x000000ff,0x000000ff, false},
203   {R_HWORD,   0, 1, NA, false, NA, NA, true,a29k_reloc,"HWORD",   true, 0x0000ffff,0x0000ffff, false},
204   {R_WORD,    0, 2, NA, false, NA, NA, true,a29k_reloc,"WORD",    true, 0xffffffff,0xffffffff, false},
205 };
206 #undef NA
207
208 #define BADMAG(x) A29KBADMAG(x)
209
210 #include "coffcode.h"
211
212 bfd_target a29kcoff_big_vec =
213 {
214   "coff-a29k-big",              /* name */
215   bfd_target_coff_flavour_enum,
216   true,                         /* data byte order is big */
217   true,                         /* header byte order is big */
218
219   (HAS_RELOC | EXEC_P |         /* object flags */
220    HAS_LINENO | HAS_DEBUG |
221    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
222
223   (SEC_HAS_CONTENTS | SEC_ALLOC /* section flags */
224         | SEC_LOAD | SEC_RELOC  
225         | SEC_READONLY ),
226   '/',                          /* ar_pad_char */
227   15,                           /* ar_max_namelen */
228
229      3,                         /* minimum section alignment */
230 _do_getb64, _do_putb64,  _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
231 _do_getb64, _do_putb64,  _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
232
233
234   {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
235      bfd_generic_archive_p, _bfd_dummy_target},
236   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
237      bfd_false},
238   {bfd_false, coff_write_object_contents,       /* bfd_write_contents */
239      _bfd_write_archive_contents, bfd_false},
240
241      JUMP_TABLE(coff),
242      COFF_SWAP_TABLE
243   };
244