📄 bout.c
字号:
/* BFD back-end for Intel 960 b.out binaries. Copyright 1990, 1991, 1992 Free Software Foundation, Inc. Written by Cygnus Support.This file is part of BFD, the Binary File Descriptor library.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include "bfd.h"#include "sysdep.h"#include "libbfd.h"#include "seclet.h"#include "bout.h"#include "aout/stab_gnu.h"#include "libaout.h" /* BFD a.out internal data structures */extern bfd_error_vector_type bfd_error_vector;PROTO (static boolean, b_out_squirt_out_relocs,(bfd *abfd, asection *section));PROTO (static bfd_target *, b_out_callback, (bfd *));PROTO (boolean, aout_32_slurp_symbol_table, (bfd *abfd));PROTO (void , aout_32_write_syms, ());/* Swaps the information in an executable header taken from a raw byte stream memory image, into the internal exec_header structure. */PROTO(void, bout_swap_exec_header_in, (bfd *abfd, struct external_exec *raw_bytes, struct internal_exec *execp)); voidDEFUN(bout_swap_exec_header_in,(abfd, raw_bytes, execp), bfd *abfd AND struct external_exec *raw_bytes AND struct internal_exec *execp){ struct external_exec *bytes = (struct external_exec *)raw_bytes; /* Now fill in fields in the execp, from the bytes in the raw data. */ execp->a_info = bfd_h_get_32 (abfd, bytes->e_info); execp->a_text = GET_WORD (abfd, bytes->e_text); execp->a_data = GET_WORD (abfd, bytes->e_data); execp->a_bss = GET_WORD (abfd, bytes->e_bss); execp->a_syms = GET_WORD (abfd, bytes->e_syms); execp->a_entry = GET_WORD (abfd, bytes->e_entry); execp->a_trsize = GET_WORD (abfd, bytes->e_trsize); execp->a_drsize = GET_WORD (abfd, bytes->e_drsize); execp->a_tload = GET_WORD (abfd, bytes->e_tload); execp->a_dload = GET_WORD (abfd, bytes->e_dload); execp->a_talign = bytes->e_talign[0]; execp->a_dalign = bytes->e_dalign[0]; execp->a_balign = bytes->e_balign[0]; execp->a_relaxable = bytes->e_relaxable[0];}/* Swaps the information in an internal exec header structure into the supplied buffer ready for writing to disk. */PROTO(void, bout_swap_exec_header_out, (bfd *abfd, struct internal_exec *execp, struct external_exec *raw_bytes));voidDEFUN(bout_swap_exec_header_out,(abfd, execp, raw_bytes), bfd *abfd AND struct internal_exec *execp AND struct external_exec *raw_bytes){ struct external_exec *bytes = (struct external_exec *)raw_bytes; /* Now fill in fields in the raw data, from the fields in the exec struct. */ bfd_h_put_32 (abfd, execp->a_info , bytes->e_info); PUT_WORD (abfd, execp->a_text , bytes->e_text); PUT_WORD (abfd, execp->a_data , bytes->e_data); PUT_WORD (abfd, execp->a_bss , bytes->e_bss); PUT_WORD (abfd, execp->a_syms , bytes->e_syms); PUT_WORD (abfd, execp->a_entry , bytes->e_entry); PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize); PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize); PUT_WORD (abfd, execp->a_tload , bytes->e_tload); PUT_WORD (abfd, execp->a_dload , bytes->e_dload); bytes->e_talign[0] = execp->a_talign; bytes->e_dalign[0] = execp->a_dalign; bytes->e_balign[0] = execp->a_balign; bytes->e_relaxable[0] = execp->a_relaxable;}static bfd_target *b_out_object_p (abfd) bfd *abfd;{ struct internal_exec anexec; struct external_exec exec_bytes; if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd) != EXEC_BYTES_SIZE) { bfd_error = wrong_format; return 0; } anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info); if (N_BADMAG (anexec)) { bfd_error = wrong_format; return 0; } bout_swap_exec_header_in (abfd, &exec_bytes, &anexec); return aout_32_some_aout_object_p (abfd, &anexec, b_out_callback);}/* Finish up the opening of a b.out file for reading. Fill in all the fields that are not handled by common code. */static bfd_target *b_out_callback (abfd) bfd *abfd;{ struct internal_exec *execp = exec_hdr (abfd); unsigned long bss_start; /* Architecture and machine type */ bfd_set_arch_mach(abfd, bfd_arch_i960, /* B.out only used on i960 */ bfd_mach_i960_core /* Default */ ); /* The positions of the string table and symbol table. */ obj_str_filepos (abfd) = N_STROFF (*execp); obj_sym_filepos (abfd) = N_SYMOFF (*execp); /* The alignments of the sections */ obj_textsec (abfd)->alignment_power = execp->a_talign; obj_datasec (abfd)->alignment_power = execp->a_dalign; obj_bsssec (abfd)->alignment_power = execp->a_balign; /* The starting addresses of the sections. */ obj_textsec (abfd)->vma = execp->a_tload; obj_datasec (abfd)->vma = execp->a_dload; /* And reload the sizes, since the aout module zaps them */ obj_textsec (abfd)->_raw_size = execp->a_text; bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section */ obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign); /* The file positions of the sections */ obj_textsec (abfd)->filepos = N_TXTOFF(*execp); obj_datasec (abfd)->filepos = N_DATOFF(*execp); /* The file positions of the relocation info */ obj_textsec (abfd)->rel_filepos = N_TROFF(*execp); obj_datasec (abfd)->rel_filepos = N_DROFF(*execp); adata(abfd).page_size = 1; /* Not applicable. */ adata(abfd).segment_size = 1; /* Not applicable. */ adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE; if (execp->a_relaxable) abfd->flags |= BFD_IS_RELAXABLE; return abfd->xvec;}struct bout_data_struct { struct aoutdata a; struct internal_exec e;};static booleanb_out_mkobject (abfd) bfd *abfd;{ struct bout_data_struct *rawptr; rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct)); if (rawptr == NULL) { bfd_error = no_memory; return false; } abfd->tdata.bout_data = rawptr; exec_hdr (abfd) = &rawptr->e; /* For simplicity's sake we just make all the sections right here. */ obj_textsec (abfd) = (asection *)NULL; obj_datasec (abfd) = (asection *)NULL; obj_bsssec (abfd) = (asection *)NULL; bfd_make_section (abfd, ".text"); bfd_make_section (abfd, ".data"); bfd_make_section (abfd, ".bss"); return true;}static booleanb_out_write_object_contents (abfd) bfd *abfd;{ struct external_exec swapped_hdr; exec_hdr (abfd)->a_info = BMAGIC; exec_hdr (abfd)->a_text = obj_textsec (abfd)->_raw_size; exec_hdr (abfd)->a_data = obj_datasec (abfd)->_raw_size; exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->_raw_size; exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist); exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd); exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) * sizeof (struct relocation_info)); exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) * sizeof (struct relocation_info)); exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power; exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power; exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power; exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma; exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma; bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr); bfd_seek (abfd, (file_ptr) 0, SEEK_SET); bfd_write ((PTR) &swapped_hdr, 1, EXEC_BYTES_SIZE, abfd); /* Now write out reloc info, followed by syms and strings */ if (bfd_get_symcount (abfd) != 0) { bfd_seek (abfd, (file_ptr)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET); aout_32_write_syms (abfd); bfd_seek (abfd, (file_ptr)(N_TROFF(*exec_hdr(abfd))), SEEK_SET); if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd))) return false; bfd_seek (abfd, (file_ptr)(N_DROFF(*exec_hdr(abfd))), SEEK_SET); if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd))) return false; } return true;}/** Some reloc hackery */#define CALLS 0x66003800 /* Template for 'calls' instruction */#define BAL 0x0b000000 /* Template for 'bal' instruction */#define BALX 0x85000000 /* Template for 'balx' instruction */#define BAL_MASK 0x00ffffff#define CALL 0x09000000#define PCREL13_MASK 0x1fff/* Magic to turn callx into calljx */static bfd_reloc_status_type DEFUN (calljx_callback, (abfd, reloc_entry, src, dst, input_section), bfd *abfd AND arelent *reloc_entry AND PTR src AND PTR dst AND asection *input_section){ int word = bfd_get_32(abfd, src); asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr); aout_symbol_type *symbol = aout_symbol(symbol_in); if (IS_CALLNAME(symbol->other)) { aout_symbol_type *balsym = symbol+1; int inst = bfd_get_32(abfd, (bfd_byte *) src-4); /* The next symbol should be an N_BALNAME */ BFD_ASSERT(IS_BALNAME(balsym->other)); inst &= BAL_MASK; inst |= BALX; bfd_put_32(abfd, inst, (bfd_byte *) dst-4); symbol = balsym; } word += symbol->symbol.section->output_offset + symbol->symbol.section->output_section->vma + symbol->symbol.value + reloc_entry->addend; bfd_put_32(abfd, word, dst); return bfd_reloc_ok;}/* Magic to turn call into callj */static bfd_reloc_status_type DEFUN (callj_callback, (abfd, reloc_entry, data, srcidx,dstidx, input_section), bfd *abfd AND arelent *reloc_entry AND PTR data AND unsigned int srcidx AND unsigned int dstidx AND asection *input_section ){ int word = bfd_get_32(abfd, (bfd_byte *) data + srcidx); asymbol *symbol_in = *(reloc_entry->sym_ptr_ptr); aout_symbol_type *symbol = aout_symbol(symbol_in); if (IS_OTHER(symbol->other)) { /* Call to a system procedure - replace code with system procedure number */ word = CALLS | (symbol->other - 1); } else if (IS_CALLNAME(symbol->other)) { aout_symbol_type *balsym = symbol+1; /* The next symbol should be an N_BALNAME */ BFD_ASSERT(IS_BALNAME(balsym->other)); /* We are calling a leaf - so replace the call instruction with a bal */ word = BAL | (((word & BAL_MASK) + balsym->symbol.section->output_offset + balsym->symbol.section->output_section->vma+ balsym->symbol.value + reloc_entry->addend - dstidx - ( input_section->output_section->vma + input_section->output_offset)) & BAL_MASK); } else { word = CALL | (((word & BAL_MASK) + symbol->symbol.section->output_offset + symbol->symbol.section->output_section->vma+ symbol->symbol.value + reloc_entry->addend - dstidx - ( input_section->output_section->vma + input_section->output_offset)) & BAL_MASK); } bfd_put_32(abfd, word, (bfd_byte *) data + dstidx); return bfd_reloc_ok;}/* type rshift size bitsize pcrel bitpos absolute overflow check*/#define ABS32CODE 0#define ABS32CODE_SHRUNK 1 #define PCREL24 2#define CALLJ 3#define ABS32 4#define PCREL13 5#define ABS32_MAYBE_RELAXABLE 1#define ABS32_WAS_RELAXABLE 2#define ALIGNER 10#define ALIGNDONE 11static reloc_howto_type howto_reloc_callj =HOWTO(CALLJ, 0, 2, 24, true, 0, true, true, 0,"callj", true, 0x00ffffff, 0x00ffffff,false);static reloc_howto_type howto_reloc_abs32 =HOWTO(ABS32, 0, 2, 32, false, 0, true, true,0,"abs32", true, 0xffffffff,0xffffffff,false);static reloc_howto_type howto_reloc_pcrel24 =HOWTO(PCREL24, 0, 2, 24, true, 0, true, true,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false);static reloc_howto_type howto_reloc_pcrel13 =HOWTO(PCREL13, 0, 2, 13, true, 0, true, true,0,"pcrel13", true, 0x00001fff,0x00001fff,false);static reloc_howto_type howto_reloc_abs32codeshrunk = HOWTO(ABS32CODE_SHRUNK, 0, 2, 24, true, 0, true, true, 0,"callx->callj", true, 0x00ffffff, 0x00ffffff,false);static reloc_howto_type howto_reloc_abs32code =HOWTO(ABS32CODE, 0, 2, 32, false, 0, true, true,0,"callx", true, 0xffffffff,0xffffffff,false);static reloc_howto_type howto_align_table[] = { HOWTO (ALIGNER, 0, 0x1, 0, false, 0, false, false, 0, "align16", false, 0, 0, false), HOWTO (ALIGNER, 0, 0x3, 0, false, 0, false, false, 0, "align32", false, 0, 0, false), HOWTO (ALIGNER, 0, 0x7, 0, false, 0, false, false, 0, "align64", false, 0, 0, false), HOWTO (ALIGNER, 0, 0xf, 0, false, 0, false, false, 0, "align128", false, 0, 0, false),};static reloc_howto_type howto_done_align_table[] = { HOWTO (ALIGNDONE, 0x1, 0x1, 0, false, 0, false, false, 0, "donealign16", false, 0, 0, false), HOWTO (ALIGNDONE, 0x3, 0x3, 0, false, 0, false, false, 0, "donealign32", false, 0, 0, false), HOWTO (ALIGNDONE, 0x7, 0x7, 0, false, 0, false, false, 0, "donealign64", false, 0, 0, false), HOWTO (ALIGNDONE, 0xf, 0xf, 0, false, 0, false, false, 0, "donealign128", false, 0, 0, false),};static reloc_howto_type *b_out_reloc_type_lookup (abfd, code) bfd *abfd; bfd_reloc_code_real_type code;{ switch (code) { default: return 0; case BFD_RELOC_I960_CALLJ: return &howto_reloc_callj; case BFD_RELOC_32: return &howto_reloc_abs32; case BFD_RELOC_24_PCREL: return &howto_reloc_pcrel24; }}/* Allocate enough room for all the reloc entries, plus pointers to them all */static booleanb_out_slurp_reloc_table (abfd, asect, symbols) bfd *abfd; sec_ptr asect; asymbol **symbols;{ register struct relocation_info *rptr; unsigned int counter ; arelent *cache_ptr ; int extern_mask, pcrel_mask, callj_mask, length_shift;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -