* coffcode.h (bfd_coff_backend_data): Add new field
[external/binutils.git] / bfd / coff-a29k.c
1 /* BFD back-end for AMD 29000 COFF binaries.
2    Copyright 1990, 1991, 1992, 1993, 1994 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 static boolean coff_a29k_relocate_section
35   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
36            struct internal_reloc *, struct internal_syment *, asection **));
37 static boolean coff_a29k_adjust_symndx
38   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
39            struct internal_reloc *, boolean *));
40
41 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
42
43 #define INSERT_HWORD(WORD,HWORD)        \
44     (((WORD) & 0xff00ff00) | (((HWORD) & 0xff00) << 8) | ((HWORD)& 0xff))
45 #define EXTRACT_HWORD(WORD) \
46     (((WORD) & 0x00ff0000) >> 8) | ((WORD)& 0xff)
47 #define SIGN_EXTEND_HWORD(HWORD) \
48     ((HWORD) & 0x8000 ? (HWORD)|0xffff0000 : (HWORD))
49
50 /* Provided the symbol, returns the value reffed */
51 static long
52 get_symbol_value (symbol)       
53      asymbol *symbol;
54 {                                             
55   long relocation = 0;
56
57   if (bfd_is_com_section (symbol->section))
58   {
59     relocation = 0;                           
60   }
61   else 
62   {                                      
63     relocation = symbol->value +
64      symbol->section->output_section->vma +
65       symbol->section->output_offset;
66   }                                           
67
68   return(relocation);
69 }
70
71 /* this function is in charge of performing all the 29k relocations */
72
73 static bfd_reloc_status_type
74 a29k_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
75             error_message)
76      bfd *abfd;
77      arelent *reloc_entry;
78      asymbol *symbol_in;
79      PTR data;
80      asection *input_section;
81      bfd *output_bfd;
82      char **error_message;
83 {
84   /* the consth relocation comes in two parts, we have to remember
85      the state between calls, in these variables */
86   static boolean part1_consth_active = false;
87   static unsigned long part1_consth_value;
88
89   unsigned long insn;
90   unsigned long sym_value;
91   unsigned long unsigned_value;
92   unsigned short r_type;
93   long signed_value;
94
95   unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
96   bfd_byte  *hit_data =addr + (bfd_byte *)(data);
97         
98   r_type = reloc_entry->howto->type;
99
100   if (output_bfd) {
101     /* Partial linking - do nothing */
102     reloc_entry->address += input_section->output_offset;
103     return bfd_reloc_ok;
104
105   }
106
107   if (symbol_in != NULL
108       && bfd_is_und_section (symbol_in->section))
109   {
110     /* Keep the state machine happy in case we're called again */
111     if (r_type == R_IHIHALF) 
112     {
113       part1_consth_active = true;
114       part1_consth_value  = 0;
115     }
116     return(bfd_reloc_undefined);
117   }
118
119   if ((part1_consth_active) && (r_type != R_IHCONST)) 
120   {
121     part1_consth_active = false;
122     *error_message = (char *) "Missing IHCONST";
123     return(bfd_reloc_dangerous);
124   }
125
126
127   sym_value = get_symbol_value(symbol_in);
128
129   switch (r_type) 
130   {
131    case R_IREL:         
132     insn = bfd_get_32(abfd, hit_data); 
133     /* Take the value in the field and sign extend it */
134     signed_value = EXTRACT_HWORD(insn);
135     signed_value = SIGN_EXTEND_HWORD(signed_value);
136     signed_value <<= 2;
137     signed_value +=  sym_value + reloc_entry->addend;
138     if (((signed_value + reloc_entry->address) & ~0x3ffff) == 0)
139     {                           /* Absolute jmp/call */
140       insn |= (1<<24);          /* Make it absolute */
141       signed_value += reloc_entry->address;
142       /* FIXME: Should we change r_type to R_IABS */
143     } 
144     else 
145     {
146       /* Relative jmp/call, so subtract from the value the
147          address of the place we're coming from */
148       signed_value -= (input_section->output_section->vma
149                        + input_section->output_offset);
150       if (signed_value>0x1ffff || signed_value<-0x20000) 
151        return(bfd_reloc_overflow);
152     }
153     signed_value >>= 2;
154     insn = INSERT_HWORD(insn, signed_value);
155     bfd_put_32(abfd, insn ,hit_data); 
156     break;
157    case R_ILOHALF: 
158     insn = bfd_get_32(abfd, hit_data); 
159     unsigned_value = EXTRACT_HWORD(insn);
160     unsigned_value +=  sym_value + reloc_entry->addend;
161     insn = INSERT_HWORD(insn, unsigned_value);
162     bfd_put_32(abfd, insn, hit_data); 
163     break;
164    case R_IHIHALF:
165     insn = bfd_get_32(abfd, hit_data); 
166     /* consth, part 1 
167        Just get the symbol value that is referenced */
168     part1_consth_active = true;
169     part1_consth_value = sym_value + reloc_entry->addend;
170     /* Don't modify insn until R_IHCONST */
171     break;
172    case R_IHCONST:      
173     insn = bfd_get_32(abfd, hit_data); 
174     /* consth, part 2 
175        Now relocate the reference */
176     if (part1_consth_active == false) {
177       *error_message = (char *) "Missing IHIHALF";
178       return(bfd_reloc_dangerous);
179     }
180     /* sym_ptr_ptr = r_symndx, in coff_slurp_reloc_table() */
181     unsigned_value = 0;         /*EXTRACT_HWORD(insn) << 16;*/
182     unsigned_value += reloc_entry->addend; /* r_symndx */
183     unsigned_value += part1_consth_value;
184     unsigned_value = unsigned_value >> 16;
185     insn = INSERT_HWORD(insn, unsigned_value);
186     part1_consth_active = false;
187     bfd_put_32(abfd, insn, hit_data); 
188     break;
189    case R_BYTE:
190     insn = bfd_get_8(abfd, hit_data); 
191     unsigned_value = insn + sym_value + reloc_entry->addend;    
192     if (unsigned_value & 0xffffff00) {
193       fprintf(stderr,"Relocation problem : ");
194       fprintf(stderr,"byte value too large in module %s\n",
195               abfd->filename); 
196       return(bfd_reloc_overflow);
197     }
198     bfd_put_8(abfd, unsigned_value, hit_data); 
199     break;
200    case R_HWORD:
201     insn = bfd_get_16(abfd, hit_data); 
202     unsigned_value = insn + sym_value + reloc_entry->addend;    
203     if (unsigned_value & 0xffff0000) {
204       fprintf(stderr,"Relocation problem : ");
205       fprintf(stderr,"hword value too large in module %s\n",
206               abfd->filename); 
207       return(bfd_reloc_overflow);
208     }
209
210     bfd_put_16(abfd, insn, hit_data); 
211     break;
212    case R_WORD:
213     insn = bfd_get_32(abfd, hit_data); 
214     insn += sym_value + reloc_entry->addend;  
215     bfd_put_32(abfd, insn, hit_data);
216     break;
217    default:
218     *error_message = "Unrecognized reloc";
219     return (bfd_reloc_dangerous);
220   }
221
222
223   return(bfd_reloc_ok); 
224 }
225
226 /*      type       rightshift
227                        size
228                           bitsize
229                                pc-relative
230                                      bitpos
231                                          absolute
232                                              complain_on_overflow
233                                                   special_function
234                                                     relocation name
235                                                                partial_inplace 
236                                                                       src_mask
237 */
238
239 /*FIXME: I'm not real sure about this table */
240 static reloc_howto_type howto_table[] = 
241 {
242   {R_ABS,     0, 3, 32, false, 0, complain_overflow_bitfield,a29k_reloc,"ABS",     true, 0xffffffff,0xffffffff, false},
243   {1},  {2},  {3},   {4},  {5},  {6},  {7},  {8},  {9}, {10},
244   {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20},
245   {21}, {22}, {23},
246   {R_IREL,    0, 3, 32, true,  0, complain_overflow_signed,a29k_reloc,"IREL",    true, 0xffffffff,0xffffffff, false},
247   {R_IABS,    0, 3, 32, false, 0, complain_overflow_bitfield, a29k_reloc,"IABS",    true, 0xffffffff,0xffffffff, false},
248   {R_ILOHALF, 0, 3, 16, true,  0, complain_overflow_signed, a29k_reloc,"ILOHALF", true, 0x0000ffff,0x0000ffff, false},
249   {R_IHIHALF, 0, 3, 16, true,  16, complain_overflow_signed, a29k_reloc,"IHIHALF", true, 0xffff0000,0xffff0000, false},
250   {R_IHCONST, 0, 3, 16, true,  0, complain_overflow_signed, a29k_reloc,"IHCONST", true, 0xffff0000,0xffff0000, false},
251   {R_BYTE,    0, 0, 8, false, 0, complain_overflow_bitfield, a29k_reloc,"BYTE",    true, 0x000000ff,0x000000ff, false},
252   {R_HWORD,   0, 1, 16, false, 0, complain_overflow_bitfield, a29k_reloc,"HWORD",   true, 0x0000ffff,0x0000ffff, false},
253   {R_WORD,    0, 2, 32, false, 0, complain_overflow_bitfield, a29k_reloc,"WORD",    true, 0xffffffff,0xffffffff, false},
254 };
255
256 #define BADMAG(x) A29KBADMAG(x)
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     static bfd_vma ihihalf_vaddr = (bfd_vma) -1;
270
271     relent->address = reloc->r_vaddr;           
272     relent->howto = howto_table + reloc->r_type;
273     if (reloc->r_type == R_IHCONST) 
274     {           
275       /* The address of an R_IHCONST should always be the address of
276          the immediately preceding R_IHIHALF.  relocs generated by gas
277          are correct, but relocs generated by High C are different (I
278          can't figure out what the address means for High C).  We can
279          handle both gas and High C by ignoring the address here, and
280          simply reusing the address saved for R_IHIHALF.  */
281         if (ihihalf_vaddr == (bfd_vma) -1)
282           abort ();
283         relent->address = ihihalf_vaddr;
284         ihihalf_vaddr = (bfd_vma) -1;
285         relent->addend = reloc->r_symndx;               
286         relent->sym_ptr_ptr= bfd_abs_section_ptr->symbol_ptr_ptr;
287     }
288     else 
289     {
290       asymbol *ptr;
291       relent->sym_ptr_ptr = symbols + obj_convert(abfd)[reloc->r_symndx];
292
293       ptr = *(relent->sym_ptr_ptr);
294
295       if (ptr 
296           && bfd_asymbol_bfd(ptr) == abfd               
297
298           && ((ptr->flags & BSF_OLD_COMMON)== 0))       
299       {                                         
300           relent->addend = 0;
301       }                                         
302       else
303       {                                 
304           relent->addend = 0;                   
305       }                 
306       relent->address-= section->vma;
307       if (reloc->r_type == R_IHIHALF)
308         ihihalf_vaddr = relent->address;
309       else if (ihihalf_vaddr != (bfd_vma) -1)
310         abort ();
311   }
312 }
313
314 /* The reloc processing routine for the optimized COFF linker.  */
315
316 static boolean
317 coff_a29k_relocate_section (output_bfd, info, input_bfd, input_section,
318                             contents, relocs, syms, sections)
319      bfd *output_bfd;
320      struct bfd_link_info *info;
321      bfd *input_bfd;
322      asection *input_section;
323      bfd_byte *contents;
324      struct internal_reloc *relocs;
325      struct internal_syment *syms;
326      asection **sections;
327 {
328   struct internal_reloc *rel;
329   struct internal_reloc *relend;
330   boolean hihalf;
331   bfd_vma hihalf_val;
332
333   /* If we are performing a relocateable link, we don't need to do a
334      thing.  The caller will take care of adjusting the reloc
335      addresses and symbol indices.  */
336   if (info->relocateable)
337     return true;
338
339   hihalf = false;
340   hihalf_val = 0;
341
342   rel = relocs;
343   relend = rel + input_section->reloc_count;
344   for (; rel < relend; rel++)
345     {
346       long symndx;
347       bfd_byte *loc;
348       struct coff_link_hash_entry *h;
349       struct internal_syment *sym;
350       asection *sec;
351       bfd_vma val;
352       boolean overflow;
353       unsigned long insn;
354       long signed_value;
355       unsigned long unsigned_value;
356       bfd_reloc_status_type rstat;
357
358       symndx = rel->r_symndx;
359       loc = contents + rel->r_vaddr - input_section->vma;
360
361       if (symndx == -1)
362         h = NULL;
363       else
364         h = obj_coff_sym_hashes (input_bfd)[symndx];
365
366       sym = NULL;
367       sec = NULL;
368       val = 0;
369
370       /* An R_IHCONST reloc does not have a symbol.  Instead, the
371          symbol index is an addend.  R_IHCONST is always used in
372          conjunction with R_IHHALF.  */
373       if (rel->r_type != R_IHCONST)
374         {
375           if (h == NULL)
376             {
377               if (symndx == -1)
378                 sec = bfd_abs_section_ptr;
379               else
380                 {
381                   sym = syms + symndx;
382                   sec = sections[symndx];
383                   val = (sec->output_section->vma
384                          + sec->output_offset
385                          + sym->n_value
386                          - sec->vma);
387                 }
388             }
389           else
390             {
391               if (h->root.type == bfd_link_hash_defined
392                   || h->root.type == bfd_link_hash_defweak)
393                 {
394                   sec = h->root.u.def.section;
395                   val = (h->root.u.def.value
396                          + sec->output_section->vma
397                          + sec->output_offset);
398                 }
399               else
400                 {
401                   if (! ((*info->callbacks->undefined_symbol)
402                          (info, h->root.root.string, input_bfd, input_section,
403                           rel->r_vaddr - input_section->vma)))
404                     return false;
405                 }
406             }
407
408           if (hihalf)
409             {
410               if (! ((*info->callbacks->reloc_dangerous)
411                      (info, "missing IHCONST reloc", input_bfd,
412                       input_section, rel->r_vaddr - input_section->vma)))
413                 return false;
414               hihalf = false;
415             }
416         }
417
418       overflow = false;
419
420       switch (rel->r_type)
421         {
422         default:
423           bfd_set_error (bfd_error_bad_value);
424           return false;
425
426         case R_IREL:
427           insn = bfd_get_32 (input_bfd, loc);
428
429           /* Extract the addend.  */
430           signed_value = EXTRACT_HWORD (insn);
431           signed_value = SIGN_EXTEND_HWORD (signed_value);
432           signed_value <<= 2;
433
434           /* Determine the destination of the jump.  */
435           signed_value += val + rel->r_vaddr - input_section->vma;
436
437           if ((signed_value & ~0x3ffff) == 0)
438             {
439               /* We can use an absolute jump.  */
440               insn |= (1 << 24);
441             }
442           else
443             {
444               /* Make the destination PC relative.  */
445               signed_value -= (input_section->output_section->vma
446                                + input_section->output_offset
447                                + (rel->r_vaddr - input_section->vma));
448               if (signed_value > 0x1ffff || signed_value < - 0x20000)
449                 {
450                   overflow = true;
451                   signed_value = 0;
452                 }
453             }
454
455           /* Put the adjusted value back into the instruction.  */
456           signed_value >>= 2;
457           insn = INSERT_HWORD (insn, signed_value);
458
459           bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
460
461           break;
462
463         case R_ILOHALF:
464           insn = bfd_get_32 (input_bfd, loc);
465           unsigned_value = EXTRACT_HWORD (insn);
466           unsigned_value += val;
467           insn = INSERT_HWORD (insn, unsigned_value);
468           bfd_put_32 (input_bfd, insn, loc);
469           break;
470
471         case R_IHIHALF:
472           /* Save the value for the R_IHCONST reloc.  */
473           hihalf = true;
474           hihalf_val = val;
475           break;
476
477         case R_IHCONST:
478           if (! hihalf)
479             {
480               if (! ((*info->callbacks->reloc_dangerous)
481                      (info, "missing IHIHALF reloc", input_bfd,
482                       input_section, rel->r_vaddr - input_section->vma)))
483                 return false;
484               hihalf_val = 0;
485             }
486
487           insn = bfd_get_32 (input_bfd, loc);
488           unsigned_value = rel->r_symndx + hihalf_val;
489           unsigned_value >>= 16;
490           insn = INSERT_HWORD (insn, unsigned_value);
491           bfd_put_32 (input_bfd, (bfd_vma) insn, loc);
492
493           hihalf = false;
494
495           break;
496
497         case R_BYTE:
498         case R_HWORD:
499         case R_WORD:
500           rstat = _bfd_relocate_contents (howto_table + rel->r_type,
501                                           input_bfd, val, loc);
502           if (rstat == bfd_reloc_overflow)
503             overflow = true;
504           else if (rstat != bfd_reloc_ok)
505             abort ();
506           break;
507         }
508
509       if (overflow)
510         {
511           const char *name;
512           char buf[SYMNMLEN + 1];
513
514           if (symndx == -1)
515             name = "*ABS*";
516           else if (h != NULL)
517             name = h->root.root.string;
518           else if (sym == NULL)
519             name = "*unknown*";
520           else if (sym->_n._n_n._n_zeroes == 0
521                    && sym->_n._n_n._n_offset != 0)
522             name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
523           else
524             {
525               strncpy (buf, sym->_n._n_name, SYMNMLEN);
526               buf[SYMNMLEN] = '\0';
527               name = buf;
528             }
529
530           if (! ((*info->callbacks->reloc_overflow)
531                  (info, name, howto_table[rel->r_type].name, (bfd_vma) 0,
532                   input_bfd, input_section,
533                   rel->r_vaddr - input_section->vma)))
534             return false;
535         }
536     }     
537
538   return true;
539 }
540
541 #define coff_relocate_section coff_a29k_relocate_section
542
543 /* We don't want to change the symndx of a R_IHCONST reloc, since it
544    is actually an addend, not a symbol index at all.  */
545
546 /*ARGSUSED*/
547 static boolean
548 coff_a29k_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
549      bfd *obfd;
550      struct bfd_link_info *info;
551      bfd *ibfd;
552      asection *sec;
553      struct internal_reloc *irel;
554      boolean *adjustedp;
555 {
556   if (irel->r_type == R_IHCONST)
557     *adjustedp = true;
558   else
559     *adjustedp = false;
560   return true;
561 }
562
563 #define coff_adjust_symndx coff_a29k_adjust_symndx
564
565 #include "coffcode.h"
566
567 const bfd_target a29kcoff_big_vec =
568 {
569   "coff-a29k-big",              /* name */
570   bfd_target_coff_flavour,
571   true,                         /* data byte order is big */
572   true,                         /* header byte order is big */
573
574   (HAS_RELOC | EXEC_P |         /* object flags */
575    HAS_LINENO | HAS_DEBUG |
576    HAS_SYMS | HAS_LOCALS | WP_TEXT),
577
578   (SEC_HAS_CONTENTS | SEC_ALLOC /* section flags */
579    | SEC_LOAD | SEC_RELOC  
580    | SEC_READONLY ),
581   '_',                          /* leading underscore */
582   '/',                          /* ar_pad_char */
583   15,                           /* ar_max_namelen */
584   2,                            /* minimum section alignment */
585   /* data */
586   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
587      bfd_getb32, bfd_getb_signed_32,   bfd_putb32,
588      bfd_getb16, bfd_getb_signed_16, bfd_putb16,
589   /* hdrs */
590   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
591      bfd_getb32, bfd_getb_signed_32,   bfd_putb32,
592      bfd_getb16, bfd_getb_signed_16, bfd_putb16,
593
594  {
595             
596    _bfd_dummy_target,
597    coff_object_p,
598    bfd_generic_archive_p,
599    _bfd_dummy_target
600   },
601  {
602    bfd_false,
603    coff_mkobject,
604    _bfd_generic_mkarchive,
605    bfd_false
606   },
607  {
608    bfd_false,
609    coff_write_object_contents,
610    _bfd_write_archive_contents,
611    bfd_false
612   },
613
614      BFD_JUMP_TABLE_GENERIC (coff),
615      BFD_JUMP_TABLE_COPY (coff),
616      BFD_JUMP_TABLE_CORE (_bfd_nocore),
617      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
618      BFD_JUMP_TABLE_SYMBOLS (coff),
619      BFD_JUMP_TABLE_RELOCS (coff),
620      BFD_JUMP_TABLE_WRITE (coff),
621      BFD_JUMP_TABLE_LINK (coff),
622      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
623
624   COFF_SWAP_TABLE
625  };