xcofflink.c

来自「基于4个mips核的noc设计」· C语言 代码 · 共 2,198 行 · 第 1/5 页

C
2,198
字号
/* POWER/PowerPC XCOFF linker support.   Copyright 1995, 1996, 1997, 1998, 1999, 2000   Free Software Foundation, Inc.   Written by Ian Lance Taylor <ian@cygnus.com>, 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 "bfdlink.h"#include "libbfd.h"#include "coff/internal.h"#include "libcoff.h"/* This file holds the XCOFF linker code.  */#define STRING_SIZE_SIZE (4)/* Get the XCOFF hash table entries for a BFD.  */#define obj_xcoff_sym_hashes(bfd) \  ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))/* XCOFF relocation types.  These probably belong in a header file   somewhere.  The relocations are described in the function   _bfd_ppc_xcoff_relocate_section in this file.  */#define R_POS   (0x00)#define R_NEG   (0x01)#define R_REL   (0x02)#define R_TOC   (0x03)#define R_RTB   (0x04)#define R_GL    (0x05)#define R_TCL   (0x06)#define R_BA    (0x08)#define R_BR    (0x0a)#define R_RL    (0x0c)#define R_RLA   (0x0d)#define R_REF   (0x0f)#define R_TRL   (0x12)#define R_TRLA  (0x13)#define R_RRTBI (0x14)#define R_RRTBA (0x15)#define R_CAI   (0x16)#define R_CREL  (0x17)#define R_RBA   (0x18)#define R_RBAC  (0x19)#define R_RBR   (0x1a)#define R_RBRC  (0x1b)/* The first word of global linkage code.  This must be modified by   filling in the correct TOC offset.  */#define XCOFF_GLINK_FIRST (0x81820000)	/* lwz r12,0(r2) *//* The remaining words of global linkage code.  */static unsigned long xcoff_glink_code[] = {  0x90410014,	/* stw r2,20(r1) */  0x800c0000,	/* lwz r0,0(r12) */  0x804c0004,	/* lwz r2,4(r12) */  0x7c0903a6,	/* mtctr r0 */  0x4e800420,	/* bctr */  0x0,		/* start of traceback table */  0x000c8000,	/* traceback table */  0x0		/* traceback table */};#define XCOFF_GLINK_SIZE \  (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)/* We reuse the SEC_ROM flag as a mark flag for garbage collection.   This flag will only be used on input sections.  */#define SEC_MARK (SEC_ROM)/* The ldhdr structure.  This appears at the start of the .loader   section.  */struct internal_ldhdr {  /* The version number: currently always 1.  */  unsigned long l_version;  /* The number of symbol table entries.  */  bfd_size_type l_nsyms;  /* The number of relocation table entries.  */  bfd_size_type l_nreloc;  /* The length of the import file string table.  */  bfd_size_type l_istlen;  /* The number of import files.  */  bfd_size_type l_nimpid;  /* The offset from the start of the .loader section to the first     entry in the import file table.  */  bfd_size_type l_impoff;  /* The length of the string table.  */  bfd_size_type l_stlen;  /* The offset from the start of the .loader section to the first     entry in the string table.  */  bfd_size_type l_stoff;};struct external_ldhdr {  bfd_byte l_version[4];  bfd_byte l_nsyms[4];  bfd_byte l_nreloc[4];  bfd_byte l_istlen[4];  bfd_byte l_nimpid[4];  bfd_byte l_impoff[4];  bfd_byte l_stlen[4];  bfd_byte l_stoff[4];};#define LDHDRSZ (8 * 4)/* The ldsym structure.  This is used to represent a symbol in the   .loader section.  */struct internal_ldsym {  union {    /* The symbol name if <= SYMNMLEN characters.  */    char _l_name[SYMNMLEN];    struct {      /* Zero if the symbol name is more than SYMNMLEN characters.  */      long _l_zeroes;      /* The offset in the string table if the symbol name is more	 than SYMNMLEN characters.  */      long _l_offset;    } _l_l;  } _l;  /* The symbol value.  */  bfd_vma l_value;  /* The symbol section number.  */  short l_scnum;  /* The symbol type and flags.  */  char l_smtype;  /* The symbol storage class.  */  char l_smclas;  /* The import file ID.  */  bfd_size_type l_ifile;  /* Offset to the parameter type check string.  */  bfd_size_type l_parm;};struct external_ldsym {  union {    bfd_byte _l_name[SYMNMLEN];    struct {      bfd_byte _l_zeroes[4];      bfd_byte _l_offset[4];    } _l_l;  } _l;  bfd_byte l_value[4];  bfd_byte l_scnum[2];  bfd_byte l_smtype[1];  bfd_byte l_smclas[1];  bfd_byte l_ifile[4];  bfd_byte l_parm[4];};#define LDSYMSZ (8 + 3 * 4 + 2 + 2)/* These flags are for the l_smtype field (the lower three bits are an   XTY_* value).  *//* Imported symbol.  */#define L_IMPORT (0x40)/* Entry point.  */#define L_ENTRY (0x20)/* Exported symbol.  */#define L_EXPORT (0x10)/* The ldrel structure.  This is used to represent a reloc in the   .loader section.  */struct internal_ldrel {  /* The reloc address.  */  bfd_vma l_vaddr;  /* The symbol table index in the .loader section symbol table.  */  bfd_size_type l_symndx;  /* The relocation type and size.  */  short l_rtype;  /* The section number this relocation applies to.  */  short l_rsecnm;};struct external_ldrel {  bfd_byte l_vaddr[4];  bfd_byte l_symndx[4];  bfd_byte l_rtype[2];  bfd_byte l_rsecnm[2];};#define LDRELSZ (2 * 4 + 2 * 2)/* The list of import files.  */struct xcoff_import_file {  /* The next entry in the list.  */  struct xcoff_import_file *next;  /* The path.  */  const char *path;  /* The file name.  */  const char *file;  /* The member name.  */  const char *member;};/* An entry in the XCOFF linker hash table.  */struct xcoff_link_hash_entry {  struct bfd_link_hash_entry root;  /* Symbol index in output file.  Set to -1 initially.  Set to -2 if     there is a reloc against this symbol.  */  long indx;  /* If we have created a TOC entry for this symbol, this is the .tc     section which holds it.  */  asection *toc_section;  union {    /* If we have created a TOC entry (the XCOFF_SET_TOC flag is set),       this is the offset in toc_section.  */    bfd_vma toc_offset;    /* If the TOC entry comes from an input file, this is set to the       symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol.  */    long toc_indx;  } u;  /* If this symbol is a function entry point which is called, this     field holds a pointer to the function descriptor.  If this symbol     is a function descriptor, this field holds a pointer to the     function entry point.  */  struct xcoff_link_hash_entry *descriptor;  /* The .loader symbol table entry, if there is one.  */  struct internal_ldsym *ldsym;  /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table     index.  If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,     this is the l_ifile value.  */  long ldindx;  /* Some linker flags.  */  unsigned short flags;  /* Symbol is referenced by a regular object.  */#define XCOFF_REF_REGULAR (01)  /* Symbol is defined by a regular object.  */#define XCOFF_DEF_REGULAR (02)  /* Symbol is defined by a dynamic object.  */#define XCOFF_DEF_DYNAMIC (04)  /* Symbol is used in a reloc being copied into the .loader section.  */#define XCOFF_LDREL (010)  /* Symbol is the entry point.  */#define XCOFF_ENTRY (020)  /* Symbol is called; this is, it appears in a R_BR reloc.  */#define XCOFF_CALLED (040)  /* Symbol needs the TOC entry filled in.  */#define XCOFF_SET_TOC (0100)  /* Symbol is explicitly imported.  */#define XCOFF_IMPORT (0200)  /* Symbol is explicitly exported.  */#define XCOFF_EXPORT (0400)  /* Symbol has been processed by xcoff_build_ldsyms.  */#define XCOFF_BUILT_LDSYM (01000)  /* Symbol is mentioned by a section which was not garbage collected.  */#define XCOFF_MARK (02000)  /* Symbol size is recorded in size_list list from hash table.  */#define XCOFF_HAS_SIZE (04000)  /* Symbol is a function descriptor.  */#define XCOFF_DESCRIPTOR (010000)  /* Multiple definitions have been for the symbol.  */#define XCOFF_MULTIPLY_DEFINED (020000)  /* The storage mapping class.  */  unsigned char smclas;};/* The XCOFF linker hash table.  */struct xcoff_link_hash_table {  struct bfd_link_hash_table root;  /* The .debug string hash table.  We need to compute this while     reading the input files, so that we know how large the .debug     section will be before we assign section positions.  */  struct bfd_strtab_hash *debug_strtab;  /* The .debug section we will use for the final output.  */  asection *debug_section;  /* The .loader section we will use for the final output.  */  asection *loader_section;  /* A count of non TOC relative relocs which will need to be     allocated in the .loader section.  */  size_t ldrel_count;  /* The .loader section header.  */  struct internal_ldhdr ldhdr;  /* The .gl section we use to hold global linkage code.  */  asection *linkage_section;  /* The .tc section we use to hold toc entries we build for global     linkage code.  */  asection *toc_section;  /* The .ds section we use to hold function descriptors which we     create for exported symbols.  */  asection *descriptor_section;  /* The list of import files.  */  struct xcoff_import_file *imports;  /* Required alignment of sections within the output file.  */  unsigned long file_align;  /* Whether the .text section must be read-only.  */  boolean textro;  /* Whether garbage collection was done.  */  boolean gc;  /* A linked list of symbols for which we have size information.  */  struct xcoff_link_size_list {    struct xcoff_link_size_list *next;    struct xcoff_link_hash_entry *h;    bfd_size_type size;  } *size_list;  /* Magic sections: _text, _etext, _data, _edata, _end, end.  */  asection *special_sections[6];};/* Information we keep for each section in the output file during the   final link phase.  */struct xcoff_link_section_info {  /* The relocs to be output.  */  struct internal_reloc *relocs;  /* For each reloc against a global symbol whose index was not known     when the reloc was handled, the global hash table entry.  */  struct xcoff_link_hash_entry **rel_hashes;  /* If there is a TOC relative reloc against a global symbol, and the     index of the TOC symbol is not known when the reloc was handled,     an entry is added to this linked list.  This is not an array,     like rel_hashes, because this case is quite uncommon.  */  struct xcoff_toc_rel_hash {    struct xcoff_toc_rel_hash *next;    struct xcoff_link_hash_entry *h;    struct internal_reloc *rel;  } *toc_rel_hashes;};/* Information that we pass around while doing the final link step.  */struct xcoff_final_link_info {  /* General link information.  */  struct bfd_link_info *info;  /* Output BFD.  */  bfd *output_bfd;  /* Hash table for long symbol names.  */  struct bfd_strtab_hash *strtab;  /* Array of information kept for each output section, indexed by the     target_index field.  */  struct xcoff_link_section_info *section_info;  /* Symbol index of last C_FILE symbol (-1 if none).  */  long last_file_index;  /* Contents of last C_FILE symbol.  */  struct internal_syment last_file;  /* Symbol index of TOC symbol.  */  long toc_symindx;  /* Start of .loader symbols.  */  struct external_ldsym *ldsym;  /* Next .loader reloc to swap out.  */  struct external_ldrel *ldrel;  /* File position of start of line numbers.  */  file_ptr line_filepos;  /* Buffer large enough to hold swapped symbols of any input file.  */  struct internal_syment *internal_syms;  /* Buffer large enough to hold output indices of symbols of any     input file.  */  long *sym_indices;  /* Buffer large enough to hold output symbols for any input file.  */  bfd_byte *outsyms;  /* Buffer large enough to hold external line numbers for any input     section.  */  bfd_byte *linenos;  /* Buffer large enough to hold any input section.  */  bfd_byte *contents;  /* Buffer large enough to hold external relocs of any input section.  */  bfd_byte *external_relocs;};static void xcoff_swap_ldhdr_in  PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));static void xcoff_swap_ldhdr_out  PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));static void xcoff_swap_ldsym_in  PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));static void xcoff_swap_ldsym_out  PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));static void xcoff_swap_ldrel_in  PARAMS ((bfd *, const struct external_ldrel *, struct internal_ldrel *));static void xcoff_swap_ldrel_out  PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));static struct bfd_hash_entry *xcoff_link_hash_newfunc  PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));static struct internal_reloc *xcoff_read_internal_relocs  PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,	   struct internal_reloc *));static boolean xcoff_link_add_object_symbols  PARAMS ((bfd *, struct bfd_link_info *));static boolean xcoff_link_check_archive_element  PARAMS ((bfd *, struct bfd_link_info *, boolean *));static boolean xcoff_link_check_ar_symbols  PARAMS ((bfd *, struct bfd_link_info *, boolean *));static boolean xcoff_link_check_dynamic_ar_symbols  PARAMS ((bfd *, struct bfd_link_info *, boolean *));static bfd_size_type xcoff_find_reloc  PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));static boolean xcoff_link_add_dynamic_symbols  PARAMS ((bfd *, struct bfd_link_info *));static boolean xcoff_mark_symbol  PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?