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