bout.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 1,528 行 · 第 1/3 页
C
1,528 行
/* BFD back-end for Intel 960 b.out binaries. Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "bfd.h"#include "sysdep.h"#include "libbfd.h"#include "bfdlink.h"#include "genlink.h"#include "bout.h"#include "aout/stab_gnu.h"#include "libaout.h" /* BFD a.out internal data structures */static int aligncode PARAMS ((bfd *abfd, asection *input_section, arelent *r, unsigned int shrink));static void perform_slip PARAMS ((bfd *abfd, unsigned int slip, asection *input_section, bfd_vma value));static boolean b_out_squirt_out_relocs PARAMS ((bfd *abfd, asection *section));static const bfd_target *b_out_callback PARAMS ((bfd *));static bfd_reloc_status_type calljx_callback PARAMS ((bfd *, struct bfd_link_info *, arelent *, PTR src, PTR dst, asection *));static bfd_reloc_status_type callj_callback PARAMS ((bfd *, struct bfd_link_info *, arelent *, PTR data, unsigned int srcidx, unsigned int dstidx, asection *, boolean));static bfd_vma get_value PARAMS ((arelent *, struct bfd_link_info *, asection *));static int abs32code PARAMS ((bfd *, asection *, arelent *, unsigned int, struct bfd_link_info *));static boolean b_out_bfd_relax_section PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));static bfd_byte *b_out_bfd_get_relocated_section_contents PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *, boolean, asymbol **));/* Swaps the information in an executable header taken from a raw byte stream memory image, into the internal exec_header structure. */voidbout_swap_exec_header_in (abfd, raw_bytes, execp) bfd *abfd; struct external_exec *raw_bytes; 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));voidbout_swap_exec_header_out (abfd, execp, raw_bytes) bfd *abfd; struct internal_exec *execp; 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 const 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) { if (bfd_get_error () != bfd_error_system_call) bfd_set_error (bfd_error_wrong_format); return 0; } anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info); if (N_BADMAG (anexec)) { bfd_set_error (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 const 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; obj_textsec (abfd)->lma = obj_textsec (abfd)->vma; obj_datasec (abfd)->lma = obj_datasec (abfd)->vma; /* 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); obj_bsssec (abfd)->lma = obj_bsssec (abfd)->vma; /* 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) return false; abfd->tdata.bout_data = rawptr; exec_hdr (abfd) = &rawptr->e; obj_textsec (abfd) = (asection *)NULL; obj_datasec (abfd) = (asection *)NULL; obj_bsssec (abfd) = (asection *)NULL; return true;}static intb_out_symbol_cmp (a, b) struct aout_symbol **a, **b;{ asection *sec; bfd_vma av, bv; /* Primary key is address */ sec = bfd_get_section (&(*a)->symbol); av = sec->output_section->vma + sec->output_offset + (*a)->symbol.value; sec = bfd_get_section (&(*b)->symbol); bv = sec->output_section->vma + sec->output_offset + (*b)->symbol.value; if (av < bv) return -1; if (av > bv) return 1; /* Secondary key puts CALLNAME syms last and BALNAME syms first, so that they have the best chance of being contiguous. */ if (IS_BALNAME ((*a)->other) || IS_CALLNAME ((*b)->other)) return -1; if (IS_CALLNAME ((*a)->other) || IS_BALNAME ((*b)->other)) return 1; return 0;}static booleanb_out_write_object_contents (abfd) bfd *abfd;{ struct external_exec swapped_hdr; if (! aout_32_make_sections (abfd)) return false; 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); if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 || (bfd_write ((PTR) &swapped_hdr, 1, EXEC_BYTES_SIZE, abfd) != EXEC_BYTES_SIZE)) return false; /* Now write out reloc info, followed by syms and strings */ if (bfd_get_symcount (abfd) != 0) { /* Make sure {CALL,BAL}NAME symbols remain adjacent on output by sorting. This is complicated by the fact that stabs are also ordered. Solve this by shifting all stabs to the end in order, then sorting the rest. */ asymbol **outsyms, **p, **q; outsyms = bfd_get_outsymbols (abfd); p = outsyms + bfd_get_symcount (abfd); for (q = p--; p >= outsyms; p--) { if ((*p)->flags & BSF_DEBUGGING) { asymbol *t = *--q; *q = *p; *p = t; } } if (q > outsyms) qsort (outsyms, q - outsyms, sizeof (asymbol*), b_out_symbol_cmp); /* Back to your regularly scheduled program. */ if (bfd_seek (abfd, (file_ptr) (N_SYMOFF(*exec_hdr(abfd))), SEEK_SET) != 0) return false; if (! aout_32_write_syms (abfd)) return false; if (bfd_seek (abfd, (file_ptr) (N_TROFF(*exec_hdr(abfd))), SEEK_SET) != 0) return false; if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd))) return false; if (bfd_seek (abfd, (file_ptr) (N_DROFF(*exec_hdr(abfd))), SEEK_SET) != 0) return false; 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 BAL_MASK 0x00ffffff#define BALX 0x85f00000 /* Template for 'balx' instruction */#define BALX_MASK 0x0007ffff#define CALL 0x09000000#define PCREL13_MASK 0x1fff#define output_addr(sec) ((sec)->output_offset+(sec)->output_section->vma)/* Magic to turn callx into calljx */static bfd_reloc_status_typecalljx_callback (abfd, link_info, reloc_entry, src, dst, input_section) bfd *abfd; struct bfd_link_info *link_info; arelent *reloc_entry; PTR src; PTR dst; 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); bfd_vma value; value = get_value (reloc_entry, link_info, input_section); 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 &= BALX_MASK; inst |= BALX; bfd_put_32 (abfd, inst, (bfd_byte *) dst-4); symbol = balsym; value = (symbol->symbol.value + output_addr (symbol->symbol.section)); } word += value + reloc_entry->addend; bfd_put_32 (abfd, word, dst); return bfd_reloc_ok;}/* Magic to turn call into callj */static bfd_reloc_status_typecallj_callback (abfd, link_info, reloc_entry, data, srcidx, dstidx, input_section, shrinking) bfd *abfd; struct bfd_link_info *link_info; arelent *reloc_entry; PTR data; unsigned int srcidx; unsigned int dstidx; asection *input_section; boolean shrinking;{ 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); bfd_vma value; value = get_value (reloc_entry, link_info, input_section); 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 + output_addr (balsym->symbol.section) + balsym->symbol.value + reloc_entry->addend - dstidx - output_addr (input_section)) & BAL_MASK); } else if ((symbol->symbol.flags & BSF_SECTION_SYM) != 0) { /* A callj against a symbol in the same section is a fully resolved relative call. We don't need to do anything here. If the symbol is not in the same section, I'm not sure what to do; fortunately, this case will probably never arise. */ BFD_ASSERT (! shrinking); BFD_ASSERT (symbol->symbol.section == input_section); } else { word = CALL | (((word & BAL_MASK) + value + reloc_entry->addend - (shrinking ? dstidx : 0) - output_addr (input_section)) & 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, complain_overflow_signed, 0,"callj", true, 0x00ffffff, 0x00ffffff,false);static reloc_howto_type howto_reloc_abs32 =HOWTO(ABS32, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"abs32", true, 0xffffffff,0xffffffff,false);static reloc_howto_type howto_reloc_pcrel24 =HOWTO(PCREL24, 0, 2, 24, true, 0, complain_overflow_signed,0,"pcrel24", true, 0x00ffffff,0x00ffffff,false);static reloc_howto_type howto_reloc_pcrel13 =HOWTO(PCREL13, 0, 2, 13, true, 0, complain_overflow_signed,0,"pcrel13", true, 0x00001fff,0x00001fff,false);static reloc_howto_type howto_reloc_abs32codeshrunk =HOWTO(ABS32CODE_SHRUNK, 0, 2, 24, true, 0, complain_overflow_signed, 0,"callx->callj", true, 0x00ffffff, 0x00ffffff,false);static reloc_howto_type howto_reloc_abs32code =HOWTO(ABS32CODE, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"callx", true, 0xffffffff,0xffffffff,false);static reloc_howto_type howto_align_table[] = { HOWTO (ALIGNER, 0, 0x1, 0, false, 0, complain_overflow_dont, 0, "align16", false, 0, 0, false), HOWTO (ALIGNER, 0, 0x3, 0, false, 0, complain_overflow_dont, 0, "align32", false, 0, 0, false), HOWTO (ALIGNER, 0, 0x7, 0, false, 0, complain_overflow_dont, 0, "align64", false, 0, 0, false), HOWTO (ALIGNER, 0, 0xf, 0, false, 0, complain_overflow_dont, 0, "align128", false, 0, 0, false),};static reloc_howto_type howto_done_align_table[] = { HOWTO (ALIGNDONE, 0x1, 0x1, 0, false, 0, complain_overflow_dont, 0, "donealign16", false, 0, 0, false), HOWTO (ALIGNDONE, 0x3, 0x3, 0, false, 0, complain_overflow_dont, 0, "donealign32", false, 0, 0, false), HOWTO (ALIGNDONE, 0x7, 0x7, 0, false, 0, complain_overflow_dont, 0, "donealign64", false, 0, 0, false), HOWTO (ALIGNDONE, 0xf, 0xf, 0, false, 0, complain_overflow_dont, 0, "donealign128", false, 0, 0, false),};static reloc_howto_type *b_out_bfd_reloc_type_lookup (abfd, code) bfd *abfd ATTRIBUTE_UNUSED; bfd_reloc_code_real_type code;{ switch (code) { default: return 0; case BFD_RELOC_I960_CALLJ: return &howto_reloc_callj; case BFD_RELOC_32: case BFD_RELOC_CTOR: 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 */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?