⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elf64-x86-64.c

📁 基于4个mips核的noc设计
💻 C
📖 第 1 页 / 共 4 页
字号:
	case R_X86_64_64:	case R_X86_64_32S:	case R_X86_64_PC32:	  if (h != NULL)	    h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;	  /* If we are creating a shared library, and this is a reloc	     against a global symbol, or a non PC relative reloc	     against a local symbol, then we need to copy the reloc	     into the shared library.  However, if we are linking with	     -Bsymbolic, we do not need to copy a reloc against a	     global symbol which is defined in an object we are	     including in the link (i.e., DEF_REGULAR is set).	At	     this point we have not seen all the input files, so it is	     possible that DEF_REGULAR is not set now but will be set	     later (it is never cleared).  We account for that	     possibility below by storing information in the	     pcrel_relocs_copied field of the hash table entry.	     A similar situation occurs when creating shared libraries	     and symbol visibility changes render the symbol local.  */	  if (info->shared	      && (sec->flags & SEC_ALLOC) != 0	      && (((ELF64_R_TYPE (rel->r_info) != R_X86_64_PC8)		  && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC16)		  && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32))		  || (h != NULL		      && (! info->symbolic			  || (h->elf_link_hash_flags			      & ELF_LINK_HASH_DEF_REGULAR) == 0))))	    {	      /* When creating a shared object, we must copy these		 reloc types into the output file.  We create a reloc		 section in dynobj and make room for this reloc.  */	      if (sreloc == NULL)		{		  const char *name;		  name = (bfd_elf_string_from_elf_section			  (abfd,			   elf_elfheader (abfd)->e_shstrndx,			   elf_section_data (sec)->rel_hdr.sh_name));		  if (name == NULL)		    return false;		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0			      && strcmp (bfd_get_section_name (abfd, sec),					 name + 5) == 0);		  sreloc = bfd_get_section_by_name (dynobj, name);		  if (sreloc == NULL)		    {		      flagword flags;		      sreloc = bfd_make_section (dynobj, name);		      flags = (SEC_HAS_CONTENTS | SEC_READONLY			       | SEC_IN_MEMORY | SEC_LINKER_CREATED);		      if ((sec->flags & SEC_ALLOC) != 0)			flags |= SEC_ALLOC | SEC_LOAD;		      if (sreloc == NULL			  || ! bfd_set_section_flags (dynobj, sreloc, flags)			  || ! bfd_set_section_alignment (dynobj, sreloc, 3))			return false;		    }		}	      sreloc->_raw_size += sizeof (Elf64_External_Rela);	      /* If this is a global symbol, we count the number of PC		 relative relocations we have entered for this symbol,		 so that we can discard them later as necessary.  Note		 that this function is only called if we are using an		 elf64_x86_64 linker hash table, which means that h is		 really a pointer to an elf64_x86_64_link_hash_entry.  */	      if (h != NULL		  && ((ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8)		      || (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16)		      || (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)))		{		  struct elf64_x86_64_link_hash_entry *eh;		  struct elf64_x86_64_pcrel_relocs_copied *p;		  eh = (struct elf64_x86_64_link_hash_entry *) h;		  for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)		    if (p->section == sreloc)		      break;		  if (p == NULL)		    {		      p = ((struct elf64_x86_64_pcrel_relocs_copied *)			   bfd_alloc (dynobj, sizeof *p));		      if (p == NULL)			return false;		      p->next = eh->pcrel_relocs_copied;		      eh->pcrel_relocs_copied = p;		      p->section = sreloc;		      p->count = 0;		    }		  ++p->count;		}	    }	  break;	}    }  return true;}/* Return the section that should be marked against GC for a given   relocation.	*/static asection *elf64_x86_64_gc_mark_hook (abfd, info, rel, h, sym)     bfd *abfd;     struct bfd_link_info *info ATTRIBUTE_UNUSED;     Elf_Internal_Rela *rel ATTRIBUTE_UNUSED;     struct elf_link_hash_entry *h;     Elf_Internal_Sym *sym;{  if (h != NULL)    {      switch (h->root.type)	{	case bfd_link_hash_defined:	case bfd_link_hash_defweak:	  return h->root.u.def.section;	case bfd_link_hash_common:	  return h->root.u.c.p->section;	default:	  break;	}    }  else    {      if (!(elf_bad_symtab (abfd)	    && ELF_ST_BIND (sym->st_info) != STB_LOCAL)	  && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)		&& sym->st_shndx != SHN_COMMON))	{	  return bfd_section_from_elf_index (abfd, sym->st_shndx);	}    }  return NULL;}/* Update the got entry reference counts for the section being removed.	 */static booleanelf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)     bfd *abfd;     struct bfd_link_info *info ATTRIBUTE_UNUSED;     asection *sec;     const Elf_Internal_Rela *relocs;{  Elf_Internal_Shdr *symtab_hdr;  struct elf_link_hash_entry **sym_hashes;  bfd_signed_vma *local_got_refcounts;  const Elf_Internal_Rela *rel, *relend;  unsigned long r_symndx;  struct elf_link_hash_entry *h;  bfd *dynobj;  asection *sgot;  asection *srelgot;  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;  sym_hashes = elf_sym_hashes (abfd);  local_got_refcounts = elf_local_got_refcounts (abfd);  dynobj = elf_hash_table (info)->dynobj;  if (dynobj == NULL)    return true;  sgot = bfd_get_section_by_name (dynobj, ".got");  srelgot = bfd_get_section_by_name (dynobj, ".rela.got");  relend = relocs + sec->reloc_count;  for (rel = relocs; rel < relend; rel++)    switch (ELF64_R_TYPE (rel->r_info))      {      case R_X86_64_GOT32:      case R_X86_64_GOTPCREL:	r_symndx = ELF64_R_SYM (rel->r_info);	if (r_symndx >= symtab_hdr->sh_info)	  {	    h = sym_hashes[r_symndx - symtab_hdr->sh_info];	    if (h->got.refcount > 0)	      {		h->got.refcount -= 1;		if (h->got.refcount == 0)		  {		    sgot->_raw_size -= GOT_ENTRY_SIZE;		    srelgot->_raw_size -= sizeof (Elf64_External_Rela);		  }	      }	  }	else if (local_got_refcounts != NULL)	  {	    if (local_got_refcounts[r_symndx] > 0)	      {		local_got_refcounts[r_symndx] -= 1;		if (local_got_refcounts[r_symndx] == 0)		  {		    sgot->_raw_size -= GOT_ENTRY_SIZE;		    if (info->shared)		      srelgot->_raw_size -= sizeof (Elf64_External_Rela);		  }	      }	  }	break;      case R_X86_64_PLT32:	r_symndx = ELF64_R_SYM (rel->r_info);	if (r_symndx >= symtab_hdr->sh_info)	  {	    h = sym_hashes[r_symndx - symtab_hdr->sh_info];	    if (h->plt.refcount > 0)	      h->plt.refcount -= 1;	  }	break;      default:	break;      }  return true;}/* Adjust a symbol defined by a dynamic object and referenced by a   regular object.  The current definition is in some section of the   dynamic object, but we're not including those sections.  We have to   change the definition to something the rest of the link can   understand.	*/static booleanelf64_x86_64_adjust_dynamic_symbol (info, h)     struct bfd_link_info *info;     struct elf_link_hash_entry *h;{  bfd *dynobj;  asection *s;  unsigned int power_of_two;  dynobj = elf_hash_table (info)->dynobj;  /* Make sure we know what is going on here.  */  BFD_ASSERT (dynobj != NULL	      && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)		  || h->weakdef != NULL		  || ((h->elf_link_hash_flags		       & ELF_LINK_HASH_DEF_DYNAMIC) != 0		      && (h->elf_link_hash_flags			  & ELF_LINK_HASH_REF_REGULAR) != 0		      && (h->elf_link_hash_flags			  & ELF_LINK_HASH_DEF_REGULAR) == 0)));  /* If this is a function, put it in the procedure linkage table.  We     will fill in the contents of the procedure linkage table later,     when we know the address of the .got section.  */  if (h->type == STT_FUNC      || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)    {      if ((! info->shared	   && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0	   && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)	  || (info->shared && h->plt.refcount <= 0))	{	  /* This case can occur if we saw a PLT32 reloc in an input	     file, but the symbol was never referred to by a dynamic	     object, or if all references were garbage collected.  In	     such a case, we don't actually need to build a procedure	     linkage table, and we can just do a PC32 reloc instead.  */	  h->plt.offset = (bfd_vma) -1;	  h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;	  return true;	}      /* Make sure this symbol is output as a dynamic symbol.  */      if (h->dynindx == -1)	{	  if (! bfd_elf64_link_record_dynamic_symbol (info, h))	    return false;	}      s = bfd_get_section_by_name (dynobj, ".plt");      BFD_ASSERT (s != NULL);      /* If this is the first .plt entry, make room for the special	 first entry.  */      if (s->_raw_size == 0)	s->_raw_size = PLT_ENTRY_SIZE;      /* If this symbol is not defined in a regular file, and we are	 not generating a shared library, then set the symbol to this	 location in the .plt.	This is required to make function	 pointers compare as equal between the normal executable and	 the shared library.  */      if (! info->shared	  && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)	{	  h->root.u.def.section = s;	  h->root.u.def.value = s->_raw_size;	}      h->plt.offset = s->_raw_size;      /* Make room for this entry.  */      s->_raw_size += PLT_ENTRY_SIZE;      /* We also need to make an entry in the .got.plt section, which	 will be placed in the .got section by the linker script.  */      s = bfd_get_section_by_name (dynobj, ".got.plt");      BFD_ASSERT (s != NULL);      s->_raw_size += GOT_ENTRY_SIZE;      /* We also need to make an entry in the .rela.plt section.  */      s = bfd_get_section_by_name (dynobj, ".rela.plt");      BFD_ASSERT (s != NULL);      s->_raw_size += sizeof (Elf64_External_Rela);      return true;    }  /* If this is a weak symbol, and there is a real definition, the     processor independent code will have arranged for us to see the     real definition first, and we can just use the same value.	 */  if (h->weakdef != NULL)    {      BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined		  || h->weakdef->root.type == bfd_link_hash_defweak);      h->root.u.def.section = h->weakdef->root.u.def.section;      h->root.u.def.value = h->weakdef->root.u.def.value;      return true;    }  /* This is a reference to a symbol defined by a dynamic object which     is not a function.	 */  /* If we are creating a shared library, we must presume that the     only references to the symbol are via the global offset table.     For such cases we need not do anything here; the relocations will     be handled correctly by relocate_section.	*/  if (info->shared)    return true;  /* If there are no references to this symbol that do not use the     GOT, we don't need to generate a copy reloc.  */  if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)    return true;  /* We must allocate the symbol in our .dynbss section, which will     become part of the .bss section of the executable.	 There will be     an entry for this symbol in the .dynsym section.  The dynamic     object will contain position independent code, so all references     from the dynamic object to this symbol will go through the global     offset table.  The dynamic linker will use the .dynsym entry to     determine the address it must put in the global offset table, so     both the dynamic object and the regular object will refer to the     same memory location for the variable.  */  s = bfd_get_section_by_name (dynobj, ".dynbss");  BFD_ASSERT (s != NULL);  /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker     to copy the initial value out of the dynamic object and into the     runtime process image.  We need to remember the offset into the     .rela.bss section we are going to use.  */  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)    {      asection *srel;      srel = bfd_get_section_by_name (dynobj, ".rela.bss");      BFD_ASSERT (srel != NULL);      srel->_raw_size += sizeof (Elf64_External_Rela);      h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;    }  /* We need to figure out the alignment required for this symbol.  I     have no idea how ELF linkers handle this.	16-bytes is the size     of the largest type that requires hard alignment -- long double.  */  /* FIXME: This is VERY ugly. Should be fixed for all architectures using     this construct.  */  power_of_two = bfd_log2 (h->size);  if (power_of_two > 4)    power_of_two = 4;  /* Apply the required alignment.  */  s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));  if (power_of_two > bfd_get_section_alignment (dynobj, s))    {      if (! bfd_set_section_alignment (dynobj, s, power_of_two))	return false;    }  /* Define the symbol as being at this point in the section.  */  h->root.u.def.section = s;  h->root.u.def.value = s->_raw_size;  /* Increment the section size to make room for the symbol.  */  s->_raw_size += h->size;  return true;}/* Set the sizes of the dynamic sections.  */static booleanelf64_x86_64_size_dynamic_sections (output_bfd, info)     bfd *output_bfd;     struct bfd_link_info *info;{  bfd *dynobj;  asection *s;  boolean plt;  boolean relocs;  boolean reltext;  dynobj = elf_hash_table (info)->dynobj;  BFD_ASSERT (dynobj != NULL);  if (elf_hash_table (info)->dynamic_sections_created)    {      /* Set the contents of the .interp section to the interpreter.  */      if (! info->shared)	{	  s = bfd_get_section_by_name (dynobj, ".interp");	  BFD_ASSERT (s != NULL);	  s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;	}    }  else    {      /* We may have created entries in the .rela.got section.	 However, if we are not creating the dynamic sections, we will	 not actually use these entries.  Reset the size of .rela.got,	 which will cause it to get stripped from the output file	 below.	 */      s = bfd_get_section_by_name (dynobj, ".rela.got");      if (s != NULL)	s->_raw_size = 0;    }  /* If this is a -Bsymbolic shared link, then we need to discard all     PC relative relocs against symbols defined in a regular object.     We allocated space for them in the check_relocs routine, but we     will not fill them in in the relocate_section routine.  */  if (info->shared)    elf64_x86_64_link_hash_traverse (elf64_x86_64_hash_table (info),				     elf64_x86_64_discard_copies,				     (PTR) info);  /* The check_relocs and adjust_dynamic_symbol entry points have     determined the sizes of the various dynamic sections.  Allocate     memory for them.  */  plt = relocs = reltext = false;  for (s = dynobj->sections; s != NULL; s = s->next)    {      const char *name;      boolean strip;      if ((s->flags & SEC_LINKER_CREATED) == 0)	continue;      /* It's OK to base decisions on the section name, because none	 of the dynobj section names depend upon the input files.  */      name = bfd_get_section_name (dynobj, s);      strip = false;      if (strcmp (name, ".plt") == 0)	{	  if (s->_raw_size == 0)	    {	      /* Strip this section if we don't need it; see the		 comment below.	 */	      strip = true;	    }	  else	    {	      /* Remember whether there is a PLT.  */	      plt = true;

⌨️ 快捷键说明

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