daily update
[binutils.git] / bfd / coff-i960.c
blob5bf028c5c4a4e85438e3adbc727a7490e5a95b9c
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 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 2 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, MA 02110-1301, USA. */
22 #define I960 1
23 #define BADMAG(x) I960BADMAG(x)
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "libbfd.h"
28 #include "coff/i960.h"
29 #include "coff/internal.h"
30 #include "libcoff.h" /* to allow easier abstraction-breaking */
32 static bfd_boolean coff_i960_is_local_label_name
33 PARAMS ((bfd *, const char *));
34 static bfd_reloc_status_type optcall_callback
35 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
36 static bfd_reloc_status_type coff_i960_relocate
37 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
38 static reloc_howto_type *coff_i960_reloc_type_lookup
39 PARAMS ((bfd *, bfd_reloc_code_real_type));
40 static bfd_boolean coff_i960_start_final_link
41 PARAMS ((bfd *, struct bfd_link_info *));
42 static bfd_boolean coff_i960_relocate_section
43 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
44 struct internal_reloc *, struct internal_syment *, asection **));
45 static bfd_boolean coff_i960_adjust_symndx
46 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
47 struct internal_reloc *, bfd_boolean *));
49 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
50 #define COFF_ALIGN_IN_SECTION_HEADER 1
52 #define GET_SCNHDR_ALIGN H_GET_32
53 #define PUT_SCNHDR_ALIGN H_PUT_32
55 /* The i960 does not support an MMU, so COFF_PAGE_SIZE can be
56 arbitrarily small. */
57 #define COFF_PAGE_SIZE 1
59 #define COFF_LONG_FILENAMES
61 /* This set of local label names is taken from gas. */
63 static bfd_boolean
64 coff_i960_is_local_label_name (abfd, name)
65 bfd *abfd ATTRIBUTE_UNUSED;
66 const char *name;
68 return (name[0] == 'L'
69 || (name[0] == '.'
70 && (name[1] == 'C'
71 || name[1] == 'I'
72 || name[1] == '.')));
75 /* This is just like the usual CALC_ADDEND, but it includes the
76 section VMA for PC relative relocs. */
77 #ifndef CALC_ADDEND
78 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
79 { \
80 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
81 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
82 coffsym = (obj_symbols (abfd) \
83 + (cache_ptr->sym_ptr_ptr - symbols)); \
84 else if (ptr) \
85 coffsym = coff_symbol_from (abfd, ptr); \
86 if (coffsym != (coff_symbol_type *) NULL \
87 && coffsym->native->u.syment.n_scnum == 0) \
88 cache_ptr->addend = 0; \
89 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
90 && ptr->section != (asection *) NULL) \
91 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
92 else \
93 cache_ptr->addend = 0; \
94 if (ptr && (reloc.r_type == 25 || reloc.r_type == 27)) \
95 cache_ptr->addend += asect->vma; \
97 #endif
99 #define CALLS 0x66003800 /* Template for 'calls' instruction */
100 #define BAL 0x0b000000 /* Template for 'bal' instruction */
101 #define BAL_MASK 0x00ffffff
103 static bfd_reloc_status_type
104 optcall_callback (abfd, reloc_entry, symbol_in, data,
105 input_section, ignore_bfd, error_message)
106 bfd *abfd;
107 arelent *reloc_entry;
108 asymbol *symbol_in;
109 PTR data;
110 asection *input_section;
111 bfd *ignore_bfd ATTRIBUTE_UNUSED;
112 char **error_message;
114 /* This item has already been relocated correctly, but we may be
115 * able to patch in yet better code - done by digging out the
116 * correct info on this symbol */
117 bfd_reloc_status_type result;
118 coff_symbol_type *cs = coffsymbol(symbol_in);
120 /* Don't do anything with symbols which aren't tied up yet,
121 except move the reloc. */
122 if (bfd_is_und_section (cs->symbol.section)) {
123 reloc_entry->address += input_section->output_offset;
124 return bfd_reloc_ok;
127 /* So the target symbol has to be of coff type, and the symbol
128 has to have the correct native information within it */
129 if ((bfd_asymbol_flavour(&cs->symbol) != bfd_target_coff_flavour)
130 || (cs->native == (combined_entry_type *)NULL))
132 /* This is interesting, consider the case where we're outputting coff
133 from a mix n match input, linking from coff to a symbol defined in a
134 bout file will cause this match to be true. Should I complain? This
135 will only work if the bout symbol is non leaf. */
136 *error_message =
137 (char *) _("uncertain calling convention for non-COFF symbol");
138 result = bfd_reloc_dangerous;
140 else
142 switch (cs->native->u.syment.n_sclass)
144 case C_LEAFSTAT:
145 case C_LEAFEXT:
146 /* This is a call to a leaf procedure, replace instruction with a bal
147 to the correct location. */
149 union internal_auxent *aux = &((cs->native+2)->u.auxent);
150 int word = bfd_get_32 (abfd, (bfd_byte *)data + reloc_entry->address);
151 int olf = (aux->x_bal.x_balntry - cs->native->u.syment.n_value);
152 BFD_ASSERT(cs->native->u.syment.n_numaux==2);
154 /* We replace the original call instruction with a bal to
155 the bal entry point - the offset of which is described in
156 the 2nd auxent of the original symbol. We keep the native
157 sym and auxents untouched, so the delta between the two
158 is the offset of the bal entry point. */
159 word = ((word + olf) & BAL_MASK) | BAL;
160 bfd_put_32 (abfd, (bfd_vma) word,
161 (bfd_byte *) data + reloc_entry->address);
163 result = bfd_reloc_ok;
164 break;
165 case C_SCALL:
166 /* This is a call to a system call, replace with a calls to # */
167 BFD_ASSERT(0);
168 result = bfd_reloc_ok;
169 break;
170 default:
171 result = bfd_reloc_ok;
172 break;
175 return result;
178 /* i960 COFF is used by VxWorks 5.1. However, VxWorks 5.1 does not
179 appear to correctly handle a reloc against a symbol defined in the
180 same object file. It appears to simply discard such relocs, rather
181 than adding their values into the object file. We handle this here
182 by converting all relocs against defined symbols into relocs
183 against the section symbol, when generating a relocatable output
184 file.
186 Note that this function is only called if we are not using the COFF
187 specific backend linker. It only does something when doing a
188 relocatable link, which will almost certainly fail when not
189 generating COFF i960 output, so this function is actually no longer
190 useful. It was used before this target was converted to use the
191 COFF specific backend linker. */
193 static bfd_reloc_status_type
194 coff_i960_relocate (abfd, reloc_entry, symbol, data, input_section,
195 output_bfd, error_message)
196 bfd *abfd;
197 arelent *reloc_entry;
198 asymbol *symbol;
199 PTR data ATTRIBUTE_UNUSED;
200 asection *input_section ATTRIBUTE_UNUSED;
201 bfd *output_bfd;
202 char **error_message ATTRIBUTE_UNUSED;
204 asection *osec;
206 if (output_bfd == NULL)
208 /* Not generating relocatable output file. */
209 return bfd_reloc_continue;
212 if (bfd_is_und_section (bfd_get_section (symbol)))
214 /* Symbol is not defined, so no need to worry about it. */
215 return bfd_reloc_continue;
218 if (bfd_is_com_section (bfd_get_section (symbol)))
220 /* I don't really know what the right action is for a common
221 symbol. */
222 return bfd_reloc_continue;
225 /* Convert the reloc to use the section symbol. FIXME: This method
226 is ridiculous. */
227 osec = bfd_get_section (symbol)->output_section;
228 if (coff_section_data (output_bfd, osec) != NULL
229 && coff_section_data (output_bfd, osec)->tdata != NULL)
230 reloc_entry->sym_ptr_ptr =
231 (asymbol **) coff_section_data (output_bfd, osec)->tdata;
232 else
234 const char *sec_name;
235 asymbol **syms, **sym_end;
237 sec_name = bfd_get_section_name (output_bfd, osec);
238 syms = bfd_get_outsymbols (output_bfd);
239 sym_end = syms + bfd_get_symcount (output_bfd);
240 for (; syms < sym_end; syms++)
242 if (bfd_asymbol_name (*syms) != NULL
243 && (*syms)->value == 0
244 && strcmp ((*syms)->section->output_section->name,
245 sec_name) == 0)
246 break;
249 if (syms >= sym_end)
250 abort ();
252 reloc_entry->sym_ptr_ptr = syms;
254 if (coff_section_data (output_bfd, osec) == NULL)
256 bfd_size_type amt = sizeof (struct coff_section_tdata);
257 osec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
258 if (osec->used_by_bfd == NULL)
259 return bfd_reloc_overflow;
261 coff_section_data (output_bfd, osec)->tdata = (PTR) syms;
264 /* Let bfd_perform_relocation do its thing, which will include
265 stuffing the symbol addend into the object file. */
266 return bfd_reloc_continue;
269 static reloc_howto_type howto_rellong =
270 HOWTO ((unsigned int) R_RELLONG, 0, 2, 32,FALSE, 0,
271 complain_overflow_bitfield, coff_i960_relocate,"rellong", TRUE,
272 0xffffffff, 0xffffffff, 0);
273 static reloc_howto_type howto_iprmed =
274 HOWTO (R_IPRMED, 0, 2, 24,TRUE,0, complain_overflow_signed,
275 coff_i960_relocate, "iprmed ", TRUE, 0x00ffffff, 0x00ffffff, 0);
276 static reloc_howto_type howto_optcall =
277 HOWTO (R_OPTCALL, 0,2,24,TRUE,0, complain_overflow_signed,
278 optcall_callback, "optcall", TRUE, 0x00ffffff, 0x00ffffff, 0);
280 static reloc_howto_type *
281 coff_i960_reloc_type_lookup (abfd, code)
282 bfd *abfd ATTRIBUTE_UNUSED;
283 bfd_reloc_code_real_type code;
285 switch (code)
287 default:
288 return 0;
289 case BFD_RELOC_I960_CALLJ:
290 return &howto_optcall;
291 case BFD_RELOC_32:
292 case BFD_RELOC_CTOR:
293 return &howto_rellong;
294 case BFD_RELOC_24_PCREL:
295 return &howto_iprmed;
299 static reloc_howto_type *
300 coff_i960_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
301 const char *r_name)
303 if (strcasecmp (howto_optcall.name, r_name) == 0)
304 return &howto_optcall;
305 if (strcasecmp (howto_rellong.name, r_name) == 0)
306 return &howto_rellong;
307 if (strcasecmp (howto_iprmed.name, r_name) == 0)
308 return &howto_iprmed;
310 return NULL;
313 /* The real code is in coffcode.h */
315 #define RTYPE2HOWTO(cache_ptr, dst) \
317 reloc_howto_type *howto_ptr; \
318 switch ((dst)->r_type) { \
319 case 17: howto_ptr = &howto_rellong; break; \
320 case 25: howto_ptr = &howto_iprmed; break; \
321 case 27: howto_ptr = &howto_optcall; break; \
322 default: howto_ptr = 0; break; \
324 (cache_ptr)->howto = howto_ptr; \
327 /* i960 COFF is used by VxWorks 5.1. However, VxWorks 5.1 does not
328 appear to correctly handle a reloc against a symbol defined in the
329 same object file. It appears to simply discard such relocs, rather
330 than adding their values into the object file. We handle this by
331 converting all relocs against global symbols into relocs against
332 internal symbols at the start of the section. This routine is
333 called at the start of the linking process, and it creates the
334 necessary symbols. */
336 static bfd_boolean
337 coff_i960_start_final_link (abfd, info)
338 bfd *abfd;
339 struct bfd_link_info *info;
341 bfd_size_type symesz = bfd_coff_symesz (abfd);
342 asection *o;
343 bfd_byte *esym;
345 if (! info->relocatable)
346 return TRUE;
348 esym = (bfd_byte *) bfd_malloc (symesz);
349 if (esym == NULL)
350 return FALSE;
352 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
353 return FALSE;
355 for (o = abfd->sections; o != NULL; o = o->next)
357 struct internal_syment isym;
359 strncpy (isym._n._n_name, o->name, SYMNMLEN);
360 isym.n_value = 0;
361 isym.n_scnum = o->target_index;
362 isym.n_type = T_NULL;
363 isym.n_sclass = C_STAT;
364 isym.n_numaux = 0;
366 bfd_coff_swap_sym_out (abfd, (PTR) &isym, (PTR) esym);
368 if (bfd_bwrite (esym, symesz, abfd) != symesz)
370 free (esym);
371 return FALSE;
374 obj_raw_syment_count (abfd) += 1;
377 free (esym);
379 return TRUE;
382 /* The reloc processing routine for the optimized COFF linker. */
384 static bfd_boolean
385 coff_i960_relocate_section (output_bfd, info, input_bfd, input_section,
386 contents, relocs, syms, sections)
387 bfd *output_bfd ATTRIBUTE_UNUSED;
388 struct bfd_link_info *info;
389 bfd *input_bfd;
390 asection *input_section;
391 bfd_byte *contents;
392 struct internal_reloc *relocs;
393 struct internal_syment *syms;
394 asection **sections;
396 struct internal_reloc *rel;
397 struct internal_reloc *relend;
399 rel = relocs;
400 relend = rel + input_section->reloc_count;
401 for (; rel < relend; rel++)
403 long symndx;
404 struct coff_link_hash_entry *h;
405 struct internal_syment *sym;
406 bfd_vma addend;
407 bfd_vma val;
408 reloc_howto_type *howto;
409 bfd_reloc_status_type rstat = bfd_reloc_ok;
410 bfd_boolean done;
412 symndx = rel->r_symndx;
414 if (symndx == -1)
416 h = NULL;
417 sym = NULL;
419 else
421 h = obj_coff_sym_hashes (input_bfd)[symndx];
422 sym = syms + symndx;
425 if (sym != NULL && sym->n_scnum != 0)
426 addend = - sym->n_value;
427 else
428 addend = 0;
430 switch (rel->r_type)
432 case 17: howto = &howto_rellong; break;
433 case 25: howto = &howto_iprmed; break;
434 case 27: howto = &howto_optcall; break;
435 default:
436 bfd_set_error (bfd_error_bad_value);
437 return FALSE;
440 val = 0;
442 if (h == NULL)
444 asection *sec;
446 if (symndx == -1)
448 sec = bfd_abs_section_ptr;
449 val = 0;
451 else
453 sec = sections[symndx];
454 val = (sec->output_section->vma
455 + sec->output_offset
456 + sym->n_value
457 - sec->vma);
460 else
462 if (h->root.type == bfd_link_hash_defined
463 || h->root.type == bfd_link_hash_defweak)
465 asection *sec;
467 sec = h->root.u.def.section;
468 val = (h->root.u.def.value
469 + sec->output_section->vma
470 + sec->output_offset);
472 else if (! info->relocatable)
474 if (! ((*info->callbacks->undefined_symbol)
475 (info, h->root.root.string, input_bfd, input_section,
476 rel->r_vaddr - input_section->vma, TRUE)))
477 return FALSE;
481 done = FALSE;
483 if (howto->type == R_OPTCALL && ! info->relocatable && symndx != -1)
485 int class;
487 if (h != NULL)
488 class = h->class;
489 else
490 class = sym->n_sclass;
492 switch (class)
494 case C_NULL:
495 /* This symbol is apparently not from a COFF input file.
496 We warn, and then assume that it is not a leaf
497 function. */
498 if (! ((*info->callbacks->reloc_dangerous)
499 (info,
500 _("uncertain calling convention for non-COFF symbol"),
501 input_bfd, input_section,
502 rel->r_vaddr - input_section->vma)))
503 return FALSE;
504 break;
505 case C_LEAFSTAT:
506 case C_LEAFEXT:
507 /* This is a call to a leaf procedure; use the bal
508 instruction. */
510 long olf;
511 unsigned long word;
513 if (h != NULL)
515 BFD_ASSERT (h->numaux == 2);
516 olf = h->aux[1].x_bal.x_balntry;
518 else
520 bfd_byte *esyms;
521 union internal_auxent aux;
523 BFD_ASSERT (sym->n_numaux == 2);
524 esyms = (bfd_byte *) obj_coff_external_syms (input_bfd);
525 esyms += (symndx + 2) * bfd_coff_symesz (input_bfd);
526 bfd_coff_swap_aux_in (input_bfd, (PTR) esyms, sym->n_type,
527 sym->n_sclass, 1, sym->n_numaux,
528 (PTR) &aux);
529 olf = aux.x_bal.x_balntry;
532 word = bfd_get_32 (input_bfd,
533 (contents
534 + (rel->r_vaddr - input_section->vma)));
535 word = ((word + olf - val) & BAL_MASK) | BAL;
536 bfd_put_32 (input_bfd,
537 (bfd_vma) word,
538 contents + (rel->r_vaddr - input_section->vma));
539 done = TRUE;
541 break;
542 case C_SCALL:
543 BFD_ASSERT (0);
544 break;
548 if (! done)
550 if (howto->pc_relative)
551 addend += input_section->vma;
552 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
553 contents,
554 rel->r_vaddr - input_section->vma,
555 val, addend);
558 switch (rstat)
560 default:
561 abort ();
562 case bfd_reloc_ok:
563 break;
564 case bfd_reloc_overflow:
566 const char *name;
567 char buf[SYMNMLEN + 1];
569 if (symndx == -1)
570 name = "*ABS*";
571 else if (h != NULL)
572 name = NULL;
573 else
575 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
576 if (name == NULL)
577 return FALSE;
580 if (! ((*info->callbacks->reloc_overflow)
581 (info, (h ? &h->root : NULL), name, howto->name,
582 (bfd_vma) 0, input_bfd, input_section,
583 rel->r_vaddr - input_section->vma)))
584 return FALSE;
589 return TRUE;
592 /* Adjust the symbol index of any reloc against a global symbol to
593 instead be a reloc against the internal symbol we created specially
594 for the section. */
596 static bfd_boolean
597 coff_i960_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
598 bfd *obfd ATTRIBUTE_UNUSED;
599 struct bfd_link_info *info ATTRIBUTE_UNUSED;
600 bfd *ibfd;
601 asection *sec ATTRIBUTE_UNUSED;
602 struct internal_reloc *irel;
603 bfd_boolean *adjustedp;
605 struct coff_link_hash_entry *h;
607 *adjustedp = FALSE;
609 h = obj_coff_sym_hashes (ibfd)[irel->r_symndx];
610 if (h == NULL
611 || (h->root.type != bfd_link_hash_defined
612 && h->root.type != bfd_link_hash_defweak))
613 return TRUE;
615 irel->r_symndx = h->root.u.def.section->output_section->target_index - 1;
616 *adjustedp = TRUE;
618 return TRUE;
621 #define coff_bfd_is_local_label_name coff_i960_is_local_label_name
623 #define coff_start_final_link coff_i960_start_final_link
625 #define coff_relocate_section coff_i960_relocate_section
627 #define coff_adjust_symndx coff_i960_adjust_symndx
629 #define coff_bfd_reloc_type_lookup coff_i960_reloc_type_lookup
630 #define coff_bfd_reloc_name_lookup coff_i960_reloc_name_lookup
632 #include "coffcode.h"
634 extern const bfd_target icoff_big_vec;
636 CREATE_LITTLE_COFF_TARGET_VEC (icoff_little_vec, "coff-Intel-little", 0, 0, '_', & icoff_big_vec, COFF_SWAP_TABLE)
638 const bfd_target icoff_big_vec =
640 "coff-Intel-big", /* name */
641 bfd_target_coff_flavour,
642 BFD_ENDIAN_LITTLE, /* data byte order is little */
643 BFD_ENDIAN_BIG, /* header byte order is big */
645 (HAS_RELOC | EXEC_P | /* object flags */
646 HAS_LINENO | HAS_DEBUG |
647 HAS_SYMS | HAS_LOCALS | WP_TEXT),
649 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
650 '_', /* leading underscore */
651 '/', /* ar_pad_char */
652 15, /* ar_max_namelen */
654 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
655 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
656 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
657 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
658 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
659 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
661 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
662 bfd_generic_archive_p, _bfd_dummy_target},
663 {bfd_false, coff_mkobject, /* bfd_set_format */
664 _bfd_generic_mkarchive, bfd_false},
665 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
666 _bfd_write_archive_contents, bfd_false},
668 BFD_JUMP_TABLE_GENERIC (coff),
669 BFD_JUMP_TABLE_COPY (coff),
670 BFD_JUMP_TABLE_CORE (_bfd_nocore),
671 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
672 BFD_JUMP_TABLE_SYMBOLS (coff),
673 BFD_JUMP_TABLE_RELOCS (coff),
674 BFD_JUMP_TABLE_WRITE (coff),
675 BFD_JUMP_TABLE_LINK (coff),
676 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
678 & icoff_little_vec,
680 COFF_SWAP_TABLE