1 /* BFD back-end for Intel 960 b.out binaries.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 #include "aout/stab_gnu.h"
30 #include "libaout.h" /* BFD a.out internal data structures */
33 static int aligncode PARAMS ((bfd *abfd, asection *input_section,
34 arelent *r, unsigned int shrink));
35 static void perform_slip PARAMS ((bfd *abfd, unsigned int slip,
36 asection *input_section, bfd_vma value));
37 static boolean b_out_squirt_out_relocs PARAMS ((bfd *abfd, asection *section));
38 static bfd_target *b_out_callback PARAMS ((bfd *));
39 static bfd_reloc_status_type calljx_callback
40 PARAMS ((bfd *, struct bfd_link_info *, arelent *, PTR src, PTR dst,
42 static bfd_reloc_status_type callj_callback
43 PARAMS ((bfd *, struct bfd_link_info *, arelent *, PTR data,
44 unsigned int srcidx, unsigned int dstidx, asection *));
45 static bfd_vma get_value PARAMS ((arelent *, struct bfd_link_info *,
47 static int abs32code PARAMS ((bfd *, asection *, arelent *,
48 unsigned int, struct bfd_link_info *));
49 static boolean b_out_relax_section PARAMS ((bfd *, asection *,
50 struct bfd_link_info *,
52 static bfd_byte *b_out_get_relocated_section_contents
53 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
54 bfd_byte *, boolean, asymbol **));
56 /* Swaps the information in an executable header taken from a raw byte
57 stream memory image, into the internal exec_header structure. */
60 bout_swap_exec_header_in (abfd, raw_bytes, execp)
62 struct external_exec *raw_bytes;
63 struct internal_exec *execp;
65 struct external_exec *bytes = (struct external_exec *)raw_bytes;
67 /* Now fill in fields in the execp, from the bytes in the raw data. */
68 execp->a_info = bfd_h_get_32 (abfd, bytes->e_info);
69 execp->a_text = GET_WORD (abfd, bytes->e_text);
70 execp->a_data = GET_WORD (abfd, bytes->e_data);
71 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
72 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
73 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
74 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
75 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
76 execp->a_tload = GET_WORD (abfd, bytes->e_tload);
77 execp->a_dload = GET_WORD (abfd, bytes->e_dload);
78 execp->a_talign = bytes->e_talign[0];
79 execp->a_dalign = bytes->e_dalign[0];
80 execp->a_balign = bytes->e_balign[0];
81 execp->a_relaxable = bytes->e_relaxable[0];
84 /* Swaps the information in an internal exec header structure into the
85 supplied buffer ready for writing to disk. */
87 PROTO(void, bout_swap_exec_header_out,
89 struct internal_exec *execp,
90 struct external_exec *raw_bytes));
92 bout_swap_exec_header_out (abfd, execp, raw_bytes)
94 struct internal_exec *execp;
95 struct external_exec *raw_bytes;
97 struct external_exec *bytes = (struct external_exec *)raw_bytes;
99 /* Now fill in fields in the raw data, from the fields in the exec struct. */
100 bfd_h_put_32 (abfd, execp->a_info , bytes->e_info);
101 PUT_WORD (abfd, execp->a_text , bytes->e_text);
102 PUT_WORD (abfd, execp->a_data , bytes->e_data);
103 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
104 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
105 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
106 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
107 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
108 PUT_WORD (abfd, execp->a_tload , bytes->e_tload);
109 PUT_WORD (abfd, execp->a_dload , bytes->e_dload);
110 bytes->e_talign[0] = execp->a_talign;
111 bytes->e_dalign[0] = execp->a_dalign;
112 bytes->e_balign[0] = execp->a_balign;
113 bytes->e_relaxable[0] = execp->a_relaxable;
118 b_out_object_p (abfd)
121 struct internal_exec anexec;
122 struct external_exec exec_bytes;
124 if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd)
125 != EXEC_BYTES_SIZE) {
126 bfd_set_error (bfd_error_wrong_format);
130 anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info);
132 if (N_BADMAG (anexec)) {
133 bfd_set_error (bfd_error_wrong_format);
137 bout_swap_exec_header_in (abfd, &exec_bytes, &anexec);
138 return aout_32_some_aout_object_p (abfd, &anexec, b_out_callback);
142 /* Finish up the opening of a b.out file for reading. Fill in all the
143 fields that are not handled by common code. */
146 b_out_callback (abfd)
149 struct internal_exec *execp = exec_hdr (abfd);
150 unsigned long bss_start;
152 /* Architecture and machine type */
153 bfd_set_arch_mach(abfd,
154 bfd_arch_i960, /* B.out only used on i960 */
155 bfd_mach_i960_core /* Default */
158 /* The positions of the string table and symbol table. */
159 obj_str_filepos (abfd) = N_STROFF (*execp);
160 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
162 /* The alignments of the sections */
163 obj_textsec (abfd)->alignment_power = execp->a_talign;
164 obj_datasec (abfd)->alignment_power = execp->a_dalign;
165 obj_bsssec (abfd)->alignment_power = execp->a_balign;
167 /* The starting addresses of the sections. */
168 obj_textsec (abfd)->vma = execp->a_tload;
169 obj_datasec (abfd)->vma = execp->a_dload;
171 /* And reload the sizes, since the aout module zaps them */
172 obj_textsec (abfd)->_raw_size = execp->a_text;
174 bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section */
175 obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign);
177 /* The file positions of the sections */
178 obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
179 obj_datasec (abfd)->filepos = N_DATOFF(*execp);
181 /* The file positions of the relocation info */
182 obj_textsec (abfd)->rel_filepos = N_TROFF(*execp);
183 obj_datasec (abfd)->rel_filepos = N_DROFF(*execp);
185 adata(abfd).page_size = 1; /* Not applicable. */
186 adata(abfd).segment_size = 1; /* Not applicable. */
187 adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
189 if (execp->a_relaxable)
190 abfd->flags |= BFD_IS_RELAXABLE;
194 struct bout_data_struct {
196 struct internal_exec e;
200 b_out_mkobject (abfd)
203 struct bout_data_struct *rawptr;
205 rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct));
206 if (rawptr == NULL) {
207 bfd_set_error (bfd_error_no_memory);
211 abfd->tdata.bout_data = rawptr;
212 exec_hdr (abfd) = &rawptr->e;
214 /* For simplicity's sake we just make all the sections right here. */
215 obj_textsec (abfd) = (asection *)NULL;
216 obj_datasec (abfd) = (asection *)NULL;
217 obj_bsssec (abfd) = (asection *)NULL;
219 bfd_make_section (abfd, ".text");
220 bfd_make_section (abfd, ".data");
221 bfd_make_section (abfd, ".bss");
227 b_out_write_object_contents (abfd)
230 struct external_exec swapped_hdr;
232 exec_hdr (abfd)->a_info = BMAGIC;
234 exec_hdr (abfd)->a_text = obj_textsec (abfd)->_raw_size;
235 exec_hdr (abfd)->a_data = obj_datasec (abfd)->_raw_size;
236 exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->_raw_size;
237 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
238 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
239 exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) *
240 sizeof (struct relocation_info));
241 exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) *
242 sizeof (struct relocation_info));
244 exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
245 exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
246 exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
248 exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
249 exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
251 bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
253 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
254 bfd_write ((PTR) &swapped_hdr, 1, EXEC_BYTES_SIZE, abfd);
256 /* Now write out reloc info, followed by syms and strings */
257 if (bfd_get_symcount (abfd) != 0)
259 bfd_seek (abfd, (file_ptr)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET);
261 if (! aout_32_write_syms (abfd))
264 bfd_seek (abfd, (file_ptr)(N_TROFF(*exec_hdr(abfd))), SEEK_SET);
266 if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd))) return false;
267 bfd_seek (abfd, (file_ptr)(N_DROFF(*exec_hdr(abfd))), SEEK_SET);
269 if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd))) return false;
274 /** Some reloc hackery */
276 #define CALLS 0x66003800 /* Template for 'calls' instruction */
277 #define BAL 0x0b000000 /* Template for 'bal' instruction */
278 #define BALX 0x85000000 /* Template for 'balx' instruction */
279 #define BAL_MASK 0x00ffffff
280 #define CALL 0x09000000
281 #define PCREL13_MASK 0x1fff
284 #define output_addr(sec) ((sec)->output_offset+(sec)->output_section->vma)
286 /* Magic to turn callx into calljx */
287 static bfd_reloc_status_type
288 calljx_callback (abfd, link_info, reloc_entry, src, dst, input_section)
290 struct bfd_link_info *link_info;
291 arelent *reloc_entry;
294 asection *input_section;
296 int word = bfd_get_32 (abfd, src);
297 asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr);
298 aout_symbol_type *symbol = aout_symbol (symbol_in);
301 value = get_value (reloc_entry, link_info, input_section);
303 if (IS_CALLNAME (symbol->other))
305 aout_symbol_type *balsym = symbol+1;
306 int inst = bfd_get_32 (abfd, (bfd_byte *) src-4);
307 /* The next symbol should be an N_BALNAME */
308 BFD_ASSERT (IS_BALNAME (balsym->other));
311 bfd_put_32 (abfd, inst, (bfd_byte *) dst-4);
313 value = (symbol->symbol.value
314 + output_addr (symbol->symbol.section));
317 word += value + reloc_entry->addend;
319 bfd_put_32(abfd, word, dst);
324 /* Magic to turn call into callj */
325 static bfd_reloc_status_type
326 callj_callback (abfd, link_info, reloc_entry, data, srcidx, dstidx,
329 struct bfd_link_info *link_info;
330 arelent *reloc_entry;
334 asection *input_section;
336 int word = bfd_get_32 (abfd, (bfd_byte *) data + srcidx);
337 asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr);
338 aout_symbol_type *symbol = aout_symbol (symbol_in);
341 value = get_value (reloc_entry, link_info, input_section);
343 if (IS_OTHER(symbol->other))
345 /* Call to a system procedure - replace code with system
347 word = CALLS | (symbol->other - 1);
349 else if (IS_CALLNAME(symbol->other))
351 aout_symbol_type *balsym = symbol+1;
353 /* The next symbol should be an N_BALNAME. */
354 BFD_ASSERT(IS_BALNAME(balsym->other));
356 /* We are calling a leaf, so replace the call instruction with a
359 + output_addr (balsym->symbol.section)
360 + balsym->symbol.value + reloc_entry->addend
362 - output_addr (input_section))
367 word = CALL | (((word & BAL_MASK)
369 + reloc_entry->addend
371 - output_addr (input_section))
374 bfd_put_32(abfd, word, (bfd_byte *) data + dstidx);
378 /* type rshift size bitsize pcrel bitpos absolute overflow check*/
381 #define ABS32CODE_SHRUNK 1
386 #define ABS32_MAYBE_RELAXABLE 1
387 #define ABS32_WAS_RELAXABLE 2
391 static reloc_howto_type howto_reloc_callj =
392 HOWTO(CALLJ, 0, 2, 24, true, 0, complain_overflow_signed, 0,"callj", true, 0x00ffffff, 0x00ffffff,false);
393 static reloc_howto_type howto_reloc_abs32 =
394 HOWTO(ABS32, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"abs32", true, 0xffffffff,0xffffffff,false);
395 static reloc_howto_type howto_reloc_pcrel24 =
396 HOWTO(PCREL24, 0, 2, 24, true, 0, complain_overflow_signed,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false);
398 static reloc_howto_type howto_reloc_pcrel13 =
399 HOWTO(PCREL13, 0, 2, 13, true, 0, complain_overflow_signed,0,"pcrel13", true, 0x00001fff,0x00001fff,false);
402 static reloc_howto_type howto_reloc_abs32codeshrunk =
403 HOWTO(ABS32CODE_SHRUNK, 0, 2, 24, true, 0, complain_overflow_signed, 0,"callx->callj", true, 0x00ffffff, 0x00ffffff,false);
405 static reloc_howto_type howto_reloc_abs32code =
406 HOWTO(ABS32CODE, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"callx", true, 0xffffffff,0xffffffff,false);
408 static reloc_howto_type howto_align_table[] = {
409 HOWTO (ALIGNER, 0, 0x1, 0, false, 0, complain_overflow_dont, 0, "align16", false, 0, 0, false),
410 HOWTO (ALIGNER, 0, 0x3, 0, false, 0, complain_overflow_dont, 0, "align32", false, 0, 0, false),
411 HOWTO (ALIGNER, 0, 0x7, 0, false, 0, complain_overflow_dont, 0, "align64", false, 0, 0, false),
412 HOWTO (ALIGNER, 0, 0xf, 0, false, 0, complain_overflow_dont, 0, "align128", false, 0, 0, false),
415 static reloc_howto_type howto_done_align_table[] = {
416 HOWTO (ALIGNDONE, 0x1, 0x1, 0, false, 0, complain_overflow_dont, 0, "donealign16", false, 0, 0, false),
417 HOWTO (ALIGNDONE, 0x3, 0x3, 0, false, 0, complain_overflow_dont, 0, "donealign32", false, 0, 0, false),
418 HOWTO (ALIGNDONE, 0x7, 0x7, 0, false, 0, complain_overflow_dont, 0, "donealign64", false, 0, 0, false),
419 HOWTO (ALIGNDONE, 0xf, 0xf, 0, false, 0, complain_overflow_dont, 0, "donealign128", false, 0, 0, false),
422 static const reloc_howto_type *
423 b_out_reloc_type_lookup (abfd, code)
425 bfd_reloc_code_real_type code;
431 case BFD_RELOC_I960_CALLJ:
432 return &howto_reloc_callj;
434 return &howto_reloc_abs32;
435 case BFD_RELOC_24_PCREL:
436 return &howto_reloc_pcrel24;
440 /* Allocate enough room for all the reloc entries, plus pointers to them all */
443 b_out_slurp_reloc_table (abfd, asect, symbols)
448 register struct relocation_info *rptr;
449 unsigned int counter ;
451 int extern_mask, pcrel_mask, callj_mask, length_shift;
454 bfd_vma prev_addr = 0;
457 struct relocation_info *relocs;
458 arelent *reloc_cache;
460 if (asect->relocation) return true;
461 if (!aout_32_slurp_symbol_table (abfd)) return false;
463 if (asect == obj_datasec (abfd)) {
464 reloc_size = exec_hdr(abfd)->a_drsize;
468 if (asect == obj_textsec (abfd)) {
469 reloc_size = exec_hdr(abfd)->a_trsize;
473 bfd_set_error (bfd_error_invalid_operation);
477 bfd_seek (abfd, (file_ptr)(asect->rel_filepos), SEEK_SET);
478 count = reloc_size / sizeof (struct relocation_info);
480 relocs = (struct relocation_info *) malloc (reloc_size);
481 if (!relocs && reloc_size != 0) {
482 bfd_set_error (bfd_error_no_memory);
485 reloc_cache = (arelent *) malloc ((count+1) * sizeof (arelent));
487 free ((char*)relocs);
488 bfd_set_error (bfd_error_no_memory);
492 if (bfd_read ((PTR) relocs, 1, reloc_size, abfd) != reloc_size) {
500 if (abfd->xvec->header_byteorder_big_p) {
501 /* big-endian bit field allocation order */
509 /* little-endian bit field allocation order */
518 for (rptr = relocs, cache_ptr = reloc_cache, counter = 0;
520 counter++, rptr++, cache_ptr++)
522 unsigned char *raw = (unsigned char *)rptr;
524 cache_ptr->address = bfd_h_get_32 (abfd, raw + 0);
525 cache_ptr->howto = 0;
526 if (abfd->xvec->header_byteorder_big_p)
528 symnum = (raw[4] << 16) | (raw[5] << 8) | raw[6];
532 symnum = (raw[6] << 16) | (raw[5] << 8) | raw[4];
535 if (raw[7] & extern_mask)
537 /* if this is set then the r_index is a index into the symbol table;
538 * if the bit is not set then r_index contains a section map.
539 * we either fill in the sym entry with a pointer to the symbol,
540 * or point to the correct section
542 cache_ptr->sym_ptr_ptr = symbols + symnum;
543 cache_ptr->addend = 0;
546 /* in a.out symbols are relative to the beginning of the
547 * file rather than sections ?
548 * (look in translate_from_native_sym_flags)
549 * the reloc entry addend has added to it the offset into the
550 * file of the data, so subtract the base to make the reloc
551 * section relative */
554 /* sign-extend symnum from 24 bits to whatever host uses */
559 cache_ptr->sym_ptr_ptr = (asymbol **)NULL;
564 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr;
565 cache_ptr->addend = - obj_textsec(abfd)->vma;
569 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr;
570 cache_ptr->addend = - obj_datasec(abfd)->vma;
574 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr;
575 cache_ptr->addend = - obj_bsssec(abfd)->vma;
579 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr;
580 cache_ptr->addend = 0;
582 case -2: /* .align */
583 if (raw[7] & pcrel_mask)
585 cache_ptr->howto = &howto_align_table[(raw[7] >> length_shift) & 3];
586 cache_ptr->sym_ptr_ptr = &bfd_abs_symbol;
593 cache_ptr->addend = 0;
602 /* the i960 only has a few relocation types:
603 abs 32-bit and pcrel 24bit. except for callj's! */
604 if (cache_ptr->howto != 0)
606 else if (raw[7] & callj_mask)
608 cache_ptr->howto = &howto_reloc_callj;
610 else if ( raw[7] & pcrel_mask)
612 if (raw[7] & size_mask)
613 cache_ptr->howto = &howto_reloc_pcrel13;
615 cache_ptr->howto = &howto_reloc_pcrel24;
619 if (raw[7] & incode_mask)
621 cache_ptr->howto = &howto_reloc_abs32code;
625 cache_ptr->howto = &howto_reloc_abs32;
628 if (cache_ptr->address < prev_addr)
630 /* Ouch! this reloc is out of order, insert into the right place
633 arelent *cursor = cache_ptr-1;
634 bfd_vma stop = cache_ptr->address;
636 while (cursor->address > stop && cursor >= reloc_cache)
638 cursor[1] = cursor[0];
645 prev_addr = cache_ptr->address;
651 asect->relocation = reloc_cache;
652 asect->reloc_count = count;
660 b_out_squirt_out_relocs (abfd, section)
669 unsigned int count = section->reloc_count;
670 struct relocation_info *native, *natptr;
671 size_t natsize = count * sizeof (struct relocation_info);
672 int extern_mask, pcrel_mask, len_2, callj_mask;
673 if (count == 0) return true;
674 generic = section->orelocation;
675 native = ((struct relocation_info *) malloc (natsize));
676 if (!native && natsize != 0) {
677 bfd_set_error (bfd_error_no_memory);
681 if (abfd->xvec->header_byteorder_big_p)
683 /* Big-endian bit field allocation order */
693 /* Little-endian bit field allocation order */
702 for (natptr = native; count > 0; --count, ++natptr, ++generic)
704 arelent *g = *generic;
705 unsigned char *raw = (unsigned char *)natptr;
706 asymbol *sym = *(g->sym_ptr_ptr);
708 asection *output_section = sym->section->output_section;
710 bfd_h_put_32(abfd, g->address, raw);
711 /* Find a type in the output format which matches the input howto -
712 * at the moment we assume input format == output format FIXME!!
715 /* FIXME: Need callj stuff here, and to check the howto entries to
716 be sure they are real for this architecture. */
717 if (g->howto== &howto_reloc_callj)
719 raw[7] = callj_mask + pcrel_mask + len_2;
721 else if (g->howto == &howto_reloc_pcrel24)
723 raw[7] = pcrel_mask + len_2;
725 else if (g->howto == &howto_reloc_pcrel13)
727 raw[7] = pcrel_mask + len_1;
729 else if (g->howto == &howto_reloc_abs32code)
731 raw[7] = len_2 + incode_mask;
733 else if (g->howto >= howto_align_table
734 && g->howto <= (howto_align_table
735 + sizeof (howto_align_table) / sizeof (howto_align_table[0])
738 /* symnum == -2; extern_mask not set, pcrel_mask set */
742 | ((g->howto - howto_align_table) << 1));
749 /* already mucked with r_extern, r_idx */;
750 else if (bfd_is_com_section (output_section)
751 || output_section == &bfd_abs_section
752 || output_section == &bfd_und_section)
755 if (bfd_abs_section.symbol == sym)
757 /* Whoops, looked like an abs symbol, but is really an offset
758 from the abs section */
767 r_idx = stoi((*(g->sym_ptr_ptr))->flags);
772 /* Just an ordinary section */
774 r_idx = output_section->target_index;
777 if (abfd->xvec->header_byteorder_big_p) {
778 raw[4] = (unsigned char) (r_idx >> 16);
779 raw[5] = (unsigned char) (r_idx >> 8);
780 raw[6] = (unsigned char) (r_idx );
782 raw[6] = (unsigned char) (r_idx >> 16);
783 raw[5] = (unsigned char) (r_idx>> 8);
784 raw[4] = (unsigned char) (r_idx );
787 raw[7] |= extern_mask;
790 if (bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
799 /* This is stupid. This function should be a boolean predicate */
801 b_out_canonicalize_reloc (abfd, section, relptr, symbols)
807 arelent *tblptr = section->relocation;
808 unsigned int count = 0;
810 if (!(tblptr || b_out_slurp_reloc_table (abfd, section, symbols))) return 0;
811 tblptr = section->relocation;
812 if (!tblptr) return 0;
814 for (; count++ < section->reloc_count;)
815 *relptr++ = tblptr++;
819 return section->reloc_count;
823 b_out_get_reloc_upper_bound (abfd, asect)
827 if (bfd_get_format (abfd) != bfd_object) {
828 bfd_set_error (bfd_error_invalid_operation);
832 if (asect == obj_datasec (abfd))
833 return (sizeof (arelent *) *
834 ((exec_hdr(abfd)->a_drsize / sizeof (struct relocation_info))
837 if (asect == obj_textsec (abfd))
838 return (sizeof (arelent *) *
839 ((exec_hdr(abfd)->a_trsize / sizeof (struct relocation_info))
842 if (asect == obj_bsssec (abfd))
845 bfd_set_error (bfd_error_invalid_operation);
850 b_out_set_section_contents (abfd, section, location, offset, count)
853 unsigned char *location;
858 if (abfd->output_has_begun == false) { /* set by bfd.c handler */
859 if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL) /*||
860 (obj_textsec (abfd)->_cooked_size == 0) || (obj_datasec (abfd)->_cooked_size == 0)*/) {
861 bfd_set_error (bfd_error_invalid_operation);
865 obj_textsec (abfd)->filepos = sizeof(struct internal_exec);
866 obj_datasec(abfd)->filepos = obj_textsec(abfd)->filepos
867 + obj_textsec (abfd)->_raw_size;
870 /* regardless, once we know what we're doing, we might as well get going */
871 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
874 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false;
880 b_out_set_arch_mach (abfd, arch, machine)
882 enum bfd_architecture arch;
883 unsigned long machine;
885 bfd_default_set_arch_mach(abfd, arch, machine);
887 if (arch == bfd_arch_unknown) /* Unknown machine arch is OK */
889 if (arch == bfd_arch_i960) /* i960 default is OK */
891 case bfd_mach_i960_core:
892 case bfd_mach_i960_kb_sb:
893 case bfd_mach_i960_mc:
894 case bfd_mach_i960_xa:
895 case bfd_mach_i960_ca:
896 case bfd_mach_i960_ka_sa:
907 b_out_sizeof_headers (ignore_abfd, ignore)
911 return sizeof(struct internal_exec);
916 /************************************************************************/
918 get_value (reloc, link_info, input_section)
920 struct bfd_link_info *link_info;
921 asection *input_section;
924 asymbol *symbol = *(reloc->sym_ptr_ptr);
926 /* A symbol holds a pointer to a section, and an offset from the
927 base of the section. To relocate, we find where the section will
928 live in the output and add that in */
930 if (symbol->section == &bfd_und_section)
932 struct bfd_link_hash_entry *h;
934 /* The symbol is undefined in this BFD. Look it up in the
935 global linker hash table. FIXME: This should be changed when
936 we convert b.out to use a specific final_link function and
937 change the interface to bfd_relax_section to not require the
939 h = bfd_link_hash_lookup (link_info->hash, bfd_asymbol_name (symbol),
941 if (h != (struct bfd_link_hash_entry *) NULL
942 && h->type == bfd_link_hash_defined)
943 value = h->u.def.value + output_addr (h->u.def.section);
944 else if (h != (struct bfd_link_hash_entry *) NULL
945 && h->type == bfd_link_hash_common)
949 if (! ((*link_info->callbacks->undefined_symbol)
950 (link_info, bfd_asymbol_name (symbol),
951 input_section->owner, input_section, reloc->address)))
958 value = symbol->value + output_addr (symbol->section);
961 /* Add the value contained in the relocation */
962 value += reloc->addend;
968 perform_slip (abfd, slip, input_section, value)
971 asection *input_section;
976 s = _bfd_generic_link_get_symbols (abfd);
977 BFD_ASSERT (s != (asymbol **) NULL);
979 /* Find all symbols past this point, and make them know
984 if (p->section == input_section)
986 /* This was pointing into this section, so mangle it */
987 if (p->value > value)
990 if (p->udata != NULL)
992 struct generic_link_hash_entry *h;
994 h = (struct generic_link_hash_entry *) p->udata;
995 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
996 h->root.u.def.value -= slip;
997 BFD_ASSERT (h->root.u.def.value == p->value);
1006 /* This routine works out if the thing we want to get to can be
1007 reached with a 24bit offset instead of a 32 bit one.
1008 If it can, then it changes the amode */
1011 abs32code (abfd, input_section, r, shrink, link_info)
1013 asection *input_section;
1015 unsigned int shrink;
1016 struct bfd_link_info *link_info;
1018 bfd_vma value = get_value (r, link_info, input_section);
1019 bfd_vma dot = output_addr (input_section) + r->address;
1022 /* See if the address we're looking at within 2^23 bytes of where
1023 we are, if so then we can use a small branch rather than the
1024 jump we were going to */
1026 gap = value - (dot - shrink);
1029 if (-1<<23 < (long)gap && (long)gap < 1<<23 )
1031 /* Change the reloc type from 32bitcode possible 24, to 24bit
1034 r->howto = &howto_reloc_abs32codeshrunk;
1035 /* The place to relc moves back by four bytes */
1038 /* This will be four bytes smaller in the long run */
1040 perform_slip (abfd, 4, input_section, r->address-shrink + 4);
1046 aligncode (abfd, input_section, r, shrink)
1048 asection *input_section;
1050 unsigned int shrink;
1052 bfd_vma dot = output_addr (input_section) + r->address;
1057 int size = r->howto->size;
1059 /* Reduce the size of the alignment so that it's still aligned but
1060 smaller - the current size is already the same size as or bigger
1061 than the alignment required. */
1063 /* calculate the first byte following the padding before we optimize */
1064 old_end = ((dot + size ) & ~size) + size+1;
1065 /* work out where the new end will be - remember that we're smaller
1066 than we used to be */
1067 new_end = ((dot - shrink + size) & ~size);
1069 /* This is the new end */
1070 gap = old_end - ((dot + size) & ~size);
1072 shrink_delta = (old_end - new_end) - shrink;
1076 /* Change the reloc so that it knows how far to align to */
1077 r->howto = howto_done_align_table + (r->howto - howto_align_table);
1079 /* Encode the stuff into the addend - for future use we need to
1080 know how big the reloc used to be */
1081 r->addend = old_end - dot + r->address;
1083 /* This will be N bytes smaller in the long run, adjust all the symbols */
1084 perform_slip (abfd, shrink_delta, input_section, r->address - shrink);
1085 shrink += shrink_delta;
1091 b_out_relax_section (abfd, i, link_info, again)
1094 struct bfd_link_info *link_info;
1097 /* Get enough memory to hold the stuff */
1098 bfd *input_bfd = i->owner;
1099 asection *input_section = i;
1101 arelent **reloc_vector = NULL;
1102 bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
1105 /* We only run this relaxation once. It might work to run it
1106 multiple times, but it hasn't been tested. */
1111 reloc_vector = (arelent **) malloc (reloc_size);
1112 if (reloc_vector == NULL && reloc_size != 0)
1114 bfd_set_error (bfd_error_no_memory);
1118 /* Get the relocs and think about them */
1119 if (bfd_canonicalize_reloc(input_bfd, input_section, reloc_vector,
1120 _bfd_generic_link_get_symbols (input_bfd)))
1123 for (parent = reloc_vector; *parent; parent++)
1125 arelent *r = *parent;
1126 switch (r->howto->type)
1129 /* An alignment reloc */
1130 shrink = aligncode (abfd, input_section, r, shrink);
1133 /* A 32bit reloc in an addressing mode */
1134 shrink = abs32code (input_bfd, input_section, r, shrink,
1137 case ABS32CODE_SHRUNK:
1144 input_section->_cooked_size = input_section->_raw_size - shrink;
1146 if (reloc_vector != NULL)
1147 free (reloc_vector);
1150 if (reloc_vector != NULL)
1151 free (reloc_vector);
1156 b_out_get_relocated_section_contents (in_abfd, link_info, link_order, data,
1157 relocateable, symbols)
1159 struct bfd_link_info *link_info;
1160 struct bfd_link_order *link_order;
1162 boolean relocateable;
1165 /* Get enough memory to hold the stuff */
1166 bfd *input_bfd = link_order->u.indirect.section->owner;
1167 asection *input_section = link_order->u.indirect.section;
1168 bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd,
1170 arelent **reloc_vector = NULL;
1172 /* If producing relocateable output, don't bother to relax. */
1174 return bfd_generic_get_relocated_section_contents (in_abfd, link_info,
1179 reloc_vector = (arelent **) malloc (reloc_size);
1180 if (reloc_vector == NULL && reloc_size != 0)
1182 bfd_set_error (bfd_error_no_memory);
1186 input_section->reloc_done = 1;
1188 /* read in the section */
1189 BFD_ASSERT (true == bfd_get_section_contents(input_bfd,
1193 input_section->_raw_size));
1195 if (bfd_canonicalize_reloc(input_bfd,
1200 arelent **parent = reloc_vector;
1203 unsigned int dst_address = 0;
1204 unsigned int src_address = 0;
1208 /* Find how long a run we can do */
1209 while (dst_address < link_order->size)
1214 /* Note that the relaxing didn't tie up the addresses in the
1215 relocation, so we use the original address to work out the
1216 run of non-relocated data */
1217 BFD_ASSERT (reloc->address >= src_address);
1218 run = reloc->address - src_address;
1223 run = link_order->size - dst_address;
1225 /* Copy the bytes */
1226 for (idx = 0; idx < run; idx++)
1228 data[dst_address++] = data[src_address++];
1231 /* Now do the relocation */
1235 switch (reloc->howto->type)
1238 calljx_callback (in_abfd, link_info, reloc,
1239 src_address + data, dst_address + data,
1246 (bfd_get_32 (in_abfd, data+src_address)
1247 + get_value (reloc, link_info, input_section)),
1253 callj_callback (in_abfd, link_info, reloc, data, src_address,
1254 dst_address, input_section);
1259 BFD_ASSERT (reloc->addend >= src_address);
1260 BFD_ASSERT (reloc->addend <= input_section->_raw_size);
1261 src_address = reloc->addend;
1262 dst_address = ((dst_address + reloc->howto->size)
1263 & ~reloc->howto->size);
1265 case ABS32CODE_SHRUNK:
1266 /* This used to be a callx, but we've found out that a
1267 callj will reach, so do the right thing. */
1268 callj_callback (in_abfd, link_info, reloc, data,
1269 src_address + 4, dst_address, input_section);
1275 long int word = bfd_get_32(in_abfd, data+src_address);
1278 value = get_value (reloc, link_info, input_section);
1279 word = ((word & ~BAL_MASK)
1280 | (((word & BAL_MASK)
1282 - output_addr (input_section)
1286 bfd_put_32(in_abfd,word, data+dst_address);
1295 long int word = bfd_get_32(in_abfd, data+src_address);
1298 value = get_value (reloc, link_info, input_section);
1299 word = ((word & ~PCREL13_MASK)
1300 | (((word & PCREL13_MASK)
1303 - output_addr (input_section))
1306 bfd_put_32(in_abfd,word, data+dst_address);
1319 if (reloc_vector != NULL)
1320 free (reloc_vector);
1323 if (reloc_vector != NULL)
1324 free (reloc_vector);
1327 /***********************************************************************/
1329 /* Build the transfer vectors for Big and Little-Endian B.OUT files. */
1331 /* We don't have core files. */
1332 #define aout_32_core_file_failing_command _bfd_dummy_core_file_failing_command
1333 #define aout_32_core_file_failing_signal _bfd_dummy_core_file_failing_signal
1334 #define aout_32_core_file_matches_executable_p \
1335 _bfd_dummy_core_file_matches_executable_p
1337 /* We use BSD-Unix generic archive files. */
1338 #define aout_32_openr_next_archived_file bfd_generic_openr_next_archived_file
1339 #define aout_32_generic_stat_arch_elt bfd_generic_stat_arch_elt
1340 #define aout_32_slurp_armap bfd_slurp_bsd_armap
1341 #define aout_32_slurp_extended_name_table _bfd_slurp_extended_name_table
1342 #define aout_32_write_armap bsd_write_armap
1343 #define aout_32_truncate_arname bfd_bsd_truncate_arname
1345 /* We override these routines from the usual a.out file routines. */
1346 #define aout_32_canonicalize_reloc b_out_canonicalize_reloc
1347 #define aout_32_get_reloc_upper_bound b_out_get_reloc_upper_bound
1348 #define aout_32_set_section_contents b_out_set_section_contents
1349 #define aout_32_set_arch_mach b_out_set_arch_mach
1350 #define aout_32_sizeof_headers b_out_sizeof_headers
1352 #define aout_32_bfd_debug_info_start bfd_void
1353 #define aout_32_bfd_debug_info_end bfd_void
1354 #define aout_32_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
1356 #define aout_32_bfd_get_relocated_section_contents b_out_get_relocated_section_contents
1357 #define aout_32_bfd_relax_section b_out_relax_section
1358 #define aout_32_bfd_reloc_type_lookup b_out_reloc_type_lookup
1359 #define aout_32_bfd_make_debug_symbol \
1360 ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
1361 #define aout_32_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1362 #define aout_32_bfd_link_add_symbols _bfd_generic_link_add_symbols
1363 #define aout_32_bfd_final_link _bfd_generic_final_link
1365 bfd_target b_out_vec_big_host =
1367 "b.out.big", /* name */
1368 bfd_target_aout_flavour,
1369 false, /* data byte order is little */
1370 true, /* hdr byte order is big */
1371 (HAS_RELOC | EXEC_P | /* object flags */
1372 HAS_LINENO | HAS_DEBUG |
1373 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
1374 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1375 '_', /* symbol leading char */
1376 ' ', /* ar_pad_char */
1377 16, /* ar_max_namelen */
1378 2, /* minumum alignment power */
1380 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1381 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1382 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1383 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1384 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1385 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1386 {_bfd_dummy_target, b_out_object_p, /* bfd_check_format */
1387 bfd_generic_archive_p, _bfd_dummy_target},
1388 {bfd_false, b_out_mkobject, /* bfd_set_format */
1389 _bfd_generic_mkarchive, bfd_false},
1390 {bfd_false, b_out_write_object_contents, /* bfd_write_contents */
1391 _bfd_write_archive_contents, bfd_false},
1393 JUMP_TABLE(aout_32),
1398 bfd_target b_out_vec_little_host =
1400 "b.out.little", /* name */
1401 bfd_target_aout_flavour,
1402 false, /* data byte order is little */
1403 false, /* header byte order is little */
1404 (HAS_RELOC | EXEC_P | /* object flags */
1405 HAS_LINENO | HAS_DEBUG |
1406 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE ),
1407 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1408 '_', /* symbol leading char */
1409 ' ', /* ar_pad_char */
1410 16, /* ar_max_namelen */
1411 2, /* minum align */
1412 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1413 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1414 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1415 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1416 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1417 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
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},
1425 JUMP_TABLE(aout_32),