Initial revision
[binutils.git] / bfd / coff-i960.c
blob25455d9ead8cf4b22e1dd2345ad5b5b757374a25
1 /* BFD back-end for Intel 960 COFF files.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 1997 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #define I960 1
22 #define BADMAG(x) I960BADMAG(x)
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libbfd.h"
27 #include "coff/i960.h"
28 #include "coff/internal.h"
29 #include "libcoff.h" /* to allow easier abstraction-breaking */
31 static boolean coff_i960_is_local_label_name PARAMS ((bfd *, const char *));
32 static bfd_reloc_status_type optcall_callback
33 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34 static bfd_reloc_status_type coff_i960_relocate
35 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
36 static reloc_howto_type *coff_i960_reloc_type_lookup
37 PARAMS ((bfd *, bfd_reloc_code_real_type));
38 static boolean coff_i960_start_final_link
39 PARAMS ((bfd *, struct bfd_link_info *));
40 static boolean coff_i960_relocate_section
41 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
42 struct internal_reloc *, struct internal_syment *, asection **));
43 static boolean coff_i960_adjust_symndx
44 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
45 struct internal_reloc *, boolean *));
47 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
48 #define COFF_ALIGN_IN_SECTION_HEADER 1
50 #define GET_SCNHDR_ALIGN bfd_h_get_32
51 #define PUT_SCNHDR_ALIGN bfd_h_put_32
53 /* The i960 does not support an MMU, so COFF_PAGE_SIZE can be
54 arbitrarily small. */
55 #define COFF_PAGE_SIZE 1
57 #define COFF_LONG_FILENAMES
59 /* This set of local label names is taken from gas. */
61 static boolean
62 coff_i960_is_local_label_name (abfd, name)
63 bfd *abfd;
64 const char *name;
66 return (name[0] == 'L'
67 || (name[0] == '.'
68 && (name[1] == 'C'
69 || name[1] == 'I'
70 || name[1] == '.')));
73 /* This is just like the usual CALC_ADDEND, but it includes the
74 section VMA for PC relative relocs. */
75 #ifndef CALC_ADDEND
76 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
77 { \
78 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
79 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
80 coffsym = (obj_symbols (abfd) \
81 + (cache_ptr->sym_ptr_ptr - symbols)); \
82 else if (ptr) \
83 coffsym = coff_symbol_from (abfd, ptr); \
84 if (coffsym != (coff_symbol_type *) NULL \
85 && coffsym->native->u.syment.n_scnum == 0) \
86 cache_ptr->addend = 0; \
87 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
88 && ptr->section != (asection *) NULL) \
89 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
90 else \
91 cache_ptr->addend = 0; \
92 if (ptr && (reloc.r_type == 25 || reloc.r_type == 27)) \
93 cache_ptr->addend += asect->vma; \
95 #endif
97 #define CALLS 0x66003800 /* Template for 'calls' instruction */
98 #define BAL 0x0b000000 /* Template for 'bal' instruction */
99 #define BAL_MASK 0x00ffffff
101 static bfd_reloc_status_type
102 optcall_callback (abfd, reloc_entry, symbol_in, data,
103 input_section, ignore_bfd, error_message)
104 bfd *abfd;
105 arelent *reloc_entry;
106 asymbol *symbol_in;
107 PTR data;
108 asection *input_section;
109 bfd *ignore_bfd;
110 char **error_message;
112 /* This item has already been relocated correctly, but we may be
113 * able to patch in yet better code - done by digging out the
114 * correct info on this symbol */
115 bfd_reloc_status_type result;
116 coff_symbol_type *cs = coffsymbol(symbol_in);
118 /* Don't do anything with symbols which aren't tied up yet,
119 except move the reloc. */
120 if (bfd_is_und_section (cs->symbol.section)) {
121 reloc_entry->address += input_section->output_offset;
122 return bfd_reloc_ok;
125 /* So the target symbol has to be of coff type, and the symbol
126 has to have the correct native information within it */
127 if ((bfd_asymbol_flavour(&cs->symbol) != bfd_target_coff_flavour)
128 || (cs->native == (combined_entry_type *)NULL))
130 /* This is interesting, consider the case where we're outputting coff
131 from a mix n match input, linking from coff to a symbol defined in a
132 bout file will cause this match to be true. Should I complain? This
133 will only work if the bout symbol is non leaf. */
134 *error_message =
135 (char *) _("uncertain calling convention for non-COFF symbol");
136 result = bfd_reloc_dangerous;
138 else
140 switch (cs->native->u.syment.n_sclass)
142 case C_LEAFSTAT:
143 case C_LEAFEXT:
144 /* This is a call to a leaf procedure, replace instruction with a bal
145 to the correct location. */
147 union internal_auxent *aux = &((cs->native+2)->u.auxent);
148 int word = bfd_get_32(abfd, (bfd_byte *)data + reloc_entry->address);
149 int olf = (aux->x_bal.x_balntry - cs->native->u.syment.n_value);
150 BFD_ASSERT(cs->native->u.syment.n_numaux==2);
152 /* We replace the original call instruction with a bal to
153 the bal entry point - the offset of which is described in
154 the 2nd auxent of the original symbol. We keep the native
155 sym and auxents untouched, so the delta between the two
156 is the offset of the bal entry point. */
157 word = ((word + olf) & BAL_MASK) | BAL;
158 bfd_put_32(abfd, word, (bfd_byte *) data + reloc_entry->address);
160 result = bfd_reloc_ok;
161 break;
162 case C_SCALL:
163 /* This is a call to a system call, replace with a calls to # */
164 BFD_ASSERT(0);
165 result = bfd_reloc_ok;
166 break;
167 default:
168 result = bfd_reloc_ok;
169 break;
172 return result;
175 /* i960 COFF is used by VxWorks 5.1. However, VxWorks 5.1 does not
176 appear to correctly handle a reloc against a symbol defined in the
177 same object file. It appears to simply discard such relocs, rather
178 than adding their values into the object file. We handle this here
179 by converting all relocs against defined symbols into relocs
180 against the section symbol, when generating a relocateable output
181 file.
183 Note that this function is only called if we are not using the COFF
184 specific backend linker. It only does something when doing a
185 relocateable link, which will almost certainly fail when not
186 generating COFF i960 output, so this function is actually no longer
187 useful. It was used before this target was converted to use the
188 COFF specific backend linker. */
190 static bfd_reloc_status_type
191 coff_i960_relocate (abfd, reloc_entry, symbol, data, input_section,
192 output_bfd, error_message)
193 bfd *abfd;
194 arelent *reloc_entry;
195 asymbol *symbol;
196 PTR data;
197 asection *input_section;
198 bfd *output_bfd;
199 char **error_message;
201 asection *osec;
203 if (output_bfd == NULL)
205 /* Not generating relocateable output file. */
206 return bfd_reloc_continue;
209 if (bfd_is_und_section (bfd_get_section (symbol)))
211 /* Symbol is not defined, so no need to worry about it. */
212 return bfd_reloc_continue;
215 if (bfd_is_com_section (bfd_get_section (symbol)))
217 /* I don't really know what the right action is for a common
218 symbol. */
219 return bfd_reloc_continue;
222 /* Convert the reloc to use the section symbol. FIXME: This method
223 is ridiculous. */
224 osec = bfd_get_section (symbol)->output_section;
225 if (coff_section_data (output_bfd, osec) != NULL
226 && coff_section_data (output_bfd, osec)->tdata != NULL)
227 reloc_entry->sym_ptr_ptr =
228 (asymbol **) coff_section_data (output_bfd, osec)->tdata;
229 else
231 const char *sec_name;
232 asymbol **syms, **sym_end;
234 sec_name = bfd_get_section_name (output_bfd, osec);
235 syms = bfd_get_outsymbols (output_bfd);
236 sym_end = syms + bfd_get_symcount (output_bfd);
237 for (; syms < sym_end; syms++)
239 if (bfd_asymbol_name (*syms) != NULL
240 && (*syms)->value == 0
241 && strcmp ((*syms)->section->output_section->name,
242 sec_name) == 0)
243 break;
246 if (syms >= sym_end)
247 abort ();
249 reloc_entry->sym_ptr_ptr = syms;
251 if (coff_section_data (output_bfd, osec) == NULL)
253 osec->used_by_bfd =
254 ((PTR) bfd_zalloc (abfd,
255 sizeof (struct coff_section_tdata)));
256 if (osec->used_by_bfd == NULL)
257 return bfd_reloc_overflow;
259 coff_section_data (output_bfd, osec)->tdata = (PTR) syms;
262 /* Let bfd_perform_relocation do its thing, which will include
263 stuffing the symbol addend into the object file. */
264 return bfd_reloc_continue;
267 static reloc_howto_type howto_rellong =
268 HOWTO ((unsigned int) R_RELLONG, 0, 2, 32,false, 0,
269 complain_overflow_bitfield, coff_i960_relocate,"rellong", true,
270 0xffffffff, 0xffffffff, 0);
271 static reloc_howto_type howto_iprmed =
272 HOWTO (R_IPRMED, 0, 2, 24,true,0, complain_overflow_signed,
273 coff_i960_relocate, "iprmed ", true, 0x00ffffff, 0x00ffffff, 0);
274 static reloc_howto_type howto_optcall =
275 HOWTO (R_OPTCALL, 0,2,24,true,0, complain_overflow_signed,
276 optcall_callback, "optcall", true, 0x00ffffff, 0x00ffffff, 0);
278 static reloc_howto_type *
279 coff_i960_reloc_type_lookup (abfd, code)
280 bfd *abfd;
281 bfd_reloc_code_real_type code;
283 switch (code)
285 default:
286 return 0;
287 case BFD_RELOC_I960_CALLJ:
288 return &howto_optcall;
289 case BFD_RELOC_32:
290 case BFD_RELOC_CTOR:
291 return &howto_rellong;
292 case BFD_RELOC_24_PCREL:
293 return &howto_iprmed;
297 /* The real code is in coffcode.h */
299 #define RTYPE2HOWTO(cache_ptr, dst) \
301 reloc_howto_type *howto_ptr; \
302 switch ((dst)->r_type) { \
303 case 17: howto_ptr = &howto_rellong; break; \
304 case 25: howto_ptr = &howto_iprmed; break; \
305 case 27: howto_ptr = &howto_optcall; break; \
306 default: howto_ptr = 0; break; \
308 (cache_ptr)->howto = howto_ptr; \
311 /* i960 COFF is used by VxWorks 5.1. However, VxWorks 5.1 does not
312 appear to correctly handle a reloc against a symbol defined in the
313 same object file. It appears to simply discard such relocs, rather
314 than adding their values into the object file. We handle this by
315 converting all relocs against global symbols into relocs against
316 internal symbols at the start of the section. This routine is
317 called at the start of the linking process, and it creates the
318 necessary symbols. */
320 static boolean
321 coff_i960_start_final_link (abfd, info)
322 bfd *abfd;
323 struct bfd_link_info *info;
325 bfd_size_type symesz = bfd_coff_symesz (abfd);
326 asection *o;
327 bfd_byte *esym;
329 if (! info->relocateable)
330 return true;
332 esym = (bfd_byte *) bfd_malloc (symesz);
333 if (esym == NULL)
334 return false;
336 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
337 return false;
339 for (o = abfd->sections; o != NULL; o = o->next)
341 struct internal_syment isym;
343 strncpy (isym._n._n_name, o->name, SYMNMLEN);
344 isym.n_value = 0;
345 isym.n_scnum = o->target_index;
346 isym.n_type = T_NULL;
347 isym.n_sclass = C_STAT;
348 isym.n_numaux = 0;
350 bfd_coff_swap_sym_out (abfd, (PTR) &isym, (PTR) esym);
352 if (bfd_write (esym, symesz, 1, abfd) != symesz)
354 free (esym);
355 return false;
358 obj_raw_syment_count (abfd) += 1;
361 free (esym);
363 return true;
366 /* The reloc processing routine for the optimized COFF linker. */
368 static boolean
369 coff_i960_relocate_section (output_bfd, info, input_bfd, input_section,
370 contents, relocs, syms, sections)
371 bfd *output_bfd;
372 struct bfd_link_info *info;
373 bfd *input_bfd;
374 asection *input_section;
375 bfd_byte *contents;
376 struct internal_reloc *relocs;
377 struct internal_syment *syms;
378 asection **sections;
380 struct internal_reloc *rel;
381 struct internal_reloc *relend;
383 rel = relocs;
384 relend = rel + input_section->reloc_count;
385 for (; rel < relend; rel++)
387 long symndx;
388 struct coff_link_hash_entry *h;
389 struct internal_syment *sym;
390 bfd_vma addend;
391 bfd_vma val;
392 reloc_howto_type *howto;
393 bfd_reloc_status_type rstat = bfd_reloc_ok;
394 boolean done;
396 symndx = rel->r_symndx;
398 if (symndx == -1)
400 h = NULL;
401 sym = NULL;
403 else
405 h = obj_coff_sym_hashes (input_bfd)[symndx];
406 sym = syms + symndx;
409 if (sym != NULL && sym->n_scnum != 0)
410 addend = - sym->n_value;
411 else
412 addend = 0;
414 switch (rel->r_type)
416 case 17: howto = &howto_rellong; break;
417 case 25: howto = &howto_iprmed; break;
418 case 27: howto = &howto_optcall; break;
419 default:
420 bfd_set_error (bfd_error_bad_value);
421 return false;
424 val = 0;
426 if (h == NULL)
428 asection *sec;
430 if (symndx == -1)
432 sec = bfd_abs_section_ptr;
433 val = 0;
435 else
437 sec = sections[symndx];
438 val = (sec->output_section->vma
439 + sec->output_offset
440 + sym->n_value
441 - sec->vma);
444 else
446 if (h->root.type == bfd_link_hash_defined
447 || h->root.type == bfd_link_hash_defweak)
449 asection *sec;
451 sec = h->root.u.def.section;
452 val = (h->root.u.def.value
453 + sec->output_section->vma
454 + sec->output_offset);
456 else if (! info->relocateable)
458 if (! ((*info->callbacks->undefined_symbol)
459 (info, h->root.root.string, input_bfd, input_section,
460 rel->r_vaddr - input_section->vma)))
461 return false;
465 done = false;
467 if (howto->type == R_OPTCALL && ! info->relocateable && symndx != -1)
469 int class;
471 if (h != NULL)
472 class = h->class;
473 else
474 class = sym->n_sclass;
476 switch (class)
478 case C_NULL:
479 /* This symbol is apparently not from a COFF input file.
480 We warn, and then assume that it is not a leaf
481 function. */
482 if (! ((*info->callbacks->reloc_dangerous)
483 (info,
484 _("uncertain calling convention for non-COFF symbol"),
485 input_bfd, input_section,
486 rel->r_vaddr - input_section->vma)))
487 return false;
488 break;
489 case C_LEAFSTAT:
490 case C_LEAFEXT:
491 /* This is a call to a leaf procedure; use the bal
492 instruction. */
494 long olf;
495 unsigned long word;
497 if (h != NULL)
499 BFD_ASSERT (h->numaux == 2);
500 olf = h->aux[1].x_bal.x_balntry;
502 else
504 bfd_byte *esyms;
505 union internal_auxent aux;
507 BFD_ASSERT (sym->n_numaux == 2);
508 esyms = (bfd_byte *) obj_coff_external_syms (input_bfd);
509 esyms += (symndx + 2) * bfd_coff_symesz (input_bfd);
510 bfd_coff_swap_aux_in (input_bfd, (PTR) esyms, sym->n_type,
511 sym->n_sclass, 1, sym->n_numaux,
512 (PTR) &aux);
513 olf = aux.x_bal.x_balntry;
516 word = bfd_get_32 (input_bfd,
517 (contents
518 + (rel->r_vaddr - input_section->vma)));
519 word = ((word + olf - val) & BAL_MASK) | BAL;
520 bfd_put_32 (input_bfd,
521 word,
522 (contents
523 + (rel->r_vaddr - input_section->vma)));
524 done = true;
526 break;
527 case C_SCALL:
528 BFD_ASSERT (0);
529 break;
533 if (! done)
535 if (howto->pc_relative)
536 addend += input_section->vma;
537 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
538 contents,
539 rel->r_vaddr - input_section->vma,
540 val, addend);
543 switch (rstat)
545 default:
546 abort ();
547 case bfd_reloc_ok:
548 break;
549 case bfd_reloc_overflow:
551 const char *name;
552 char buf[SYMNMLEN + 1];
554 if (symndx == -1)
555 name = "*ABS*";
556 else if (h != NULL)
557 name = h->root.root.string;
558 else
560 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
561 if (name == NULL)
562 return false;
565 if (! ((*info->callbacks->reloc_overflow)
566 (info, name, howto->name, (bfd_vma) 0, input_bfd,
567 input_section, rel->r_vaddr - input_section->vma)))
568 return false;
573 return true;
576 /* Adjust the symbol index of any reloc against a global symbol to
577 instead be a reloc against the internal symbol we created specially
578 for the section. */
580 /*ARGSUSED*/
581 static boolean
582 coff_i960_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
583 bfd *obfd;
584 struct bfd_link_info *info;
585 bfd *ibfd;
586 asection *sec;
587 struct internal_reloc *irel;
588 boolean *adjustedp;
590 struct coff_link_hash_entry *h;
592 *adjustedp = false;
594 h = obj_coff_sym_hashes (ibfd)[irel->r_symndx];
595 if (h == NULL
596 || (h->root.type != bfd_link_hash_defined
597 && h->root.type != bfd_link_hash_defweak))
598 return true;
600 irel->r_symndx = h->root.u.def.section->output_section->target_index - 1;
601 *adjustedp = true;
603 return true;
606 #define coff_bfd_is_local_label_name coff_i960_is_local_label_name
608 #define coff_start_final_link coff_i960_start_final_link
610 #define coff_relocate_section coff_i960_relocate_section
612 #define coff_adjust_symndx coff_i960_adjust_symndx
614 #define coff_bfd_reloc_type_lookup coff_i960_reloc_type_lookup
616 #include "coffcode.h"
618 const bfd_target icoff_little_vec =
620 "coff-Intel-little", /* name */
621 bfd_target_coff_flavour,
622 BFD_ENDIAN_LITTLE, /* data byte order is little */
623 BFD_ENDIAN_LITTLE, /* header byte order is little */
625 (HAS_RELOC | EXEC_P | /* object flags */
626 HAS_LINENO | HAS_DEBUG |
627 HAS_SYMS | HAS_LOCALS | WP_TEXT),
629 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
630 '_', /* leading underscore */
631 '/', /* ar_pad_char */
632 15, /* ar_max_namelen */
634 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
635 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
636 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
637 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
638 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
639 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
641 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
642 bfd_generic_archive_p, _bfd_dummy_target},
643 {bfd_false, coff_mkobject, /* bfd_set_format */
644 _bfd_generic_mkarchive, bfd_false},
645 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
646 _bfd_write_archive_contents, bfd_false},
648 BFD_JUMP_TABLE_GENERIC (coff),
649 BFD_JUMP_TABLE_COPY (coff),
650 BFD_JUMP_TABLE_CORE (_bfd_nocore),
651 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
652 BFD_JUMP_TABLE_SYMBOLS (coff),
653 BFD_JUMP_TABLE_RELOCS (coff),
654 BFD_JUMP_TABLE_WRITE (coff),
655 BFD_JUMP_TABLE_LINK (coff),
656 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
658 COFF_SWAP_TABLE,
662 const bfd_target icoff_big_vec =
664 "coff-Intel-big", /* name */
665 bfd_target_coff_flavour,
666 BFD_ENDIAN_LITTLE, /* data byte order is little */
667 BFD_ENDIAN_BIG, /* header byte order is big */
669 (HAS_RELOC | EXEC_P | /* object flags */
670 HAS_LINENO | HAS_DEBUG |
671 HAS_SYMS | HAS_LOCALS | WP_TEXT),
673 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
674 '_', /* leading underscore */
675 '/', /* ar_pad_char */
676 15, /* ar_max_namelen */
678 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
679 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
680 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
681 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
682 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
683 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
685 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
686 bfd_generic_archive_p, _bfd_dummy_target},
687 {bfd_false, coff_mkobject, /* bfd_set_format */
688 _bfd_generic_mkarchive, bfd_false},
689 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
690 _bfd_write_archive_contents, bfd_false},
692 BFD_JUMP_TABLE_GENERIC (coff),
693 BFD_JUMP_TABLE_COPY (coff),
694 BFD_JUMP_TABLE_CORE (_bfd_nocore),
695 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
696 BFD_JUMP_TABLE_SYMBOLS (coff),
697 BFD_JUMP_TABLE_RELOCS (coff),
698 BFD_JUMP_TABLE_WRITE (coff),
699 BFD_JUMP_TABLE_LINK (coff),
700 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
702 COFF_SWAP_TABLE,