Extensive changes to move the bulk of the linker into BFD so that
[external/binutils.git] / bfd / coff-a29k.c
1 /* BFD back-end for AMD 29000 COFF binaries.
2    Copyright 1990, 1991, 1992, 1993 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 #define A29K 1
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "obstack.h"
27 #include "coff/a29k.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
30
31 static long get_symbol_value PARAMS ((asymbol *));
32 static bfd_reloc_status_type a29k_reloc
33   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34
35 #define INSERT_HWORD(WORD,HWORD)        \
36     (((WORD) & 0xff00ff00) | (((HWORD) & 0xff00) << 8) | ((HWORD)& 0xff))
37 #define EXTRACT_HWORD(WORD) \
38     (((WORD) & 0x00ff0000) >> 8) | ((WORD)& 0xff)
39 #define SIGN_EXTEND_HWORD(HWORD) \
40     ((HWORD) & 0x8000 ? (HWORD)|0xffff0000 : (HWORD))
41
42 /* Provided the symbol, returns the value reffed */
43 static long
44 get_symbol_value (symbol)       
45      asymbol *symbol;
46 {                                             
47   long relocation = 0;
48
49   if (bfd_is_com_section (symbol->section))
50   {
51     relocation = 0;                           
52   }
53   else 
54   {                                      
55     relocation = symbol->value +
56      symbol->section->output_section->vma +
57       symbol->section->output_offset;
58   }                                           
59
60   return(relocation);
61 }
62
63 /* this function is in charge of performing all the 29k relocations */
64
65 static bfd_reloc_status_type
66 a29k_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
67             error_message)
68      bfd *abfd;
69      arelent *reloc_entry;
70      asymbol *symbol_in;
71      PTR data;
72      asection *input_section;
73      bfd *output_bfd;
74      char **error_message;
75 {
76   /* the consth relocation comes in two parts, we have to remember
77      the state between calls, in these variables */
78   static boolean part1_consth_active = false;
79   static unsigned long part1_consth_value;
80
81   unsigned long insn;
82   unsigned long sym_value;
83   unsigned long unsigned_value;
84   unsigned short r_type;
85   long signed_value;
86
87   unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
88   bfd_byte  *hit_data =addr + (bfd_byte *)(data);
89         
90   r_type = reloc_entry->howto->type;
91
92   if (output_bfd) {
93     /* Partial linking - do nothing */
94     reloc_entry->address += input_section->output_offset;
95     return bfd_reloc_ok;
96
97   }
98
99   if (symbol_in && (symbol_in->section == &bfd_und_section))
100   {
101     /* Keep the state machine happy in case we're called again */
102     if (r_type == R_IHIHALF) 
103     {
104       part1_consth_active = true;
105       part1_consth_value  = 0;
106     }
107     return(bfd_reloc_undefined);
108   }
109
110   if ((part1_consth_active) && (r_type != R_IHCONST)) 
111   {
112     part1_consth_active = false;
113     *error_message = (char *) "Missing IHCONST";
114     return(bfd_reloc_dangerous);
115   }
116
117
118   sym_value = get_symbol_value(symbol_in);
119
120   switch (r_type) 
121   {
122    case R_IREL:         
123     insn = bfd_get_32(abfd, hit_data); 
124     /* Take the value in the field and sign extend it */
125     signed_value = EXTRACT_HWORD(insn);
126     signed_value = SIGN_EXTEND_HWORD(signed_value);
127     signed_value <<= 2;
128     signed_value +=  sym_value + reloc_entry->addend;
129     if ((signed_value&~0x3ffff) == 0) 
130     {                           /* Absolute jmp/call */
131       insn |= (1<<24);          /* Make it absolute */
132       /* FIXME: Should we change r_type to R_IABS */
133     } 
134     else 
135     {
136       /* Relative jmp/call, so subtract from the value the
137          address of the place we're coming from */
138       signed_value -= (input_section->output_section->vma
139                        + input_section->output_offset);
140       if (signed_value>0x1ffff || signed_value<-0x20000) 
141        return(bfd_reloc_overflow);
142     }
143     signed_value >>= 2;
144     insn = INSERT_HWORD(insn, signed_value);
145     bfd_put_32(abfd, insn ,hit_data); 
146     break;
147    case R_ILOHALF: 
148     insn = bfd_get_32(abfd, hit_data); 
149     unsigned_value = EXTRACT_HWORD(insn);
150     unsigned_value +=  sym_value + reloc_entry->addend;
151     insn = INSERT_HWORD(insn, unsigned_value);
152     bfd_put_32(abfd, insn, hit_data); 
153     break;
154    case R_IHIHALF:
155     insn = bfd_get_32(abfd, hit_data); 
156     /* consth, part 1 
157        Just get the symbol value that is referenced */
158     part1_consth_active = true;
159     part1_consth_value = sym_value + reloc_entry->addend;
160     /* Don't modify insn until R_IHCONST */
161     break;
162    case R_IHCONST:      
163     insn = bfd_get_32(abfd, hit_data); 
164     /* consth, part 2 
165        Now relocate the reference */
166     if (part1_consth_active == false) {
167       *error_message = (char *) "Missing IHIHALF";
168       return(bfd_reloc_dangerous);
169     }
170     /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
171     unsigned_value = 0;         /*EXTRACT_HWORD(insn) << 16;*/
172     unsigned_value += reloc_entry->addend; /* r_symndx */
173     unsigned_value += part1_consth_value;
174     unsigned_value = unsigned_value >> 16;
175     insn = INSERT_HWORD(insn, unsigned_value);
176     part1_consth_active = false;
177     bfd_put_32(abfd, insn, hit_data); 
178     break;
179    case R_BYTE:
180     insn = bfd_get_8(abfd, hit_data); 
181     unsigned_value = insn + sym_value + reloc_entry->addend;    
182     if (unsigned_value & 0xffffff00) {
183       fprintf(stderr,"Relocation problem : ");
184       fprintf(stderr,"byte value too large in module %s\n",
185               abfd->filename); 
186       return(bfd_reloc_overflow);
187     }
188     bfd_put_8(abfd, unsigned_value, hit_data); 
189     break;
190    case R_HWORD:
191     insn = bfd_get_16(abfd, hit_data); 
192     unsigned_value = insn + sym_value + reloc_entry->addend;    
193     if (unsigned_value & 0xffff0000) {
194       fprintf(stderr,"Relocation problem : ");
195       fprintf(stderr,"hword value too large in module %s\n",
196               abfd->filename); 
197       return(bfd_reloc_overflow);
198     }
199
200     bfd_put_16(abfd, insn, hit_data); 
201     break;
202    case R_WORD:
203     insn = bfd_get_32(abfd, hit_data); 
204     insn += sym_value + reloc_entry->addend;  
205     bfd_put_32(abfd, insn, hit_data);
206     break;
207    default:
208     *error_message = "Unrecognized reloc";
209     return (bfd_reloc_dangerous);
210   }
211
212
213   return(bfd_reloc_ok); 
214 }
215
216 /*      type       rightshift
217                        size
218                           bitsize
219                                pc-relative
220                                      bitpos
221                                          absolute
222                                              complain_on_overflow
223                                                   special_function
224                                                     relocation name
225                                                                partial_inplace 
226                                                                       src_mask
227 */
228
229 /*FIXME: I'm not real sure about this table */
230 static reloc_howto_type howto_table[] = 
231 {
232   {R_ABS,     0, 3, 32, false, 0, complain_overflow_bitfield,a29k_reloc,"ABS",     true, 0xffffffff,0xffffffff, false},
233   {1},  {2},  {3},   {4},  {5},  {6},  {7},  {8},  {9}, {10},
234   {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20},
235   {21}, {22}, {23},
236   {R_IREL,    0, 3, 32, true,  0, complain_overflow_signed,a29k_reloc,"IREL",    true, 0xffffffff,0xffffffff, false},
237   {R_IABS,    0, 3, 32, false, 0, complain_overflow_bitfield, a29k_reloc,"IABS",    true, 0xffffffff,0xffffffff, false},
238   {R_ILOHALF, 0, 3, 16, true,  0, complain_overflow_signed, a29k_reloc,"ILOHALF", true, 0x0000ffff,0x0000ffff, false},
239   {R_IHIHALF, 0, 3, 16, true,  16, complain_overflow_signed, a29k_reloc,"IHIHALF", true, 0xffff0000,0xffff0000, false},
240   {R_IHCONST, 0, 3, 16, true,  0, complain_overflow_signed, a29k_reloc,"IHCONST", true, 0xffff0000,0xffff0000, false},
241   {R_BYTE,    0, 0, 8, false, 0, complain_overflow_bitfield, a29k_reloc,"BYTE",    true, 0x000000ff,0x000000ff, false},
242   {R_HWORD,   0, 1, 16, false, 0, complain_overflow_bitfield, a29k_reloc,"HWORD",   true, 0x0000ffff,0x0000ffff, false},
243   {R_WORD,    0, 2, 32, false, 0, complain_overflow_bitfield, a29k_reloc,"WORD",    true, 0xffffffff,0xffffffff, false},
244 };
245
246 #define BADMAG(x) A29KBADMAG(x)
247
248 #define RELOC_PROCESSING(relent, reloc, symbols, abfd, section) \
249  reloc_processing(relent, reloc, symbols, abfd, section)
250
251 static void DEFUN(reloc_processing,(relent,reloc, symbols, abfd, section) ,
252            arelent *relent AND
253            struct internal_reloc *reloc AND
254            asymbol **symbols AND
255            bfd *abfd AND
256            asection *section)
257 {
258     relent->address = reloc->r_vaddr;           
259     relent->howto = howto_table + reloc->r_type;
260     if (reloc->r_type == R_IHCONST) 
261     {           
262         relent->addend = reloc->r_symndx;               
263         relent->sym_ptr_ptr= bfd_abs_section.symbol_ptr_ptr;
264     }
265     else 
266     {
267       asymbol *ptr;
268       relent->sym_ptr_ptr = symbols + obj_convert(abfd)[reloc->r_symndx];
269
270       ptr = *(relent->sym_ptr_ptr);
271
272       if (ptr 
273           && bfd_asymbol_bfd(ptr) == abfd               
274
275           && ((ptr->flags & BSF_OLD_COMMON)== 0))       
276       {                                         
277           relent->addend = 0;
278       }                                         
279       else
280       {                                 
281           relent->addend = 0;                   
282       }                 
283       relent->address-= section->vma;
284   }
285 }
286
287 #include "coffcode.h"
288
289 bfd_target a29kcoff_big_vec =
290 {
291   "coff-a29k-big",              /* name */
292   bfd_target_coff_flavour,
293   true,                         /* data byte order is big */
294   true,                         /* header byte order is big */
295
296   (HAS_RELOC | EXEC_P |         /* object flags */
297    HAS_LINENO | HAS_DEBUG |
298    HAS_SYMS | HAS_LOCALS | WP_TEXT),
299
300   (SEC_HAS_CONTENTS | SEC_ALLOC /* section flags */
301    | SEC_LOAD | SEC_RELOC  
302    | SEC_READONLY ),
303   '_',                          /* leading underscore */
304   '/',                          /* ar_pad_char */
305   15,                           /* ar_max_namelen */
306   2,                            /* minimum section alignment */
307   /* 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,
311   /* hdrs */
312   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
313      bfd_getb32, bfd_getb_signed_32,   bfd_putb32,
314      bfd_getb16, bfd_getb_signed_16, bfd_putb16,
315
316  {
317             
318    _bfd_dummy_target,
319    coff_object_p,
320    bfd_generic_archive_p,
321    _bfd_dummy_target
322   },
323  {
324    bfd_false,
325    coff_mkobject,
326    _bfd_generic_mkarchive,
327    bfd_false
328   },
329  {
330    bfd_false,
331    coff_write_object_contents,
332    _bfd_write_archive_contents,
333    bfd_false
334   },
335
336   JUMP_TABLE(coff),
337   COFF_SWAP_TABLE
338  };