1 /* BFD back-end for Intel 960 b.out binaries.
2 Copyright (C) 1990-2015 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
28 #include "libiberty.h"
30 #include "aout/stab_gnu.h"
31 #include "libaout.h" /* BFD a.out internal data structures. */
34 #define ABS32CODE_SHRUNK 1
39 #define ABS32_MAYBE_RELAXABLE 1
40 #define ABS32_WAS_RELAXABLE 2
45 static reloc_howto_type howto_reloc_callj =
46 HOWTO (CALLJ, 0, 2, 24, TRUE, 0, complain_overflow_signed, 0,"callj", TRUE, 0x00ffffff, 0x00ffffff,FALSE);
47 static reloc_howto_type howto_reloc_abs32 =
48 HOWTO (ABS32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"abs32", TRUE, 0xffffffff,0xffffffff,FALSE);
49 static reloc_howto_type howto_reloc_pcrel24 =
50 HOWTO (PCREL24, 0, 2, 24, TRUE, 0, complain_overflow_signed,0,"pcrel24", TRUE, 0x00ffffff,0x00ffffff,FALSE);
51 static reloc_howto_type howto_reloc_pcrel13 =
52 HOWTO (PCREL13, 0, 2, 13, TRUE, 0, complain_overflow_signed,0,"pcrel13", TRUE, 0x00001fff,0x00001fff,FALSE);
53 static reloc_howto_type howto_reloc_abs32codeshrunk =
54 HOWTO (ABS32CODE_SHRUNK, 0, 2, 24, TRUE, 0, complain_overflow_signed, 0,"callx->callj", TRUE, 0x00ffffff, 0x00ffffff,FALSE);
55 static reloc_howto_type howto_reloc_abs32code =
56 HOWTO (ABS32CODE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"callx", TRUE, 0xffffffff,0xffffffff,FALSE);
58 static reloc_howto_type howto_align_table[] =
60 HOWTO (ALIGNER, 0, 0x1, 0, FALSE, 0, complain_overflow_dont, 0, "align16", FALSE, 0, 0, FALSE),
61 HOWTO (ALIGNER, 0, 0x3, 0, FALSE, 0, complain_overflow_dont, 0, "align32", FALSE, 0, 0, FALSE),
62 HOWTO (ALIGNER, 0, 0x7, 0, FALSE, 0, complain_overflow_dont, 0, "align64", FALSE, 0, 0, FALSE),
63 HOWTO (ALIGNER, 0, 0xf, 0, FALSE, 0, complain_overflow_dont, 0, "align128", FALSE, 0, 0, FALSE),
66 static reloc_howto_type howto_done_align_table[] =
68 HOWTO (ALIGNDONE, 0x1, 0x1, 0, FALSE, 0, complain_overflow_dont, 0, "donealign16", FALSE, 0, 0, FALSE),
69 HOWTO (ALIGNDONE, 0x3, 0x3, 0, FALSE, 0, complain_overflow_dont, 0, "donealign32", FALSE, 0, 0, FALSE),
70 HOWTO (ALIGNDONE, 0x7, 0x7, 0, FALSE, 0, complain_overflow_dont, 0, "donealign64", FALSE, 0, 0, FALSE),
71 HOWTO (ALIGNDONE, 0xf, 0xf, 0, FALSE, 0, complain_overflow_dont, 0, "donealign128", FALSE, 0, 0, FALSE),
74 /* Swaps the information in an executable header taken from a raw byte
75 stream memory image, into the internal exec_header structure. */
78 bout_swap_exec_header_in (bfd *abfd,
79 struct external_exec *bytes,
80 struct internal_exec *execp)
82 /* Now fill in fields in the execp, from the bytes in the raw data. */
83 execp->a_info = H_GET_32 (abfd, bytes->e_info);
84 execp->a_text = GET_WORD (abfd, bytes->e_text);
85 execp->a_data = GET_WORD (abfd, bytes->e_data);
86 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
87 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
88 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
89 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
90 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
91 execp->a_tload = GET_WORD (abfd, bytes->e_tload);
92 execp->a_dload = GET_WORD (abfd, bytes->e_dload);
93 execp->a_talign = bytes->e_talign[0];
94 execp->a_dalign = bytes->e_dalign[0];
95 execp->a_balign = bytes->e_balign[0];
96 execp->a_relaxable = bytes->e_relaxable[0];
99 /* Swaps the information in an internal exec header structure into the
100 supplied buffer ready for writing to disk. */
103 bout_swap_exec_header_out (bfd *abfd,
104 struct internal_exec *execp,
105 struct external_exec *bytes)
107 /* Now fill in fields in the raw data, from the fields in the exec struct. */
108 H_PUT_32 (abfd, execp->a_info , bytes->e_info);
109 PUT_WORD (abfd, execp->a_text , bytes->e_text);
110 PUT_WORD (abfd, execp->a_data , bytes->e_data);
111 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
112 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
113 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
114 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
115 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
116 PUT_WORD (abfd, execp->a_tload , bytes->e_tload);
117 PUT_WORD (abfd, execp->a_dload , bytes->e_dload);
118 bytes->e_talign[0] = execp->a_talign;
119 bytes->e_dalign[0] = execp->a_dalign;
120 bytes->e_balign[0] = execp->a_balign;
121 bytes->e_relaxable[0] = execp->a_relaxable;
124 /* Finish up the opening of a b.out file for reading. Fill in all the
125 fields that are not handled by common code. */
127 static const bfd_target *
128 b_out_callback (bfd *abfd)
130 struct internal_exec *execp = exec_hdr (abfd);
131 unsigned long bss_start;
133 /* Architecture and machine type. */
134 bfd_set_arch_mach (abfd,
135 bfd_arch_i960, /* B.out only used on i960. */
136 bfd_mach_i960_core /* Default. */
139 /* The positions of the string table and symbol table. */
140 obj_str_filepos (abfd) = N_STROFF (*execp);
141 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
143 /* The alignments of the sections. */
144 obj_textsec (abfd)->alignment_power = execp->a_talign;
145 obj_datasec (abfd)->alignment_power = execp->a_dalign;
146 obj_bsssec (abfd)->alignment_power = execp->a_balign;
148 /* The starting addresses of the sections. */
149 obj_textsec (abfd)->vma = execp->a_tload;
150 obj_datasec (abfd)->vma = execp->a_dload;
152 obj_textsec (abfd)->lma = obj_textsec (abfd)->vma;
153 obj_datasec (abfd)->lma = obj_datasec (abfd)->vma;
155 /* And reload the sizes, since the aout module zaps them. */
156 obj_textsec (abfd)->size = execp->a_text;
158 bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section. */
159 obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign);
161 obj_bsssec (abfd)->lma = obj_bsssec (abfd)->vma;
163 /* The file positions of the sections. */
164 obj_textsec (abfd)->filepos = N_TXTOFF (*execp);
165 obj_datasec (abfd)->filepos = N_DATOFF (*execp);
167 /* The file positions of the relocation info. */
168 obj_textsec (abfd)->rel_filepos = N_TROFF (*execp);
169 obj_datasec (abfd)->rel_filepos = N_DROFF (*execp);
171 adata (abfd).page_size = 1; /* Not applicable. */
172 adata (abfd).segment_size = 1; /* Not applicable. */
173 adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
175 if (execp->a_relaxable)
176 abfd->flags |= BFD_IS_RELAXABLE;
180 static const bfd_target *
181 b_out_object_p (bfd *abfd)
183 struct internal_exec anexec;
184 struct external_exec exec_bytes;
185 bfd_size_type amt = EXEC_BYTES_SIZE;
187 if (bfd_bread ((void *) &exec_bytes, amt, abfd) != amt)
189 if (bfd_get_error () != bfd_error_system_call)
190 bfd_set_error (bfd_error_wrong_format);
194 anexec.a_info = H_GET_32 (abfd, exec_bytes.e_info);
196 if (N_BADMAG (anexec))
198 bfd_set_error (bfd_error_wrong_format);
202 bout_swap_exec_header_in (abfd, &exec_bytes, &anexec);
203 return aout_32_some_aout_object_p (abfd, &anexec, b_out_callback);
206 struct bout_data_struct
209 struct internal_exec e;
213 b_out_mkobject (bfd *abfd)
215 struct bout_data_struct *rawptr;
216 bfd_size_type amt = sizeof (struct bout_data_struct);
218 rawptr = bfd_zalloc (abfd, amt);
222 abfd->tdata.bout_data = rawptr;
223 exec_hdr (abfd) = &rawptr->e;
225 obj_textsec (abfd) = NULL;
226 obj_datasec (abfd) = NULL;
227 obj_bsssec (abfd) = NULL;
233 b_out_symbol_cmp (const void * a_ptr, const void * b_ptr)
235 struct aout_symbol ** a = (struct aout_symbol **) a_ptr;
236 struct aout_symbol ** b = (struct aout_symbol **) b_ptr;
240 /* Primary key is address. */
241 sec = bfd_get_section (&(*a)->symbol);
242 av = sec->output_section->vma + sec->output_offset + (*a)->symbol.value;
243 sec = bfd_get_section (&(*b)->symbol);
244 bv = sec->output_section->vma + sec->output_offset + (*b)->symbol.value;
251 /* Secondary key puts CALLNAME syms last and BALNAME syms first,
252 so that they have the best chance of being contiguous. */
253 if (IS_BALNAME ((*a)->other) || IS_CALLNAME ((*b)->other))
255 if (IS_CALLNAME ((*a)->other) || IS_BALNAME ((*b)->other))
262 b_out_squirt_out_relocs (bfd *abfd, asection *section)
269 unsigned int count = section->reloc_count;
270 struct relocation_info *native, *natptr;
271 bfd_size_type natsize;
272 int extern_mask, pcrel_mask, len_2, callj_mask;
277 generic = section->orelocation;
278 natsize = (bfd_size_type) count * sizeof (struct relocation_info);
279 native = bfd_malloc (natsize);
280 if (!native && natsize != 0)
283 if (bfd_header_big_endian (abfd))
285 /* Big-endian bit field allocation order. */
295 /* Little-endian bit field allocation order. */
304 for (natptr = native; count > 0; --count, ++natptr, ++generic)
306 arelent *g = *generic;
307 unsigned char *raw = (unsigned char *) natptr;
308 asymbol *sym = *(g->sym_ptr_ptr);
309 asection *output_section = sym->section->output_section;
311 H_PUT_32 (abfd, g->address, raw);
312 /* Find a type in the output format which matches the input howto -
313 at the moment we assume input format == output format FIXME!! */
315 /* FIXME: Need callj stuff here, and to check the howto entries to
316 be sure they are real for this architecture. */
317 if (g->howto== &howto_reloc_callj)
318 raw[7] = callj_mask + pcrel_mask + len_2;
319 else if (g->howto == &howto_reloc_pcrel24)
320 raw[7] = pcrel_mask + len_2;
321 else if (g->howto == &howto_reloc_pcrel13)
322 raw[7] = pcrel_mask + len_1;
323 else if (g->howto == &howto_reloc_abs32code)
324 raw[7] = len_2 + incode_mask;
325 else if (g->howto >= howto_align_table
326 && g->howto <= (howto_align_table + ARRAY_SIZE (howto_align_table) - 1))
328 /* symnum == -2; extern_mask not set, pcrel_mask set. */
332 | ((g->howto - howto_align_table) << 1));
338 /* Already mucked with r_extern, r_idx. */;
339 else if (bfd_is_com_section (output_section)
340 || bfd_is_abs_section (output_section)
341 || bfd_is_und_section (output_section))
343 if (bfd_abs_section_ptr->symbol == sym)
345 /* Whoops, looked like an abs symbol, but is really an offset
346 from the abs section. */
352 /* Fill in symbol. */
354 r_idx = (*g->sym_ptr_ptr)->udata.i;
359 /* Just an ordinary section. */
361 r_idx = output_section->target_index;
364 if (bfd_header_big_endian (abfd))
366 raw[4] = (unsigned char) (r_idx >> 16);
367 raw[5] = (unsigned char) (r_idx >> 8);
368 raw[6] = (unsigned char) (r_idx );
372 raw[6] = (unsigned char) (r_idx >> 16);
373 raw[5] = (unsigned char) (r_idx>> 8);
374 raw[4] = (unsigned char) (r_idx );
378 raw[7] |= extern_mask;
381 if (bfd_bwrite ((void *) native, natsize, abfd) != natsize)
393 b_out_write_object_contents (bfd *abfd)
395 struct external_exec swapped_hdr;
398 if (! aout_32_make_sections (abfd))
401 exec_hdr (abfd)->a_info = BMAGIC;
403 exec_hdr (abfd)->a_text = obj_textsec (abfd)->size;
404 exec_hdr (abfd)->a_data = obj_datasec (abfd)->size;
405 exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->size;
406 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * 12;
407 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
408 exec_hdr (abfd)->a_trsize = (obj_textsec (abfd)->reloc_count) * 8;
409 exec_hdr (abfd)->a_drsize = (obj_datasec (abfd)->reloc_count) * 8;
411 exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
412 exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
413 exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
415 exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
416 exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
418 bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
420 amt = EXEC_BYTES_SIZE;
421 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
422 || bfd_bwrite ((void *) &swapped_hdr, amt, abfd) != amt)
425 /* Now write out reloc info, followed by syms and strings */
426 if (bfd_get_symcount (abfd) != 0)
428 /* Make sure {CALL,BAL}NAME symbols remain adjacent on output
429 by sorting. This is complicated by the fact that stabs are
430 also ordered. Solve this by shifting all stabs to the end
431 in order, then sorting the rest. */
433 asymbol **outsyms, **p, **q;
435 outsyms = bfd_get_outsymbols (abfd);
436 p = outsyms + bfd_get_symcount (abfd);
438 for (q = p--; p >= outsyms; p--)
440 if ((*p)->flags & BSF_DEBUGGING)
449 qsort (outsyms, (size_t) (q - outsyms), sizeof (asymbol*),
452 /* Back to your regularly scheduled program. */
453 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*exec_hdr (abfd))), SEEK_SET)
457 if (! aout_32_write_syms (abfd))
460 if (bfd_seek (abfd, (file_ptr) (N_TROFF (*exec_hdr (abfd))), SEEK_SET)
464 if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd)))
466 if (bfd_seek (abfd, (file_ptr) (N_DROFF (*exec_hdr (abfd))), SEEK_SET)
470 if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd)))
476 /* Some reloc hackery. */
478 #define CALLS 0x66003800 /* Template for 'calls' instruction */
479 #define BAL 0x0b000000 /* Template for 'bal' instruction */
480 #define BAL_MASK 0x00ffffff
481 #define BALX 0x85f00000 /* Template for 'balx' instruction */
482 #define BALX_MASK 0x0007ffff
483 #define CALL 0x09000000
484 #define PCREL13_MASK 0x1fff
486 #define output_addr(sec) ((sec)->output_offset+(sec)->output_section->vma)
489 get_value (arelent *reloc,
490 struct bfd_link_info *link_info,
491 asection *input_section)
494 asymbol *symbol = *(reloc->sym_ptr_ptr);
496 /* A symbol holds a pointer to a section, and an offset from the
497 base of the section. To relocate, we find where the section will
498 live in the output and add that in. */
499 if (bfd_is_und_section (symbol->section))
501 struct bfd_link_hash_entry *h;
503 /* The symbol is undefined in this BFD. Look it up in the
504 global linker hash table. FIXME: This should be changed when
505 we convert b.out to use a specific final_link function and
506 change the interface to bfd_relax_section to not require the
508 h = bfd_wrapped_link_hash_lookup (input_section->owner, link_info,
509 bfd_asymbol_name (symbol),
511 if (h != (struct bfd_link_hash_entry *) NULL
512 && (h->type == bfd_link_hash_defined
513 || h->type == bfd_link_hash_defweak))
514 value = h->u.def.value + output_addr (h->u.def.section);
515 else if (h != (struct bfd_link_hash_entry *) NULL
516 && h->type == bfd_link_hash_common)
520 if (! ((*link_info->callbacks->undefined_symbol)
521 (link_info, bfd_asymbol_name (symbol),
522 input_section->owner, input_section, reloc->address,
529 value = symbol->value + output_addr (symbol->section);
531 /* Add the value contained in the relocation. */
532 value += reloc->addend;
537 /* Magic to turn callx into calljx. */
539 static bfd_reloc_status_type
540 calljx_callback (bfd *abfd,
541 struct bfd_link_info *link_info,
542 arelent *reloc_entry,
545 asection *input_section)
547 int word = bfd_get_32 (abfd, src);
548 asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr);
549 aout_symbol_type *symbol = aout_symbol (symbol_in);
552 value = get_value (reloc_entry, link_info, input_section);
554 if (IS_CALLNAME (symbol->other))
556 aout_symbol_type *balsym = symbol+1;
557 int inst = bfd_get_32 (abfd, (bfd_byte *) src-4);
559 /* The next symbol should be an N_BALNAME. */
560 BFD_ASSERT (IS_BALNAME (balsym->other));
563 bfd_put_32 (abfd, (bfd_vma) inst, (bfd_byte *) dst-4);
565 value = (symbol->symbol.value
566 + output_addr (symbol->symbol.section));
569 word += value + reloc_entry->addend;
571 bfd_put_32 (abfd, (bfd_vma) word, dst);
575 /* Magic to turn call into callj. */
577 static bfd_reloc_status_type
578 callj_callback (bfd *abfd,
579 struct bfd_link_info *link_info,
580 arelent *reloc_entry,
584 asection *input_section,
585 bfd_boolean shrinking)
587 int word = bfd_get_32 (abfd, (bfd_byte *) data + srcidx);
588 asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr);
589 aout_symbol_type *symbol = aout_symbol (symbol_in);
592 value = get_value (reloc_entry, link_info, input_section);
594 if (IS_OTHER (symbol->other))
595 /* Call to a system procedure - replace code with system
597 word = CALLS | (symbol->other - 1);
599 else if (IS_CALLNAME (symbol->other))
601 aout_symbol_type *balsym = symbol+1;
603 /* The next symbol should be an N_BALNAME. */
604 BFD_ASSERT (IS_BALNAME (balsym->other));
606 /* We are calling a leaf, so replace the call instruction with a
609 + output_addr (balsym->symbol.section)
610 + balsym->symbol.value + reloc_entry->addend
612 - output_addr (input_section))
615 else if ((symbol->symbol.flags & BSF_SECTION_SYM) != 0)
617 /* A callj against a symbol in the same section is a fully
618 resolved relative call. We don't need to do anything here.
619 If the symbol is not in the same section, I'm not sure what
620 to do; fortunately, this case will probably never arise. */
621 BFD_ASSERT (! shrinking);
622 BFD_ASSERT (symbol->symbol.section == input_section);
625 word = CALL | (((word & BAL_MASK)
627 + reloc_entry->addend
628 - (shrinking ? dstidx : 0)
629 - output_addr (input_section))
632 bfd_put_32 (abfd, (bfd_vma) word, (bfd_byte *) data + dstidx);
636 static reloc_howto_type *
637 b_out_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
638 bfd_reloc_code_real_type code)
644 case BFD_RELOC_I960_CALLJ:
645 return &howto_reloc_callj;
648 return &howto_reloc_abs32;
649 case BFD_RELOC_24_PCREL:
650 return &howto_reloc_pcrel24;
654 static reloc_howto_type *
655 b_out_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
658 if (strcasecmp (howto_reloc_callj.name, r_name) == 0)
659 return &howto_reloc_callj;
660 if (strcasecmp (howto_reloc_abs32.name, r_name) == 0)
661 return &howto_reloc_abs32;
662 if (strcasecmp (howto_reloc_pcrel24.name, r_name) == 0)
663 return &howto_reloc_pcrel24;
668 /* Allocate enough room for all the reloc entries, plus pointers to them all. */
671 b_out_slurp_reloc_table (bfd *abfd, sec_ptr asect, asymbol **symbols)
673 struct relocation_info *rptr;
674 unsigned int counter;
676 int extern_mask, pcrel_mask, callj_mask, length_shift;
679 bfd_vma prev_addr = 0;
681 bfd_size_type reloc_size, amt;
682 struct relocation_info *relocs;
683 arelent *reloc_cache;
685 if (asect->relocation)
688 if (!aout_32_slurp_symbol_table (abfd))
691 if (asect == obj_datasec (abfd))
692 reloc_size = exec_hdr (abfd)->a_drsize;
693 else if (asect == obj_textsec (abfd))
694 reloc_size = exec_hdr (abfd)->a_trsize;
695 else if (asect == obj_bsssec (abfd))
699 bfd_set_error (bfd_error_invalid_operation);
703 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
705 count = reloc_size / sizeof (struct relocation_info);
707 relocs = bfd_malloc (reloc_size);
708 if (!relocs && reloc_size != 0)
711 amt = ((bfd_size_type) count + 1) * sizeof (arelent);
712 reloc_cache = bfd_malloc (amt);
720 if (bfd_bread ((void *) relocs, reloc_size, abfd) != reloc_size)
728 if (bfd_header_big_endian (abfd))
730 /* Big-endian bit field allocation order. */
740 /* Little-endian bit field allocation order. */
749 for (rptr = relocs, cache_ptr = reloc_cache, counter = 0;
751 counter++, rptr++, cache_ptr++)
753 unsigned char *raw = (unsigned char *)rptr;
756 cache_ptr->address = H_GET_32 (abfd, raw + 0);
757 cache_ptr->howto = 0;
759 if (bfd_header_big_endian (abfd))
760 symnum = (raw[4] << 16) | (raw[5] << 8) | raw[6];
762 symnum = (raw[6] << 16) | (raw[5] << 8) | raw[4];
764 if (raw[7] & extern_mask)
766 /* If this is set then the r_index is an index into the symbol table;
767 if the bit is not set then r_index contains a section map.
768 We either fill in the sym entry with a pointer to the symbol,
769 or point to the correct section. */
770 cache_ptr->sym_ptr_ptr = symbols + symnum;
771 cache_ptr->addend = 0;
775 /* In a.out symbols are relative to the beginning of the
776 file rather than sections ?
777 (look in translate_from_native_sym_flags)
778 The reloc entry addend has added to it the offset into the
779 file of the data, so subtract the base to make the reloc
783 /* Sign-extend symnum from 24 bits to whatever host uses. */
788 cache_ptr->sym_ptr_ptr = (asymbol **)NULL;
793 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr;
794 cache_ptr->addend = - obj_textsec (abfd)->vma;
798 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr;
799 cache_ptr->addend = - obj_datasec (abfd)->vma;
803 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr;
804 cache_ptr->addend = - obj_bsssec (abfd)->vma;
808 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr;
809 cache_ptr->addend = 0;
811 case -2: /* .align */
812 if (raw[7] & pcrel_mask)
814 cache_ptr->howto = &howto_align_table[(raw[7] >> length_shift) & 3];
815 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
822 cache_ptr->addend = 0;
830 /* The i960 only has a few relocation types:
831 abs 32-bit and pcrel 24bit. except for callj's! */
832 if (cache_ptr->howto != 0)
834 else if (raw[7] & callj_mask)
836 cache_ptr->howto = &howto_reloc_callj;
838 else if ( raw[7] & pcrel_mask)
840 if (raw[7] & size_mask)
841 cache_ptr->howto = &howto_reloc_pcrel13;
843 cache_ptr->howto = &howto_reloc_pcrel24;
847 if (raw[7] & incode_mask)
848 cache_ptr->howto = &howto_reloc_abs32code;
850 cache_ptr->howto = &howto_reloc_abs32;
853 if (cache_ptr->address < prev_addr)
855 /* Ouch! this reloc is out of order, insert into the right place. */
857 arelent *cursor = cache_ptr-1;
858 bfd_vma stop = cache_ptr->address;
861 while (cursor->address > stop && cursor >= reloc_cache)
863 cursor[1] = cursor[0];
870 prev_addr = cache_ptr->address;
875 asect->relocation = reloc_cache;
876 asect->reloc_count = count;
881 /* This is stupid. This function should be a boolean predicate. */
884 b_out_canonicalize_reloc (bfd *abfd,
892 if ((section->flags & SEC_CONSTRUCTOR) != 0)
894 arelent_chain *chain = section->constructor_chain;
896 for (count = 0; count < section->reloc_count; count++)
898 *relptr++ = &chain->relent;
904 if (section->relocation == NULL
905 && ! b_out_slurp_reloc_table (abfd, section, symbols))
908 tblptr = section->relocation;
909 for (count = 0; count++ < section->reloc_count;)
910 *relptr++ = tblptr++;
915 return section->reloc_count;
919 b_out_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
921 if (bfd_get_format (abfd) != bfd_object)
923 bfd_set_error (bfd_error_invalid_operation);
927 if (asect->flags & SEC_CONSTRUCTOR)
928 return sizeof (arelent *) * (asect->reloc_count + 1);
930 if (asect == obj_datasec (abfd))
931 return (sizeof (arelent *) *
932 ((exec_hdr (abfd)->a_drsize / sizeof (struct relocation_info))
935 if (asect == obj_textsec (abfd))
936 return (sizeof (arelent *) *
937 ((exec_hdr (abfd)->a_trsize / sizeof (struct relocation_info))
940 if (asect == obj_bsssec (abfd))
943 bfd_set_error (bfd_error_invalid_operation);
949 b_out_set_section_contents (bfd *abfd,
951 const void * location,
955 if (! abfd->output_has_begun)
957 /* Set by bfd.c handler. */
958 if (! aout_32_make_sections (abfd))
961 obj_textsec (abfd)->filepos = sizeof (struct external_exec);
962 obj_datasec (abfd)->filepos = obj_textsec (abfd)->filepos
963 + obj_textsec (abfd)->size;
966 /* Regardless, once we know what we're doing, we might as well get going. */
967 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
973 return bfd_bwrite ((void *) location, count, abfd) == count;
977 b_out_set_arch_mach (bfd *abfd,
978 enum bfd_architecture arch,
979 unsigned long machine)
981 bfd_default_set_arch_mach (abfd, arch, machine);
983 if (arch == bfd_arch_unknown) /* Unknown machine arch is OK. */
986 if (arch == bfd_arch_i960) /* i960 default is OK. */
989 case bfd_mach_i960_core:
990 case bfd_mach_i960_kb_sb:
991 case bfd_mach_i960_mc:
992 case bfd_mach_i960_xa:
993 case bfd_mach_i960_ca:
994 case bfd_mach_i960_ka_sa:
995 case bfd_mach_i960_jx:
996 case bfd_mach_i960_hx:
1007 b_out_sizeof_headers (bfd *ignore_abfd ATTRIBUTE_UNUSED,
1008 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1010 return sizeof (struct external_exec);
1014 perform_slip (bfd *abfd,
1016 asection *input_section,
1021 s = _bfd_generic_link_get_symbols (abfd);
1022 BFD_ASSERT (s != (asymbol **) NULL);
1024 /* Find all symbols past this point, and make them know
1030 if (p->section == input_section)
1032 /* This was pointing into this section, so mangle it. */
1033 if (p->value > value)
1037 if (p->udata.p != NULL)
1039 struct generic_link_hash_entry *h;
1041 h = (struct generic_link_hash_entry *) p->udata.p;
1042 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
1043 h->root.u.def.value -= slip;
1044 BFD_ASSERT (h->root.u.def.value == p->value);
1052 /* This routine works out if the thing we want to get to can be
1053 reached with a 24bit offset instead of a 32 bit one.
1054 If it can, then it changes the amode. */
1057 abs32code (bfd *abfd,
1058 asection *input_section,
1060 unsigned int shrink,
1061 struct bfd_link_info *link_info)
1063 bfd_vma value = get_value (r, link_info, input_section);
1064 bfd_vma dot = output_addr (input_section) + r->address;
1067 /* See if the address we're looking at within 2^23 bytes of where
1068 we are, if so then we can use a small branch rather than the
1069 jump we were going to. */
1070 gap = value - (dot - shrink);
1072 if ((long)(-1UL << 23) < (long)gap && (long)gap < 1L << 23)
1074 /* Change the reloc type from 32bitcode possible 24, to 24bit
1076 r->howto = &howto_reloc_abs32codeshrunk;
1077 /* The place to relc moves back by four bytes. */
1080 /* This will be four bytes smaller in the long run. */
1082 perform_slip (abfd, 4, input_section, r->address-shrink + 4);
1089 aligncode (bfd *abfd,
1090 asection *input_section,
1092 unsigned int shrink)
1094 bfd_vma dot = output_addr (input_section) + r->address;
1097 unsigned int shrink_delta;
1098 int size = r->howto->size;
1100 /* Reduce the size of the alignment so that it's still aligned but
1101 smaller - the current size is already the same size as or bigger
1102 than the alignment required. */
1104 /* Calculate the first byte following the padding before we optimize. */
1105 old_end = ((dot + size ) & ~size) + size+1;
1106 /* Work out where the new end will be - remember that we're smaller
1107 than we used to be. */
1108 new_end = ((dot - shrink + size) & ~size);
1110 shrink_delta = (old_end - new_end) - shrink;
1114 /* Change the reloc so that it knows how far to align to. */
1115 r->howto = howto_done_align_table + (r->howto - howto_align_table);
1117 /* Encode the stuff into the addend - for future use we need to
1118 know how big the reloc used to be. */
1119 r->addend = old_end - dot + r->address;
1121 /* This will be N bytes smaller in the long run, adjust all the symbols. */
1122 perform_slip (abfd, shrink_delta, input_section, r->address - shrink);
1123 shrink += shrink_delta;
1130 b_out_bfd_relax_section (bfd *abfd,
1132 struct bfd_link_info *link_info,
1135 /* Get enough memory to hold the stuff. */
1136 bfd *input_bfd = i->owner;
1137 asection *input_section = i;
1138 unsigned int shrink = 0 ;
1139 arelent **reloc_vector = NULL;
1140 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
1142 if (bfd_link_relocatable (link_info))
1143 (*link_info->callbacks->einfo)
1144 (_("%P%F: --relax and -r may not be used together\n"));
1149 /* We only run this relaxation once. It might work to run it
1150 multiple times, but it hasn't been tested. */
1157 reloc_vector = bfd_malloc ((bfd_size_type) reloc_size);
1158 if (reloc_vector == NULL && reloc_size != 0)
1161 /* Get the relocs and think about them. */
1163 bfd_canonicalize_reloc (input_bfd, input_section, reloc_vector,
1164 _bfd_generic_link_get_symbols (input_bfd));
1165 if (reloc_count < 0)
1167 if (reloc_count > 0)
1171 for (parent = reloc_vector; *parent; parent++)
1173 arelent *r = *parent;
1175 switch (r->howto->type)
1178 /* An alignment reloc. */
1179 shrink = aligncode (abfd, input_section, r, shrink);
1182 /* A 32bit reloc in an addressing mode. */
1183 shrink = abs32code (input_bfd, input_section, r, shrink,
1186 case ABS32CODE_SHRUNK:
1193 input_section->size -= shrink;
1195 if (reloc_vector != NULL)
1196 free (reloc_vector);
1199 if (reloc_vector != NULL)
1200 free (reloc_vector);
1205 b_out_bfd_get_relocated_section_contents (bfd *output_bfd,
1206 struct bfd_link_info *link_info,
1207 struct bfd_link_order *link_order,
1209 bfd_boolean relocatable,
1212 /* Get enough memory to hold the stuff. */
1213 bfd *input_bfd = link_order->u.indirect.section->owner;
1214 asection *input_section = link_order->u.indirect.section;
1215 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
1216 arelent **reloc_vector = NULL;
1222 /* If producing relocatable output, don't bother to relax. */
1224 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1229 reloc_vector = bfd_malloc ((bfd_size_type) reloc_size);
1230 if (reloc_vector == NULL && reloc_size != 0)
1233 /* Read in the section. */
1234 BFD_ASSERT (bfd_get_section_contents (input_bfd,
1238 input_section->size));
1240 reloc_count = bfd_canonicalize_reloc (input_bfd,
1244 if (reloc_count < 0)
1246 if (reloc_count > 0)
1248 arelent **parent = reloc_vector;
1250 unsigned int dst_address = 0;
1251 unsigned int src_address = 0;
1255 /* Find how long a run we can do. */
1256 while (dst_address < link_order->size)
1261 /* Note that the relaxing didn't tie up the addresses in the
1262 relocation, so we use the original address to work out the
1263 run of non-relocated data. */
1264 BFD_ASSERT (reloc->address >= src_address);
1265 run = reloc->address - src_address;
1269 run = link_order->size - dst_address;
1271 /* Copy the bytes. */
1272 for (idx = 0; idx < run; idx++)
1273 data[dst_address++] = data[src_address++];
1275 /* Now do the relocation. */
1278 switch (reloc->howto->type)
1281 calljx_callback (input_bfd, link_info, reloc,
1282 src_address + data, dst_address + data,
1288 bfd_put_32 (input_bfd,
1289 (bfd_get_32 (input_bfd, data + src_address)
1290 + get_value (reloc, link_info, input_section)),
1291 data + dst_address);
1296 callj_callback (input_bfd, link_info, reloc, data,
1297 src_address, dst_address, input_section,
1303 BFD_ASSERT (reloc->addend >= src_address);
1304 BFD_ASSERT ((bfd_vma) reloc->addend
1305 <= input_section->size);
1306 src_address = reloc->addend;
1307 dst_address = ((dst_address + reloc->howto->size)
1308 & ~reloc->howto->size);
1310 case ABS32CODE_SHRUNK:
1311 /* This used to be a callx, but we've found out that a
1312 callj will reach, so do the right thing. */
1313 callj_callback (input_bfd, link_info, reloc, data,
1314 src_address + 4, dst_address, input_section,
1321 long int word = bfd_get_32 (input_bfd,
1322 data + src_address);
1325 value = get_value (reloc, link_info, input_section);
1326 word = ((word & ~BAL_MASK)
1327 | (((word & BAL_MASK)
1329 - output_addr (input_section)
1333 bfd_put_32 (input_bfd, (bfd_vma) word, data + dst_address);
1341 long int word = bfd_get_32 (input_bfd,
1342 data + src_address);
1345 value = get_value (reloc, link_info, input_section);
1346 word = ((word & ~PCREL13_MASK)
1347 | (((word & PCREL13_MASK)
1350 - output_addr (input_section))
1353 bfd_put_32 (input_bfd, (bfd_vma) word, data + dst_address);
1365 if (reloc_vector != NULL)
1366 free (reloc_vector);
1369 if (reloc_vector != NULL)
1370 free (reloc_vector);
1375 /* Build the transfer vectors for Big and Little-Endian B.OUT files. */
1377 #define aout_32_find_line _bfd_nosymbols_find_line
1378 #define aout_32_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
1379 #define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
1380 #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info
1381 #define b_out_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1382 #define b_out_bfd_link_add_symbols _bfd_generic_link_add_symbols
1383 #define b_out_bfd_link_just_syms _bfd_generic_link_just_syms
1384 #define b_out_bfd_copy_link_hash_symbol_type \
1385 _bfd_generic_copy_link_hash_symbol_type
1386 #define b_out_bfd_final_link _bfd_generic_final_link
1387 #define b_out_bfd_link_split_section _bfd_generic_link_split_section
1388 #define b_out_bfd_gc_sections bfd_generic_gc_sections
1389 #define b_out_bfd_lookup_section_flags bfd_generic_lookup_section_flags
1390 #define b_out_bfd_merge_sections bfd_generic_merge_sections
1391 #define b_out_bfd_is_group_section bfd_generic_is_group_section
1392 #define b_out_bfd_discard_group bfd_generic_discard_group
1393 #define b_out_section_already_linked _bfd_generic_section_already_linked
1394 #define b_out_bfd_define_common_symbol bfd_generic_define_common_symbol
1395 #define aout_32_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
1397 extern const bfd_target bout_le_vec;
1399 const bfd_target bout_be_vec =
1401 "b.out.big", /* Name. */
1402 bfd_target_aout_flavour,
1403 BFD_ENDIAN_LITTLE, /* Data byte order. */
1404 BFD_ENDIAN_BIG, /* Header byte order. */
1405 (HAS_RELOC | EXEC_P | /* Object flags. */
1406 HAS_LINENO | HAS_DEBUG |
1407 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
1408 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
1409 '_', /* Symbol leading char. */
1410 ' ', /* AR_pad_char. */
1411 16, /* AR_max_namelen. */
1412 0, /* match priority. */
1413 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1414 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1415 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */
1416 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1417 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1418 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
1419 {_bfd_dummy_target, b_out_object_p, /* bfd_check_format. */
1420 bfd_generic_archive_p, _bfd_dummy_target},
1421 {bfd_false, b_out_mkobject, /* bfd_set_format. */
1422 _bfd_generic_mkarchive, bfd_false},
1423 {bfd_false, b_out_write_object_contents, /* bfd_write_contents. */
1424 _bfd_write_archive_contents, bfd_false},
1426 BFD_JUMP_TABLE_GENERIC (aout_32),
1427 BFD_JUMP_TABLE_COPY (_bfd_generic),
1428 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1429 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
1430 BFD_JUMP_TABLE_SYMBOLS (aout_32),
1431 BFD_JUMP_TABLE_RELOCS (b_out),
1432 BFD_JUMP_TABLE_WRITE (b_out),
1433 BFD_JUMP_TABLE_LINK (b_out),
1434 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1441 const bfd_target bout_le_vec =
1443 "b.out.little", /* Name. */
1444 bfd_target_aout_flavour,
1445 BFD_ENDIAN_LITTLE, /* Data byte order. */
1446 BFD_ENDIAN_LITTLE, /* Header byte order. */
1447 (HAS_RELOC | EXEC_P | /* Object flags. */
1448 HAS_LINENO | HAS_DEBUG |
1449 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
1450 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
1451 '_', /* Symbol leading char. */
1452 ' ', /* AR_pad_char. */
1453 16, /* AR_max_namelen. */
1454 0, /* match priority. */
1455 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1456 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1457 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */
1458 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1459 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1460 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Headers. */
1462 {_bfd_dummy_target, b_out_object_p, /* bfd_check_format. */
1463 bfd_generic_archive_p, _bfd_dummy_target},
1464 {bfd_false, b_out_mkobject, /* bfd_set_format. */
1465 _bfd_generic_mkarchive, bfd_false},
1466 {bfd_false, b_out_write_object_contents, /* bfd_write_contents. */
1467 _bfd_write_archive_contents, bfd_false},
1469 BFD_JUMP_TABLE_GENERIC (aout_32),
1470 BFD_JUMP_TABLE_COPY (_bfd_generic),
1471 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1472 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
1473 BFD_JUMP_TABLE_SYMBOLS (aout_32),
1474 BFD_JUMP_TABLE_RELOCS (b_out),
1475 BFD_JUMP_TABLE_WRITE (b_out),
1476 BFD_JUMP_TABLE_LINK (b_out),
1477 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),