elflink.c

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

C
548
字号
/* ELF linking support for BFD.   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001   Free Software Foundation, Inc.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"#define ARCH_SIZE 0#include "elf-bfd.h"boolean_bfd_elf_create_got_section (abfd, info)     bfd *abfd;     struct bfd_link_info *info;{  flagword flags;  register asection *s;  struct elf_link_hash_entry *h;  struct elf_backend_data *bed = get_elf_backend_data (abfd);  int ptralign;  /* This function may be called more than once.  */  if (bfd_get_section_by_name (abfd, ".got") != NULL)    return true;  switch (bed->s->arch_size)    {    case 32:      ptralign = 2;      break;    case 64:      ptralign = 3;      break;    default:      bfd_set_error (bfd_error_bad_value);      return false;    }  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY	   | SEC_LINKER_CREATED);  s = bfd_make_section (abfd, ".got");  if (s == NULL      || !bfd_set_section_flags (abfd, s, flags)      || !bfd_set_section_alignment (abfd, s, ptralign))    return false;  if (bed->want_got_plt)    {      s = bfd_make_section (abfd, ".got.plt");      if (s == NULL	  || !bfd_set_section_flags (abfd, s, flags)	  || !bfd_set_section_alignment (abfd, s, ptralign))	return false;    }  /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got     (or .got.plt) section.  We don't do this in the linker script     because we don't want to define the symbol if we are not creating     a global offset table.  */  h = NULL;  if (!(_bfd_generic_link_add_one_symbol	(info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,	 bed->got_symbol_offset, (const char *) NULL, false,	 bed->collect, (struct bfd_link_hash_entry **) &h)))    return false;  h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;  h->type = STT_OBJECT;  if (info->shared      && ! _bfd_elf_link_record_dynamic_symbol (info, h))    return false;  elf_hash_table (info)->hgot = h;  /* The first bit of the global offset table is the header.  */  s->_raw_size += bed->got_header_size + bed->got_symbol_offset;  return true;}/* Create dynamic sections when linking against a dynamic object.  */boolean_bfd_elf_create_dynamic_sections (abfd, info)     bfd *abfd;     struct bfd_link_info *info;{  flagword flags, pltflags;  register asection *s;  struct elf_backend_data *bed = get_elf_backend_data (abfd);  int ptralign;  switch (bed->s->arch_size)    {    case 32:      ptralign = 2;      break;    case 64:      ptralign = 3;      break;    default:      bfd_set_error (bfd_error_bad_value);      return false;    }  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and     .rel[a].bss sections.  */  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY	   | SEC_LINKER_CREATED);  pltflags = flags;  pltflags |= SEC_CODE;  if (bed->plt_not_loaded)    pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);  if (bed->plt_readonly)    pltflags |= SEC_READONLY;  s = bfd_make_section (abfd, ".plt");  if (s == NULL      || ! bfd_set_section_flags (abfd, s, pltflags)      || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))    return false;  if (bed->want_plt_sym)    {      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the	 .plt section.  */      struct elf_link_hash_entry *h = NULL;      if (! (_bfd_generic_link_add_one_symbol	     (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,	      (bfd_vma) 0, (const char *) NULL, false,	      get_elf_backend_data (abfd)->collect,	      (struct bfd_link_hash_entry **) &h)))	return false;      h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;      h->type = STT_OBJECT;      if (info->shared	  && ! _bfd_elf_link_record_dynamic_symbol (info, h))	return false;    }  s = bfd_make_section (abfd,			bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");  if (s == NULL      || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)      || ! bfd_set_section_alignment (abfd, s, ptralign))    return false;  if (! _bfd_elf_create_got_section (abfd, info))    return false;  if (bed->want_dynbss)    {      /* The .dynbss section is a place to put symbols which are defined	 by dynamic objects, are referenced by regular objects, and are	 not functions.  We must allocate space for them in the process	 image and use a R_*_COPY reloc to tell the dynamic linker to	 initialize them at run time.  The linker script puts the .dynbss	 section into the .bss section of the final image.  */      s = bfd_make_section (abfd, ".dynbss");      if (s == NULL	  || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))	return false;      /* The .rel[a].bss section holds copy relocs.  This section is not     normally needed.  We need to create it here, though, so that the     linker will map it to an output section.  We can't just create it     only if we need it, because we will not know whether we need it     until we have seen all the input files, and the first time the     main linker code calls BFD after examining all the input files     (size_dynamic_sections) the input sections have already been     mapped to the output sections.  If the section turns out not to     be needed, we can discard it later.  We will never need this     section when generating a shared object, since they do not use     copy relocs.  */      if (! info->shared)	{	  s = bfd_make_section (abfd,				(bed->default_use_rela_p				 ? ".rela.bss" : ".rel.bss"));	  if (s == NULL	      || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)	      || ! bfd_set_section_alignment (abfd, s, ptralign))	    return false;	}    }  return true;}/* Record a new dynamic symbol.  We record the dynamic symbols as we   read the input files, since we need to have a list of all of them   before we can determine the final sizes of the output sections.   Note that we may actually call this function even though we are not   going to output any dynamic symbols; in some cases we know that a   symbol should be in the dynamic symbol table, but only if there is   one.  */boolean_bfd_elf_link_record_dynamic_symbol (info, h)     struct bfd_link_info *info;     struct elf_link_hash_entry *h;{  if (h->dynindx == -1)    {      struct bfd_strtab_hash *dynstr;      char *p, *alc;      const char *name;      boolean copy;      bfd_size_type indx;      /* XXX: The ABI draft says the linker must turn hidden and	 internal symbols into STB_LOCAL symbols when producing the	 DSO. However, if ld.so honors st_other in the dynamic table,	 this would not be necessary.  */      switch (ELF_ST_VISIBILITY (h->other))	{	case STV_INTERNAL:	case STV_HIDDEN:	  if (h->root.type != bfd_link_hash_undefined	      && h->root.type != bfd_link_hash_undefweak)	    {	      h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;	      return true;	    }	default:	  break;	}      h->dynindx = elf_hash_table (info)->dynsymcount;      ++elf_hash_table (info)->dynsymcount;      dynstr = elf_hash_table (info)->dynstr;      if (dynstr == NULL)	{	  /* Create a strtab to hold the dynamic symbol names.  */	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();	  if (dynstr == NULL)	    return false;	}      /* We don't put any version information in the dynamic string         table.  */      name = h->root.root.string;      p = strchr (name, ELF_VER_CHR);      if (p == NULL)	{	  alc = NULL;	  copy = false;

⌨️ 快捷键说明

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