coff-ppc.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 2,197 行 · 第 1/5 页
C
2,197 行
/* BFD back-end for PowerPC Microsoft Portable Executable files. Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. Original version pieced together by Kim Knuttila (krk@cygnus.com) There is nothing new under the sun. This file draws a lot on other coff files, in particular, those for the rs/6000, alpha, mips, and intel backends, and the PE work for the arm.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, 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA. *//* Current State: - objdump works - relocs generated by gas - ld will link files, but they do not run. - dlltool will not produce correct output in some .reloc cases, and will not produce the right glue code for dll function calls.*/#include "bfd.h"#include "sysdep.h"#include "libbfd.h"#include "coff/powerpc.h"#include "coff/internal.h"#include "coff/pe.h"#ifdef BADMAG#undef BADMAG#endif#define BADMAG(x) PPCBADMAG(x)#include "libcoff.h"/* This file is compiled more than once, but we only compile the final_link routine once. */extern boolean ppc_bfd_coff_final_link PARAMS ((bfd *, struct bfd_link_info *));extern void dump_toc PARAMS ((PTR));/* The toc is a set of bfd_vma fields. We use the fact that valid *//* addresses are even (i.e. the bit representing "1" is off) to allow *//* us to encode a little extra information in the field *//* - Unallocated addresses are intialized to 1. *//* - Allocated addresses are even numbers. *//* The first time we actually write a reference to the toc in the bfd, *//* we want to record that fact in a fixup file (if it is asked for), so *//* we keep track of whether or not an address has been written by marking *//* the low order bit with a "1" upon writing */#define SET_UNALLOCATED(x) ((x) = 1)#define IS_UNALLOCATED(x) ((x) == 1)#define IS_WRITTEN(x) ((x) & 1)#define MARK_AS_WRITTEN(x) ((x) |= 1)#define MAKE_ADDR_AGAIN(x) ((x) &= ~1)/* Turn on this check if you suspect something amiss in the hash tables */#ifdef DEBUG_HASH/* Need a 7 char string for an eye catcher */#define EYE "krkjunk"#define HASH_CHECK_DCL char eye_catcher[8];#define HASH_CHECK_INIT(ret) strcpy(ret->eye_catcher, EYE)#define HASH_CHECK(addr) \ if (strcmp(addr->eye_catcher, EYE) != 0) \ { \ fprintf (stderr,\ _("File %s, line %d, Hash check failure, bad eye %8s\n"), \ __FILE__, __LINE__, addr->eye_catcher); \ abort (); \ }#else#define HASH_CHECK_DCL#define HASH_CHECK_INIT(ret)#define HASH_CHECK(addr)#endif/* In order not to add an int to every hash table item for every coff linker, we define our own hash table, derived from the coff one *//* PE linker hash table entries. */struct ppc_coff_link_hash_entry{ struct coff_link_hash_entry root; /* First entry, as required */ /* As we wonder around the relocs, we'll keep the assigned toc_offset here */ bfd_vma toc_offset; /* Our addition, as required */ int symbol_is_glue; unsigned long int glue_insn; HASH_CHECK_DCL};/* PE linker hash table. */struct ppc_coff_link_hash_table{ struct coff_link_hash_table root; /* First entry, as required */};static struct bfd_hash_entry *ppc_coff_link_hash_newfunc PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));static boolean ppc_coff_link_hash_table_init PARAMS ((struct ppc_coff_link_hash_table *, bfd *, struct bfd_hash_entry *(*) (struct bfd_hash_entry *, struct bfd_hash_table *, const char *)));static struct bfd_link_hash_table *ppc_coff_link_hash_table_create PARAMS ((bfd *));static boolean coff_ppc_relocate_section PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, struct internal_reloc *, struct internal_syment *, asection **));static reloc_howto_type *coff_ppc_rtype_to_howto PARAMS ((bfd *, asection *, struct internal_reloc *, struct coff_link_hash_entry *, struct internal_syment *, bfd_vma *));/* Routine to create an entry in the link hash table. */static struct bfd_hash_entry *ppc_coff_link_hash_newfunc (entry, table, string) struct bfd_hash_entry *entry; struct bfd_hash_table *table; const char *string;{ struct ppc_coff_link_hash_entry *ret = (struct ppc_coff_link_hash_entry *) entry; /* Allocate the structure if it has not already been allocated by a subclass. */ if (ret == (struct ppc_coff_link_hash_entry *) NULL) ret = (struct ppc_coff_link_hash_entry *) bfd_hash_allocate (table, sizeof (struct ppc_coff_link_hash_entry)); if (ret == (struct ppc_coff_link_hash_entry *) NULL) return NULL; /* Call the allocation method of the superclass. */ ret = ((struct ppc_coff_link_hash_entry *) _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); if (ret) { /* Initialize the local fields. */ SET_UNALLOCATED(ret->toc_offset); ret->symbol_is_glue = 0; ret->glue_insn = 0; HASH_CHECK_INIT(ret); } return (struct bfd_hash_entry *) ret;}/* Initialize a PE linker hash table. */static booleanppc_coff_link_hash_table_init (table, abfd, newfunc) struct ppc_coff_link_hash_table *table; bfd *abfd; struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));{ return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc);}/* Create a PE linker hash table. */static struct bfd_link_hash_table *ppc_coff_link_hash_table_create (abfd) bfd *abfd;{ struct ppc_coff_link_hash_table *ret; ret = ((struct ppc_coff_link_hash_table *) bfd_alloc (abfd, sizeof (struct ppc_coff_link_hash_table))); if (ret == NULL) return NULL; if (! ppc_coff_link_hash_table_init (ret, abfd, ppc_coff_link_hash_newfunc)) { bfd_release (abfd, ret); return (struct bfd_link_hash_table *) NULL; } return &ret->root.root;}/* Now, tailor coffcode.h to use our hash stuff */#define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create/* The nt loader points the toc register to &toc + 32768, in order to *//* use the complete range of a 16-bit displacement. We have to adjust *//* for this when we fix up loads displaced off the toc reg. */#define TOC_LOAD_ADJUSTMENT (-32768)#define TOC_SECTION_NAME ".private.toc"/* The main body of code is in coffcode.h. */#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)/* In case we're on a 32-bit machine, construct a 64-bit "-1" value from smaller values. Start with zero, widen, *then* decrement. */#define MINUS_ONE (((bfd_vma)0) - 1)/* these should definitely go in a header file somewhere... *//* NOP */#define IMAGE_REL_PPC_ABSOLUTE 0x0000/* 64-bit address */#define IMAGE_REL_PPC_ADDR64 0x0001/* 32-bit address */#define IMAGE_REL_PPC_ADDR32 0x0002/* 26-bit address, shifted left 2 (branch absolute) */#define IMAGE_REL_PPC_ADDR24 0x0003/* 16-bit address */#define IMAGE_REL_PPC_ADDR16 0x0004/* 16-bit address, shifted left 2 (load doubleword) */#define IMAGE_REL_PPC_ADDR14 0x0005/* 26-bit PC-relative offset, shifted left 2 (branch relative) */#define IMAGE_REL_PPC_REL24 0x0006/* 16-bit PC-relative offset, shifted left 2 (br cond relative) */#define IMAGE_REL_PPC_REL14 0x0007/* 16-bit offset from TOC base */#define IMAGE_REL_PPC_TOCREL16 0x0008/* 16-bit offset from TOC base, shifted left 2 (load doubleword) */#define IMAGE_REL_PPC_TOCREL14 0x0009/* 32-bit addr w/o image base */#define IMAGE_REL_PPC_ADDR32NB 0x000A/* va of containing section (as in an image sectionhdr) */#define IMAGE_REL_PPC_SECREL 0x000B/* sectionheader number */#define IMAGE_REL_PPC_SECTION 0x000C/* substitute TOC restore instruction iff symbol is glue code */#define IMAGE_REL_PPC_IFGLUE 0x000D/* symbol is glue code; virtual address is TOC restore instruction */#define IMAGE_REL_PPC_IMGLUE 0x000E/* va of containing section (limited to 16 bits) */#define IMAGE_REL_PPC_SECREL16 0x000F/* stuff to handle immediate data when the number of bits in the *//* data is greater than the number of bits in the immediate field *//* We need to do (usually) 32 bit arithmetic on 16 bit chunks */#define IMAGE_REL_PPC_REFHI 0x0010#define IMAGE_REL_PPC_REFLO 0x0011#define IMAGE_REL_PPC_PAIR 0x0012/* This is essentially the same as tocrel16, with TOCDEFN assumed */#define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013/* Flag bits in IMAGE_RELOCATION.TYPE *//* subtract reloc value rather than adding it */#define IMAGE_REL_PPC_NEG 0x0100/* fix branch prediction bit to predict branch taken */#define IMAGE_REL_PPC_BRTAKEN 0x0200/* fix branch prediction bit to predict branch not taken */#define IMAGE_REL_PPC_BRNTAKEN 0x0400/* toc slot defined in file (or, data in toc) */#define IMAGE_REL_PPC_TOCDEFN 0x0800/* masks to isolate above values in IMAGE_RELOCATION.Type */#define IMAGE_REL_PPC_TYPEMASK 0x00FF#define IMAGE_REL_PPC_FLAGMASK 0x0F00#define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)#define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)#define EXTRACT_JUNK(x) \ ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))/* static helper functions to make relocation work *//* (Work In Progress) */static bfd_reloc_status_type ppc_refhi_reloc PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, asection *section, bfd *output_bfd, char **error));#if 0static bfd_reloc_status_type ppc_reflo_reloc PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, asection *section, bfd *output_bfd, char **error));#endifstatic bfd_reloc_status_type ppc_pair_reloc PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, asection *section, bfd *output_bfd, char **error));static bfd_reloc_status_type ppc_toc16_reloc PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, asection *section, bfd *output_bfd, char **error));#if 0static bfd_reloc_status_type ppc_addr32nb_reloc PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, asection *section, bfd *output_bfd, char **error));#endifstatic bfd_reloc_status_type ppc_section_reloc PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, asection *section, bfd *output_bfd, char **error));static bfd_reloc_status_type ppc_secrel_reloc PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, asection *section, bfd *output_bfd, char **error));static bfd_reloc_status_type ppc_imglue_reloc PARAMS ((bfd *abfd, arelent *reloc, asymbol *symbol, PTR data, asection *section, bfd *output_bfd, char **error));static boolean in_reloc_p PARAMS((bfd *abfd, reloc_howto_type *howto));/* FIXME: It'll take a while to get through all of these. I only need a few to get us started, so those I'll make sure work. Those marked FIXME are either completely unverified or have a specific unknown marked in the comment *//*---------------------------------------------------------------------------*//* *//* Relocation entries for Windows/NT on PowerPC. *//* *//* From the document "" we find the following listed as used relocs: *//* *//* ABSOLUTE : The noop *//* ADDR[64|32|16] : fields that hold addresses in data fields or the *//* 16 bit displacement field on a load/store. *//* ADDR[24|14] : fields that hold addresses in branch and cond *//* branches. These represent [26|16] bit addresses. *//* The low order 2 bits are preserved. *//* REL[24|14] : branches relative to the Instruction Address *//* register. These represent [26|16] bit addresses, *//* as before. The instruction field will be zero, and *//* the address of the SYM will be inserted at link time. *//* TOCREL16 : 16 bit displacement field referring to a slot in *//* toc. *//* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. *//* ADDR32NB : 32 bit address relative to the virtual origin. *//* (On the alpha, this is always a linker generated thunk)*//* (i.e. 32bit addr relative to the image base) *//* SECREL : The value is relative to the start of the section *//* containing the symbol. *//* SECTION : access to the header containing the item. Supports the *//* codeview debugger. *//* *//* In particular, note that the document does not indicate that the *//* relocations listed in the header file are used. *//* *//* *//* *//*---------------------------------------------------------------------------*/static reloc_howto_type ppc_coff_howto_table[] ={ /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */ /* Unused: */ HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */ 0, /* rightshift */ 0, /* size (0 = byte, 1 = short, 2 = long) */ 0, /* bitsize */ false, /* pc_relative */ 0, /* bitpos */ complain_overflow_dont, /* dont complain_on_overflow */ 0, /* special_function */ "ABSOLUTE", /* name */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?