* elf32-spu.c (build_stub): Fix malloc under-allocation.
[binutils.git] / bfd / coff-i960.c
blob928315f97517ad98b2ec784dc489775e91cf4f62
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, 2005, 2007, 2008, 2009, 2011
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #define I960 1
25 #define BADMAG(x) I960BADMAG(x)
27 #include "sysdep.h"
28 #include "bfd.h"
29 #include "libbfd.h"
30 #include "coff/i960.h"
31 #include "coff/internal.h"
33 #ifndef bfd_pe_print_pdata
34 #define bfd_pe_print_pdata NULL
35 #endif
37 #include "libcoff.h" /* To allow easier abstraction-breaking. */
39 static bfd_boolean coff_i960_is_local_label_name
40 PARAMS ((bfd *, const char *));
41 static bfd_reloc_status_type optcall_callback
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static bfd_reloc_status_type coff_i960_relocate
44 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
45 static reloc_howto_type *coff_i960_reloc_type_lookup
46 PARAMS ((bfd *, bfd_reloc_code_real_type));
47 static bfd_boolean coff_i960_start_final_link
48 PARAMS ((bfd *, struct bfd_link_info *));
49 static bfd_boolean coff_i960_relocate_section
50 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
51 struct internal_reloc *, struct internal_syment *, asection **));
52 static bfd_boolean coff_i960_adjust_symndx
53 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
54 struct internal_reloc *, bfd_boolean *));
56 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
57 #define COFF_ALIGN_IN_SECTION_HEADER 1
59 #define GET_SCNHDR_ALIGN H_GET_32
60 #define PUT_SCNHDR_ALIGN H_PUT_32
62 /* The i960 does not support an MMU, so COFF_PAGE_SIZE can be
63 arbitrarily small. */
64 #define COFF_PAGE_SIZE 1
66 #define COFF_LONG_FILENAMES
68 /* This set of local label names is taken from gas. */
70 static bfd_boolean
71 coff_i960_is_local_label_name (abfd, name)
72 bfd *abfd ATTRIBUTE_UNUSED;
73 const char *name;
75 return (name[0] == 'L'
76 || (name[0] == '.'
77 && (name[1] == 'C'
78 || name[1] == 'I'
79 || name[1] == '.')));
82 /* This is just like the usual CALC_ADDEND, but it includes the
83 section VMA for PC relative relocs. */
84 #ifndef CALC_ADDEND
85 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
86 { \
87 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
88 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
89 coffsym = (obj_symbols (abfd) \
90 + (cache_ptr->sym_ptr_ptr - symbols)); \
91 else if (ptr) \
92 coffsym = coff_symbol_from (abfd, ptr); \
93 if (coffsym != (coff_symbol_type *) NULL \
94 && coffsym->native->u.syment.n_scnum == 0) \
95 cache_ptr->addend = 0; \
96 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
97 && ptr->section != (asection *) NULL) \
98 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
99 else \
100 cache_ptr->addend = 0; \
101 if (ptr && (reloc.r_type == 25 || reloc.r_type == 27)) \
102 cache_ptr->addend += asect->vma; \
104 #endif
106 #define CALLS 0x66003800 /* Template for 'calls' instruction */
107 #define BAL 0x0b000000 /* Template for 'bal' instruction */
108 #define BAL_MASK 0x00ffffff
110 static bfd_reloc_status_type
111 optcall_callback (abfd, reloc_entry, symbol_in, data,
112 input_section, ignore_bfd, error_message)
113 bfd *abfd;
114 arelent *reloc_entry;
115 asymbol *symbol_in;
116 PTR data;
117 asection *input_section;
118 bfd *ignore_bfd ATTRIBUTE_UNUSED;
119 char **error_message;
121 /* This item has already been relocated correctly, but we may be
122 * able to patch in yet better code - done by digging out the
123 * correct info on this symbol */
124 bfd_reloc_status_type result;
125 coff_symbol_type *cs = coffsymbol(symbol_in);
127 /* Don't do anything with symbols which aren't tied up yet,
128 except move the reloc. */
129 if (bfd_is_und_section (cs->symbol.section)) {
130 reloc_entry->address += input_section->output_offset;
131 return bfd_reloc_ok;
134 /* So the target symbol has to be of coff type, and the symbol
135 has to have the correct native information within it */
136 if ((bfd_asymbol_flavour(&cs->symbol) != bfd_target_coff_flavour)
137 || (cs->native == (combined_entry_type *)NULL))
139 /* This is interesting, consider the case where we're outputting coff
140 from a mix n match input, linking from coff to a symbol defined in a
141 bout file will cause this match to be true. Should I complain? This
142 will only work if the bout symbol is non leaf. */
143 *error_message =
144 (char *) _("uncertain calling convention for non-COFF symbol");
145 result = bfd_reloc_dangerous;
147 else
149 switch (cs->native->u.syment.n_sclass)
151 case C_LEAFSTAT:
152 case C_LEAFEXT:
153 /* This is a call to a leaf procedure, replace instruction with a bal
154 to the correct location. */
156 union internal_auxent *aux = &((cs->native+2)->u.auxent);
157 int word = bfd_get_32 (abfd, (bfd_byte *)data + reloc_entry->address);
158 int olf = (aux->x_bal.x_balntry - cs->native->u.syment.n_value);
159 BFD_ASSERT(cs->native->u.syment.n_numaux==2);
161 /* We replace the original call instruction with a bal to
162 the bal entry point - the offset of which is described in
163 the 2nd auxent of the original symbol. We keep the native
164 sym and auxents untouched, so the delta between the two
165 is the offset of the bal entry point. */
166 word = ((word + olf) & BAL_MASK) | BAL;
167 bfd_put_32 (abfd, (bfd_vma) word,
168 (bfd_byte *) data + reloc_entry->address);
170 result = bfd_reloc_ok;
171 break;
172 case C_SCALL:
173 /* This is a call to a system call, replace with a calls to # */
174 BFD_ASSERT(0);
175 result = bfd_reloc_ok;
176 break;
177 default:
178 result = bfd_reloc_ok;
179 break;
182 return result;
185 /* i960 COFF is used by VxWorks 5.1. However, VxWorks 5.1 does not
186 appear to correctly handle a reloc against a symbol defined in the
187 same object file. It appears to simply discard such relocs, rather
188 than adding their values into the object file. We handle this here
189 by converting all relocs against defined symbols into relocs
190 against the section symbol, when generating a relocatable output
191 file.
193 Note that this function is only called if we are not using the COFF
194 specific backend linker. It only does something when doing a
195 relocatable link, which will almost certainly fail when not
196 generating COFF i960 output, so this function is actually no longer
197 useful. It was used before this target was converted to use the
198 COFF specific backend linker. */
200 static bfd_reloc_status_type
201 coff_i960_relocate (abfd, reloc_entry, symbol, data, input_section,
202 output_bfd, error_message)
203 bfd *abfd;
204 arelent *reloc_entry;
205 asymbol *symbol;
206 PTR data ATTRIBUTE_UNUSED;
207 asection *input_section ATTRIBUTE_UNUSED;
208 bfd *output_bfd;
209 char **error_message ATTRIBUTE_UNUSED;
211 asection *osec;
213 if (output_bfd == NULL)
215 /* Not generating relocatable output file. */
216 return bfd_reloc_continue;
219 if (bfd_is_und_section (bfd_get_section (symbol)))
221 /* Symbol is not defined, so no need to worry about it. */
222 return bfd_reloc_continue;
225 if (bfd_is_com_section (bfd_get_section (symbol)))
227 /* I don't really know what the right action is for a common
228 symbol. */
229 return bfd_reloc_continue;
232 /* Convert the reloc to use the section symbol. FIXME: This method
233 is ridiculous. */
234 osec = bfd_get_section (symbol)->output_section;
235 if (coff_section_data (output_bfd, osec) != NULL
236 && coff_section_data (output_bfd, osec)->tdata != NULL)
237 reloc_entry->sym_ptr_ptr =
238 (asymbol **) coff_section_data (output_bfd, osec)->tdata;
239 else
241 const char *sec_name;
242 asymbol **syms, **sym_end;
244 sec_name = bfd_get_section_name (output_bfd, osec);
245 syms = bfd_get_outsymbols (output_bfd);
246 sym_end = syms + bfd_get_symcount (output_bfd);
247 for (; syms < sym_end; syms++)
249 if (bfd_asymbol_name (*syms) != NULL
250 && (*syms)->value == 0
251 && strcmp ((*syms)->section->output_section->name,
252 sec_name) == 0)
253 break;
256 if (syms >= sym_end)
257 abort ();
259 reloc_entry->sym_ptr_ptr = syms;
261 if (coff_section_data (output_bfd, osec) == NULL)
263 bfd_size_type amt = sizeof (struct coff_section_tdata);
264 osec->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
265 if (osec->used_by_bfd == NULL)
266 return bfd_reloc_overflow;
268 coff_section_data (output_bfd, osec)->tdata = (PTR) syms;
271 /* Let bfd_perform_relocation do its thing, which will include
272 stuffing the symbol addend into the object file. */
273 return bfd_reloc_continue;
276 static reloc_howto_type howto_rellong =
277 HOWTO ((unsigned int) R_RELLONG, 0, 2, 32,FALSE, 0,
278 complain_overflow_bitfield, coff_i960_relocate,"rellong", TRUE,
279 0xffffffff, 0xffffffff, 0);
280 static reloc_howto_type howto_iprmed =
281 HOWTO (R_IPRMED, 0, 2, 24,TRUE,0, complain_overflow_signed,
282 coff_i960_relocate, "iprmed ", TRUE, 0x00ffffff, 0x00ffffff, 0);
283 static reloc_howto_type howto_optcall =
284 HOWTO (R_OPTCALL, 0,2,24,TRUE,0, complain_overflow_signed,
285 optcall_callback, "optcall", TRUE, 0x00ffffff, 0x00ffffff, 0);
287 static reloc_howto_type *
288 coff_i960_reloc_type_lookup (abfd, code)
289 bfd *abfd ATTRIBUTE_UNUSED;
290 bfd_reloc_code_real_type code;
292 switch (code)
294 default:
295 return 0;
296 case BFD_RELOC_I960_CALLJ:
297 return &howto_optcall;
298 case BFD_RELOC_32:
299 case BFD_RELOC_CTOR:
300 return &howto_rellong;
301 case BFD_RELOC_24_PCREL:
302 return &howto_iprmed;
306 static reloc_howto_type *
307 coff_i960_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
308 const char *r_name)
310 if (strcasecmp (howto_optcall.name, r_name) == 0)
311 return &howto_optcall;
312 if (strcasecmp (howto_rellong.name, r_name) == 0)
313 return &howto_rellong;
314 if (strcasecmp (howto_iprmed.name, r_name) == 0)
315 return &howto_iprmed;
317 return NULL;
320 /* The real code is in coffcode.h */
322 #define RTYPE2HOWTO(cache_ptr, dst) \
324 reloc_howto_type *howto_ptr; \
325 switch ((dst)->r_type) { \
326 case 17: howto_ptr = &howto_rellong; break; \
327 case 25: howto_ptr = &howto_iprmed; break; \
328 case 27: howto_ptr = &howto_optcall; break; \
329 default: howto_ptr = 0; break; \
331 (cache_ptr)->howto = howto_ptr; \
334 /* i960 COFF is used by VxWorks 5.1. However, VxWorks 5.1 does not
335 appear to correctly handle a reloc against a symbol defined in the
336 same object file. It appears to simply discard such relocs, rather
337 than adding their values into the object file. We handle this by
338 converting all relocs against global symbols into relocs against
339 internal symbols at the start of the section. This routine is
340 called at the start of the linking process, and it creates the
341 necessary symbols. */
343 static bfd_boolean
344 coff_i960_start_final_link (abfd, info)
345 bfd *abfd;
346 struct bfd_link_info *info;
348 bfd_size_type symesz = bfd_coff_symesz (abfd);
349 asection *o;
350 bfd_byte *esym;
352 if (! info->relocatable)
353 return TRUE;
355 esym = (bfd_byte *) bfd_malloc (symesz);
356 if (esym == NULL)
357 return FALSE;
359 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
360 return FALSE;
362 for (o = abfd->sections; o != NULL; o = o->next)
364 struct internal_syment isym;
366 strncpy (isym._n._n_name, o->name, SYMNMLEN);
367 isym.n_value = 0;
368 isym.n_scnum = o->target_index;
369 isym.n_type = T_NULL;
370 isym.n_sclass = C_STAT;
371 isym.n_numaux = 0;
373 bfd_coff_swap_sym_out (abfd, (PTR) &isym, (PTR) esym);
375 if (bfd_bwrite (esym, symesz, abfd) != symesz)
377 free (esym);
378 return FALSE;
381 obj_raw_syment_count (abfd) += 1;
384 free (esym);
386 return TRUE;
389 /* The reloc processing routine for the optimized COFF linker. */
391 static bfd_boolean
392 coff_i960_relocate_section (output_bfd, info, input_bfd, input_section,
393 contents, relocs, syms, sections)
394 bfd *output_bfd ATTRIBUTE_UNUSED;
395 struct bfd_link_info *info;
396 bfd *input_bfd;
397 asection *input_section;
398 bfd_byte *contents;
399 struct internal_reloc *relocs;
400 struct internal_syment *syms;
401 asection **sections;
403 struct internal_reloc *rel;
404 struct internal_reloc *relend;
406 rel = relocs;
407 relend = rel + input_section->reloc_count;
408 for (; rel < relend; rel++)
410 long symndx;
411 struct coff_link_hash_entry *h;
412 struct internal_syment *sym;
413 bfd_vma addend;
414 bfd_vma val;
415 reloc_howto_type *howto;
416 bfd_reloc_status_type rstat = bfd_reloc_ok;
417 bfd_boolean done;
419 symndx = rel->r_symndx;
421 if (symndx == -1)
423 h = NULL;
424 sym = NULL;
426 else
428 h = obj_coff_sym_hashes (input_bfd)[symndx];
429 sym = syms + symndx;
432 if (sym != NULL && sym->n_scnum != 0)
433 addend = - sym->n_value;
434 else
435 addend = 0;
437 switch (rel->r_type)
439 case 17: howto = &howto_rellong; break;
440 case 25: howto = &howto_iprmed; break;
441 case 27: howto = &howto_optcall; break;
442 default:
443 bfd_set_error (bfd_error_bad_value);
444 return FALSE;
447 val = 0;
449 if (h == NULL)
451 asection *sec;
453 if (symndx == -1)
455 sec = bfd_abs_section_ptr;
456 val = 0;
458 else
460 sec = sections[symndx];
461 val = (sec->output_section->vma
462 + sec->output_offset
463 + sym->n_value
464 - sec->vma);
467 else
469 if (h->root.type == bfd_link_hash_defined
470 || h->root.type == bfd_link_hash_defweak)
472 asection *sec;
474 sec = h->root.u.def.section;
475 val = (h->root.u.def.value
476 + sec->output_section->vma
477 + sec->output_offset);
479 else if (! info->relocatable)
481 if (! ((*info->callbacks->undefined_symbol)
482 (info, h->root.root.string, input_bfd, input_section,
483 rel->r_vaddr - input_section->vma, TRUE)))
484 return FALSE;
488 done = FALSE;
490 if (howto->type == R_OPTCALL && ! info->relocatable && symndx != -1)
492 int class_val;
494 if (h != NULL)
495 class_val = h->symbol_class;
496 else
497 class_val = sym->n_sclass;
499 switch (class_val)
501 case C_NULL:
502 /* This symbol is apparently not from a COFF input file.
503 We warn, and then assume that it is not a leaf
504 function. */
505 if (! ((*info->callbacks->reloc_dangerous)
506 (info,
507 _("uncertain calling convention for non-COFF symbol"),
508 input_bfd, input_section,
509 rel->r_vaddr - input_section->vma)))
510 return FALSE;
511 break;
512 case C_LEAFSTAT:
513 case C_LEAFEXT:
514 /* This is a call to a leaf procedure; use the bal
515 instruction. */
517 long olf;
518 unsigned long word;
520 if (h != NULL)
522 BFD_ASSERT (h->numaux == 2);
523 olf = h->aux[1].x_bal.x_balntry;
525 else
527 bfd_byte *esyms;
528 union internal_auxent aux;
530 BFD_ASSERT (sym->n_numaux == 2);
531 esyms = (bfd_byte *) obj_coff_external_syms (input_bfd);
532 esyms += (symndx + 2) * bfd_coff_symesz (input_bfd);
533 bfd_coff_swap_aux_in (input_bfd, (PTR) esyms, sym->n_type,
534 sym->n_sclass, 1, sym->n_numaux,
535 (PTR) &aux);
536 olf = aux.x_bal.x_balntry;
539 word = bfd_get_32 (input_bfd,
540 (contents
541 + (rel->r_vaddr - input_section->vma)));
542 word = ((word + olf - val) & BAL_MASK) | BAL;
543 bfd_put_32 (input_bfd,
544 (bfd_vma) word,
545 contents + (rel->r_vaddr - input_section->vma));
546 done = TRUE;
548 break;
549 case C_SCALL:
550 BFD_ASSERT (0);
551 break;
555 if (! done)
557 if (howto->pc_relative)
558 addend += input_section->vma;
559 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
560 contents,
561 rel->r_vaddr - input_section->vma,
562 val, addend);
565 switch (rstat)
567 default:
568 abort ();
569 case bfd_reloc_ok:
570 break;
571 case bfd_reloc_overflow:
573 const char *name;
574 char buf[SYMNMLEN + 1];
576 if (symndx == -1)
577 name = "*ABS*";
578 else if (h != NULL)
579 name = NULL;
580 else
582 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
583 if (name == NULL)
584 return FALSE;
587 if (! ((*info->callbacks->reloc_overflow)
588 (info, (h ? &h->root : NULL), name, howto->name,
589 (bfd_vma) 0, input_bfd, input_section,
590 rel->r_vaddr - input_section->vma)))
591 return FALSE;
596 return TRUE;
599 /* Adjust the symbol index of any reloc against a global symbol to
600 instead be a reloc against the internal symbol we created specially
601 for the section. */
603 static bfd_boolean
604 coff_i960_adjust_symndx (obfd, info, ibfd, sec, irel, adjustedp)
605 bfd *obfd ATTRIBUTE_UNUSED;
606 struct bfd_link_info *info ATTRIBUTE_UNUSED;
607 bfd *ibfd;
608 asection *sec ATTRIBUTE_UNUSED;
609 struct internal_reloc *irel;
610 bfd_boolean *adjustedp;
612 struct coff_link_hash_entry *h;
614 *adjustedp = FALSE;
616 h = obj_coff_sym_hashes (ibfd)[irel->r_symndx];
617 if (h == NULL
618 || (h->root.type != bfd_link_hash_defined
619 && h->root.type != bfd_link_hash_defweak))
620 return TRUE;
622 irel->r_symndx = h->root.u.def.section->output_section->target_index - 1;
623 *adjustedp = TRUE;
625 return TRUE;
628 #define coff_bfd_is_local_label_name coff_i960_is_local_label_name
630 #define coff_start_final_link coff_i960_start_final_link
632 #define coff_relocate_section coff_i960_relocate_section
634 #define coff_adjust_symndx coff_i960_adjust_symndx
636 #define coff_bfd_reloc_type_lookup coff_i960_reloc_type_lookup
637 #define coff_bfd_reloc_name_lookup coff_i960_reloc_name_lookup
639 #include "coffcode.h"
641 extern const bfd_target icoff_big_vec;
643 CREATE_LITTLE_COFF_TARGET_VEC (icoff_little_vec, "coff-Intel-little", 0, 0, '_', & icoff_big_vec, COFF_SWAP_TABLE)
645 const bfd_target icoff_big_vec =
647 "coff-Intel-big", /* name */
648 bfd_target_coff_flavour,
649 BFD_ENDIAN_LITTLE, /* data byte order is little */
650 BFD_ENDIAN_BIG, /* header byte order is big */
652 (HAS_RELOC | EXEC_P | /* object flags */
653 HAS_LINENO | HAS_DEBUG |
654 HAS_SYMS | HAS_LOCALS | WP_TEXT),
656 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
657 '_', /* leading underscore */
658 '/', /* ar_pad_char */
659 15, /* ar_max_namelen */
660 0, /* match priority. */
662 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
663 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
664 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
665 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
666 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
667 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
669 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
670 bfd_generic_archive_p, _bfd_dummy_target},
671 {bfd_false, coff_mkobject, /* bfd_set_format */
672 _bfd_generic_mkarchive, bfd_false},
673 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
674 _bfd_write_archive_contents, bfd_false},
676 BFD_JUMP_TABLE_GENERIC (coff),
677 BFD_JUMP_TABLE_COPY (coff),
678 BFD_JUMP_TABLE_CORE (_bfd_nocore),
679 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
680 BFD_JUMP_TABLE_SYMBOLS (coff),
681 BFD_JUMP_TABLE_RELOCS (coff),
682 BFD_JUMP_TABLE_WRITE (coff),
683 BFD_JUMP_TABLE_LINK (coff),
684 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
686 & icoff_little_vec,
688 COFF_SWAP_TABLE