add pr number
[binutils.git] / bfd / coff-i960.c
blobdc74a390c69ba878a3b3b38e0a2001ad183e837f
1 /* BFD back-end for Intel 960 COFF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1997, 1999, 2000, 2001,
3 2002, 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #define I960 1
24 #define BADMAG(x) I960BADMAG(x)
26 #include "sysdep.h"
27 #include "bfd.h"
28 #include "libbfd.h"
29 #include "coff/i960.h"
30 #include "coff/internal.h"
32 #ifndef bfd_pe_print_pdata
33 #define bfd_pe_print_pdata NULL
34 #endif
36 #include "libcoff.h" /* To allow easier abstraction-breaking. */
38 static bfd_boolean coff_i960_is_local_label_name
39 PARAMS ((bfd *, const char *));
40 static bfd_reloc_status_type optcall_callback
41 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
42 static bfd_reloc_status_type coff_i960_relocate
43 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
44 static reloc_howto_type *coff_i960_reloc_type_lookup
45 PARAMS ((bfd *, bfd_reloc_code_real_type));
46 static bfd_boolean coff_i960_start_final_link
47 PARAMS ((bfd *, struct bfd_link_info *));
48 static bfd_boolean coff_i960_relocate_section
49 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
50 struct internal_reloc *, struct internal_syment *, asection **));
51 static bfd_boolean coff_i960_adjust_symndx
52 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
53 struct internal_reloc *, bfd_boolean *));
55 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
56 #define COFF_ALIGN_IN_SECTION_HEADER 1
58 #define GET_SCNHDR_ALIGN H_GET_32
59 #define PUT_SCNHDR_ALIGN H_PUT_32
61 /* The i960 does not support an MMU, so COFF_PAGE_SIZE can be
62 arbitrarily small. */
63 #define COFF_PAGE_SIZE 1
65 #define COFF_LONG_FILENAMES
67 /* This set of local label names is taken from gas. */
69 static bfd_boolean
70 coff_i960_is_local_label_name (abfd, name)
71 bfd *abfd ATTRIBUTE_UNUSED;
72 const char *name;
74 return (name[0] == 'L'
75 || (name[0] == '.'
76 && (name[1] == 'C'
77 || name[1] == 'I'
78 || name[1] == '.')));
81 /* This is just like the usual CALC_ADDEND, but it includes the
82 section VMA for PC relative relocs. */
83 #ifndef CALC_ADDEND
84 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
85 { \
86 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
87 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
88 coffsym = (obj_symbols (abfd) \
89 + (cache_ptr->sym_ptr_ptr - symbols)); \
90 else if (ptr) \
91 coffsym = coff_symbol_from (abfd, ptr); \
92 if (coffsym != (coff_symbol_type *) NULL \
93 && coffsym->native->u.syment.n_scnum == 0) \
94 cache_ptr->addend = 0; \
95 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
96 && ptr->section != (asection *) NULL) \
97 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
98 else \
99 cache_ptr->addend = 0; \
100 if (ptr && (reloc.r_type == 25 || reloc.r_type == 27)) \
101 cache_ptr->addend += asect->vma; \
103 #endif
105 #define CALLS 0x66003800 /* Template for 'calls' instruction */
106 #define BAL 0x0b000000 /* Template for 'bal' instruction */
107 #define BAL_MASK 0x00ffffff
109 static bfd_reloc_status_type
110 optcall_callback (abfd, reloc_entry, symbol_in, data,
111 input_section, ignore_bfd, error_message)
112 bfd *abfd;
113 arelent *reloc_entry;
114 asymbol *symbol_in;
115 PTR data;
116 asection *input_section;
117 bfd *ignore_bfd ATTRIBUTE_UNUSED;
118 char **error_message;
120 /* This item has already been relocated correctly, but we may be
121 * able to patch in yet better code - done by digging out the
122 * correct info on this symbol */
123 bfd_reloc_status_type result;
124 coff_symbol_type *cs = coffsymbol(symbol_in);
126 /* Don't do anything with symbols which aren't tied up yet,
127 except move the reloc. */
128 if (bfd_is_und_section (cs->symbol.section)) {
129 reloc_entry->address += input_section->output_offset;
130 return bfd_reloc_ok;
133 /* So the target symbol has to be of coff type, and the symbol
134 has to have the correct native information within it */
135 if ((bfd_asymbol_flavour(&cs->symbol) != bfd_target_coff_flavour)
136 || (cs->native == (combined_entry_type *)NULL))
138 /* This is interesting, consider the case where we're outputting coff
139 from a mix n match input, linking from coff to a symbol defined in a
140 bout file will cause this match to be true. Should I complain? This
141 will only work if the bout symbol is non leaf. */
142 *error_message =
143 (char *) _("uncertain calling convention for non-COFF symbol");
144 result = bfd_reloc_dangerous;
146 else
148 switch (cs->native->u.syment.n_sclass)
150 case C_LEAFSTAT:
151 case C_LEAFEXT:
152 /* This is a call to a leaf procedure, replace instruction with a bal
153 to the correct location. */
155 union internal_auxent *aux = &((cs->native+2)->u.auxent);
156 int word = bfd_get_32 (abfd, (bfd_byte *)data + reloc_entry->address);
157 int olf = (aux->x_bal.x_balntry - cs->native->u.syment.n_value);
158 BFD_ASSERT(cs->native->u.syment.n_numaux==2);
160 /* We replace the original call instruction with a bal to
161 the bal entry point - the offset of which is described in
162 the 2nd auxent of the original symbol. We keep the native
163 sym and auxents untouched, so the delta between the two
164 is the offset of the bal entry point. */
165 word = ((word + olf) & BAL_MASK) | BAL;
166 bfd_put_32 (abfd, (bfd_vma) word,
167 (bfd_byte *) data + reloc_entry->address);
169 result = bfd_reloc_ok;
170 break;
171 case C_SCALL:
172 /* This is a call to a system call, replace with a calls to # */
173 BFD_ASSERT(0);
174 result = bfd_reloc_ok;
175 break;
176 default:
177 result = bfd_reloc_ok;
178 break;
181 return result;
184 /* i960 COFF is used by VxWorks 5.1. However, VxWorks 5.1 does not
185 appear to correctly handle a reloc against a symbol defined in the
186 same object file. It appears to simply discard such relocs, rather
187 than adding their values into the object file. We handle this here
188 by converting all relocs against defined symbols into relocs
189 against the section symbol, when generating a relocatable output
190 file.
192 Note that this function is only called if we are not using the COFF
193 specific backend linker. It only does something when doing a
194 relocatable link, which will almost certainly fail when not
195 generating COFF i960 output, so this function is actually no longer
196 useful. It was used before this target was converted to use the
197 COFF specific backend linker. */
199 static bfd_reloc_status_type
200 coff_i960_relocate (abfd, reloc_entry, symbol, data, input_section,
201 output_bfd, error_message)
202 bfd *abfd;
203 arelent *reloc_entry;
204 asymbol *symbol;
205 PTR data ATTRIBUTE_UNUSED;
206 asection *input_section ATTRIBUTE_UNUSED;
207 bfd *output_bfd;
208 char **error_message ATTRIBUTE_UNUSED;
210 asection *osec;
212 if (output_bfd == NULL)
214 /* Not generating relocatable output file. */
215 return bfd_reloc_continue;
218 if (bfd_is_und_section (bfd_get_section (symbol)))
220 /* Symbol is not defined, so no need to worry about it. */
221 return bfd_reloc_continue;
224 if (bfd_is_com_section (bfd_get_section (symbol)))
226 /* I don't really know what the right action is for a common
227 symbol. */
228 return bfd_reloc_continue;
231 /* Convert the reloc to use the section symbol. FIXME: This method
232 is ridiculous. */
233 osec = bfd_get_section (symbol)->output_section;
234 if (coff_section_data (output_bfd, osec) != NULL
235 && coff_section_data (output_bfd, osec)->tdata != NULL)
236 reloc_entry->sym_ptr_ptr =
237 (asymbol **) coff_section_data (output_bfd, osec)->tdata;
238 else
240 const char *sec_name;
241 asymbol **syms, **sym_end;
243 sec_name = bfd_get_section_name (output_bfd, osec);
244 syms = bfd_get_outsymbols (output_bfd);
245 sym_end = syms + bfd_get_symcount (output_bfd);
246 for (; syms < sym_end; syms++)
248 if (bfd_asymbol_name (*syms) != NULL
249 && (*syms)->value == 0
250 && strcmp ((*syms)->section->output_section->name,
251 sec_name) == 0)
252 break;
255 if (syms >= sym_end)
256 abort ();
258 reloc_entry->sym_ptr_ptr = syms;
260 if (coff_section_data (output_bfd, osec) == NULL)
262 bfd_size_type amt = sizeof (struct coff_section_tdata);
263 osec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
264 if (osec->used_by_bfd == NULL)
265 return bfd_reloc_overflow;
267 coff_section_data (output_bfd, osec)->tdata = (PTR) syms;
270 /* Let bfd_perform_relocation do its thing, which will include
271 stuffing the symbol addend into the object file. */
272 return bfd_reloc_continue;
275 static reloc_howto_type howto_rellong =
276 HOWTO ((unsigned int) R_RELLONG, 0, 2, 32,FALSE, 0,
277 complain_overflow_bitfield, coff_i960_relocate,"rellong", TRUE,
278 0xffffffff, 0xffffffff, 0);
279 static reloc_howto_type howto_iprmed =
280 HOWTO (R_IPRMED, 0, 2, 24,TRUE,0, complain_overflow_signed,
281 coff_i960_relocate, "iprmed ", TRUE, 0x00ffffff, 0x00ffffff, 0);
282 static reloc_howto_type howto_optcall =
283 HOWTO (R_OPTCALL, 0,2,24,TRUE,0, complain_overflow_signed,
284 optcall_callback, "optcall", TRUE, 0x00ffffff, 0x00ffffff, 0);
286 static reloc_howto_type *
287 coff_i960_reloc_type_lookup (abfd, code)
288 bfd *abfd ATTRIBUTE_UNUSED;
289 bfd_reloc_code_real_type code;
291 switch (code)
293 default:
294 return 0;
295 case BFD_RELOC_I960_CALLJ:
296 return &howto_optcall;
297 case BFD_RELOC_32:
298 case BFD_RELOC_CTOR:
299 return &howto_rellong;
300 case BFD_RELOC_24_PCREL:
301 return &howto_iprmed;
305 static reloc_howto_type *
306 coff_i960_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
307 const char *r_name)
309 if (strcasecmp (howto_optcall.name, r_name) == 0)
310 return &howto_optcall;
311 if (strcasecmp (howto_rellong.name, r_name) == 0)
312 return &howto_rellong;
313 if (strcasecmp (howto_iprmed.name, r_name) == 0)
314 return &howto_iprmed;
316 return NULL;
319 /* The real code is in coffcode.h */
321 #define RTYPE2HOWTO(cache_ptr, dst) \
323 reloc_howto_type *howto_ptr; \
324 switch ((dst)->r_type) { \
325 case 17: howto_ptr = &howto_rellong; break; \
326 case 25: howto_ptr = &howto_iprmed; break; \
327 case 27: howto_ptr = &howto_optcall; break; \
328 default: howto_ptr = 0; break; \
330 (cache_ptr)->howto = howto_ptr; \
333 /* i960 COFF is used by VxWorks 5.1. However, VxWorks 5.1 does not
334 appear to correctly handle a reloc against a symbol defined in the
335 same object file. It appears to simply discard such relocs, rather
336 than adding their values into the object file. We handle this by
337 converting all relocs against global symbols into relocs against
338 internal symbols at the start of the section. This routine is
339 called at the start of the linking process, and it creates the
340 necessary symbols. */
342 static bfd_boolean
343 coff_i960_start_final_link (abfd, info)
344 bfd *abfd;
345 struct bfd_link_info *info;
347 bfd_size_type symesz = bfd_coff_symesz (abfd);
348 asection *o;
349 bfd_byte *esym;
351 if (! info->relocatable)
352 return TRUE;
354 esym = (bfd_byte *) bfd_malloc (symesz);
355 if (esym == NULL)
356 return FALSE;
358 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
359 return FALSE;
361 for (o = abfd->sections; o != NULL; o = o->next)
363 struct internal_syment isym;
365 strncpy (isym._n._n_name, o->name, SYMNMLEN);
366 isym.n_value = 0;
367 isym.n_scnum = o->target_index;
368 isym.n_type = T_NULL;
369 isym.n_sclass = C_STAT;
370 isym.n_numaux = 0;
372 bfd_coff_swap_sym_out (abfd, (PTR) &isym, (PTR) esym);
374 if (bfd_bwrite (esym, symesz, abfd) != symesz)
376 free (esym);
377 return FALSE;
380 obj_raw_syment_count (abfd) += 1;
383 free (esym);
385 return TRUE;
388 /* The reloc processing routine for the optimized COFF linker. */
390 static bfd_boolean
391 coff_i960_relocate_section (output_bfd, info, input_bfd, input_section,
392 contents, relocs, syms, sections)
393 bfd *output_bfd ATTRIBUTE_UNUSED;
394 struct bfd_link_info *info;
395 bfd *input_bfd;
396 asection *input_section;
397 bfd_byte *contents;
398 struct internal_reloc *relocs;
399 struct internal_syment *syms;
400 asection **sections;
402 struct internal_reloc *rel;
403 struct internal_reloc *relend;
405 rel = relocs;
406 relend = rel + input_section->reloc_count;
407 for (; rel < relend; rel++)
409 long symndx;
410 struct coff_link_hash_entry *h;
411 struct internal_syment *sym;
412 bfd_vma addend;
413 bfd_vma val;
414 reloc_howto_type *howto;
415 bfd_reloc_status_type rstat = bfd_reloc_ok;
416 bfd_boolean done;
418 symndx = rel->r_symndx;
420 if (symndx == -1)
422 h = NULL;
423 sym = NULL;
425 else
427 h = obj_coff_sym_hashes (input_bfd)[symndx];
428 sym = syms + symndx;
431 if (sym != NULL && sym->n_scnum != 0)
432 addend = - sym->n_value;
433 else
434 addend = 0;
436 switch (rel->r_type)
438 case 17: howto = &howto_rellong; break;
439 case 25: howto = &howto_iprmed; break;
440 case 27: howto = &howto_optcall; break;
441 default:
442 bfd_set_error (bfd_error_bad_value);
443 return FALSE;
446 val = 0;
448 if (h == NULL)
450 asection *sec;
452 if (symndx == -1)
454 sec = bfd_abs_section_ptr;
455 val = 0;
457 else
459 sec = sections[symndx];
460 val = (sec->output_section->vma
461 + sec->output_offset
462 + sym->n_value
463 - sec->vma);
466 else
468 if (h->root.type == bfd_link_hash_defined
469 || h->root.type == bfd_link_hash_defweak)
471 asection *sec;
473 sec = h->root.u.def.section;
474 val = (h->root.u.def.value
475 + sec->output_section->vma
476 + sec->output_offset);
478 else if (! info->relocatable)
480 if (! ((*info->callbacks->undefined_symbol)
481 (info, h->root.root.string, input_bfd, input_section,
482 rel->r_vaddr - input_section->vma, TRUE)))
483 return FALSE;
487 done = FALSE;
489 if (howto->type == R_OPTCALL && ! info->relocatable && symndx != -1)
491 int class;
493 if (h != NULL)
494 class = h->class;
495 else
496 class = sym->n_sclass;
498 switch (class)
500 case C_NULL:
501 /* This symbol is apparently not from a COFF input file.
502 We warn, and then assume that it is not a leaf
503 function. */
504 if (! ((*info->callbacks->reloc_dangerous)
505 (info,
506 _("uncertain calling convention for non-COFF symbol"),
507 input_bfd, input_section,
508 rel->r_vaddr - input_section->vma)))
509 return FALSE;
510 break;
511 case C_LEAFSTAT:
512 case C_LEAFEXT:
513 /* This is a call to a leaf procedure; use the bal
514 instruction. */
516 long olf;
517 unsigned long word;
519 if (h != NULL)
521 BFD_ASSERT (h->numaux == 2);
522 olf = h->aux[1].x_bal.x_balntry;
524 else
526 bfd_byte *esyms;
527 union internal_auxent aux;
529 BFD_ASSERT (sym->n_numaux == 2);
530 esyms = (bfd_byte *) obj_coff_external_syms (input_bfd);
531 esyms += (symndx + 2) * bfd_coff_symesz (input_bfd);
532 bfd_coff_swap_aux_in (input_bfd, (PTR) esyms, sym->n_type,
533 sym->n_sclass, 1, sym->n_numaux,
534 (PTR) &aux);
535 olf = aux.x_bal.x_balntry;
538 word = bfd_get_32 (input_bfd,
539 (contents
540 + (rel->r_vaddr - input_section->vma)));
541 word = ((word + olf - val) & BAL_MASK) | BAL;
542 bfd_put_32 (input_bfd,
543 (bfd_vma) word,
544 contents + (rel->r_vaddr - input_section->vma));
545 done = TRUE;
547 break;
548 case C_SCALL:
549 BFD_ASSERT (0);
550 break;
554 if (! done)
556 if (howto->pc_relative)
557 addend += input_section->vma;
558 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
559 contents,
560 rel->r_vaddr - input_section->vma,
561 val, addend);
564 switch (rstat)
566 default:
567 abort ();
568 case bfd_reloc_ok:
569 break;
570 case bfd_reloc_overflow:
572 const char *name;
573 char buf[SYMNMLEN + 1];
575 if (symndx == -1)
576 name = "*ABS*";
577 else if (h != NULL)
578 name = NULL;
579 else
581 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
582 if (name == NULL)
583 return FALSE;
586 if (! ((*info->callbacks->reloc_overflow)
587 (info, (h ? &h->root : NULL), name, howto->name,
588 (bfd_vma) 0, input_bfd, input_section,
589 rel->r_vaddr - input_section->vma)))
590 return FALSE;
595 return TRUE;
598 /* Adjust the symbol index of any reloc against a global symbol to
599 instead be a reloc against the internal symbol we created specially
600 for the section. */
602 static bfd_boolean
603 coff_i960_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
604 bfd *obfd ATTRIBUTE_UNUSED;
605 struct bfd_link_info *info ATTRIBUTE_UNUSED;
606 bfd *ibfd;
607 asection *sec ATTRIBUTE_UNUSED;
608 struct internal_reloc *irel;
609 bfd_boolean *adjustedp;
611 struct coff_link_hash_entry *h;
613 *adjustedp = FALSE;
615 h = obj_coff_sym_hashes (ibfd)[irel->r_symndx];
616 if (h == NULL
617 || (h->root.type != bfd_link_hash_defined
618 && h->root.type != bfd_link_hash_defweak))
619 return TRUE;
621 irel->r_symndx = h->root.u.def.section->output_section->target_index - 1;
622 *adjustedp = TRUE;
624 return TRUE;
627 #define coff_bfd_is_local_label_name coff_i960_is_local_label_name
629 #define coff_start_final_link coff_i960_start_final_link
631 #define coff_relocate_section coff_i960_relocate_section
633 #define coff_adjust_symndx coff_i960_adjust_symndx
635 #define coff_bfd_reloc_type_lookup coff_i960_reloc_type_lookup
636 #define coff_bfd_reloc_name_lookup coff_i960_reloc_name_lookup
638 #include "coffcode.h"
640 extern const bfd_target icoff_big_vec;
642 CREATE_LITTLE_COFF_TARGET_VEC (icoff_little_vec, "coff-Intel-little", 0, 0, '_', & icoff_big_vec, COFF_SWAP_TABLE)
644 const bfd_target icoff_big_vec =
646 "coff-Intel-big", /* name */
647 bfd_target_coff_flavour,
648 BFD_ENDIAN_LITTLE, /* data byte order is little */
649 BFD_ENDIAN_BIG, /* header byte order is big */
651 (HAS_RELOC | EXEC_P | /* object flags */
652 HAS_LINENO | HAS_DEBUG |
653 HAS_SYMS | HAS_LOCALS | WP_TEXT),
655 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
656 '_', /* leading underscore */
657 '/', /* ar_pad_char */
658 15, /* ar_max_namelen */
660 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
661 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
662 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
663 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
664 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
665 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
667 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
668 bfd_generic_archive_p, _bfd_dummy_target},
669 {bfd_false, coff_mkobject, /* bfd_set_format */
670 _bfd_generic_mkarchive, bfd_false},
671 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
672 _bfd_write_archive_contents, bfd_false},
674 BFD_JUMP_TABLE_GENERIC (coff),
675 BFD_JUMP_TABLE_COPY (coff),
676 BFD_JUMP_TABLE_CORE (_bfd_nocore),
677 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
678 BFD_JUMP_TABLE_SYMBOLS (coff),
679 BFD_JUMP_TABLE_RELOCS (coff),
680 BFD_JUMP_TABLE_WRITE (coff),
681 BFD_JUMP_TABLE_LINK (coff),
682 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
684 & icoff_little_vec,
686 COFF_SWAP_TABLE