elf32-sparc.c

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

C
2,014
字号
       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 .rela.plt section.  */      s = bfd_get_section_by_name (dynobj, ".rela.plt");      BFD_ASSERT (s != NULL);      s->_raw_size += sizeof (Elf32_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_SPARC_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     .rel.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 (Elf32_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.  */  power_of_two = bfd_log2 (h->size);  if (power_of_two > 3)    power_of_two = 3;  /* 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 booleanelf32_sparc_size_dynamic_sections (output_bfd, info)     bfd *output_bfd;     struct bfd_link_info *info;{  bfd *dynobj;  asection *s;  boolean reltext;  boolean relplt;  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;	}      /* Make space for the trailing nop in .plt.  */      s = bfd_get_section_by_name (dynobj, ".plt");      BFD_ASSERT (s != NULL);      if (s->_raw_size > 0)	s->_raw_size += 4;    }  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;    }  /* The check_relocs and adjust_dynamic_symbol entry points have     determined the sizes of the various dynamic sections.  Allocate     memory for them.  */  reltext = false;  relplt = 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 (strncmp (name, ".rela", 5) == 0)	{	  if (s->_raw_size == 0)	    {	      /* If we don't need this section, strip it from the		 output file.  This is to handle .rela.bss and		 .rel.plt.  We must create it in		 create_dynamic_sections, because it must be created		 before the linker maps input sections to output		 sections.  The linker does that before		 adjust_dynamic_symbol is called, and it is that		 function which decides whether anything needs to go		 into these sections.  */	      strip = true;	    }	  else	    {	      const char *outname;	      asection *target;	      /* If this relocation section applies to a read only		 section, then we probably need a DT_TEXTREL entry.  */	      outname = bfd_get_section_name (output_bfd,					      s->output_section);	      target = bfd_get_section_by_name (output_bfd, outname + 5);	      if (target != NULL		  && (target->flags & SEC_READONLY) != 0		  && (target->flags & SEC_ALLOC) != 0)		reltext = true;	      if (strcmp (name, ".rela.plt") == 0)		relplt = true;	      /* We use the reloc_count field as a counter if we need		 to copy relocs into the output file.  */	      s->reloc_count = 0;	    }	}      else if (strcmp (name, ".plt") != 0	       && strcmp (name, ".got") != 0)	{	  /* It's not one of our sections, so don't allocate space.  */	  continue;	}      if (strip)	{	  _bfd_strip_section_from_output (info, s);	  continue;	}      /* Allocate memory for the section contents.  */      /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.	 Unused entries should be reclaimed before the section's contents	 are written out, but at the moment this does not happen.  Thus in	 order to prevent writing out garbage, we initialise the section's	 contents to zero.  */      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);      if (s->contents == NULL && s->_raw_size != 0)	return false;    }  if (elf_hash_table (info)->dynamic_sections_created)    {      /* Add some entries to the .dynamic section.  We fill in the	 values later, in elf32_sparc_finish_dynamic_sections, but we	 must add the entries now so that we get the correct size for	 the .dynamic section.  The DT_DEBUG entry is filled in by the	 dynamic linker and used by the debugger.  */      if (! info->shared)	{	  if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))	    return false;	}      if (relplt)	{	  if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)	      || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)	      || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)	      || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))	    return false;	}      if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)	  || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)	  || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,					    sizeof (Elf32_External_Rela)))	return false;      if (reltext)	{	  if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))	    return false;	  info->flags |= DF_TEXTREL;	}    }  return true;}#define SET_SEC_DO_RELAX(section) do { elf_section_data(section)->tdata = (void *)1; } while (0)#define SEC_DO_RELAX(section) (elf_section_data(section)->tdata == (void *)1)static booleanelf32_sparc_relax_section (abfd, section, link_info, again)     bfd *abfd ATTRIBUTE_UNUSED;     asection *section ATTRIBUTE_UNUSED;     struct bfd_link_info *link_info ATTRIBUTE_UNUSED;     boolean *again;{  *again = false;  SET_SEC_DO_RELAX (section);  return true;}/* Relocate a SPARC ELF section.  */static booleanelf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,			      contents, relocs, local_syms, local_sections)     bfd *output_bfd;     struct bfd_link_info *info;     bfd *input_bfd;     asection *input_section;     bfd_byte *contents;     Elf_Internal_Rela *relocs;     Elf_Internal_Sym *local_syms;     asection **local_sections;{  bfd *dynobj;  Elf_Internal_Shdr *symtab_hdr;  struct elf_link_hash_entry **sym_hashes;  bfd_vma *local_got_offsets;  bfd_vma got_base;  asection *sgot;  asection *splt;  asection *sreloc;  Elf_Internal_Rela *rel;  Elf_Internal_Rela *relend;  dynobj = elf_hash_table (info)->dynobj;  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;  sym_hashes = elf_sym_hashes (input_bfd);  local_got_offsets = elf_local_got_offsets (input_bfd);  if (elf_hash_table (info)->hgot == NULL)    got_base = 0;  else    got_base = elf_hash_table (info)->hgot->root.u.def.value;  sgot = NULL;  splt = NULL;  sreloc = NULL;  rel = relocs;  relend = relocs + input_section->reloc_count;  for (; rel < relend; rel++)    {      int r_type;      reloc_howto_type *howto;      unsigned long r_symndx;      struct elf_link_hash_entry *h;      Elf_Internal_Sym *sym;      asection *sec;      bfd_vma relocation;      bfd_reloc_status_type r;      r_type = ELF32_R_TYPE (rel->r_info);      if (r_type == R_SPARC_GNU_VTINHERIT          || r_type == R_SPARC_GNU_VTENTRY)        continue;      if (r_type < 0 || r_type >= (int) R_SPARC_max_std)	{	  bfd_set_error (bfd_error_bad_value);	  return false;	}      howto = _bfd_sparc_elf_howto_table + r_type;      r_symndx = ELF32_R_SYM (rel->r_info);      if (info->relocateable)	{	  /* This is a relocateable link.  We don't have to change	     anything, unless the reloc is against a section symbol,	     in which case we have to adjust according to where the	     section symbol winds up in the output section.  */	  if (r_symndx < symtab_hdr->sh_info)	    {	      sym = local_syms + r_symndx;	      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)		{		  sec = local_sections[r_symndx];		  rel->r_addend += sec->output_offset + sym->st_value;		}	    }	  continue;	}      /* This is a final link.  */      h = NULL;      sym = NULL;      sec = NULL;      if (r_symndx < symtab_hdr->sh_info)	{	  sym = local_syms + r_symndx;	  sec = local_sections[r_symndx];	  relocation = (sec->output_section->vma			+ sec->output_offset			+ sym->st_value);	}      else	{	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];	  while (h->root.type == bfd_link_hash_indirect		 || h->root.type == bfd_link_hash_warning)	    h = (struct elf_link_hash_entry *) h->root.u.i.link;	  if (h->root.type == bfd_link_hash_defined	      || h->root.type == bfd_link_hash_defweak)	    {	      sec = h->root.u.def.section;	      if ((r_type == R_SPARC_WPLT30		   && h->plt.offset != (bfd_vma) -1)		  || ((r_type == R_SPARC_GOT10		       || r_type == R_SPARC_GOT13		       || r_type == R_SPARC_GOT22)		      && elf_hash_table (info)->dynamic_sections_created		      && (! info->shared			  || (! info->symbolic && h->dynindx != -1)			  || (h->elf_link_hash_flags			      & ELF_LINK_HASH_DEF_REGULAR) == 0))		  || (info->shared		      && ((! info->symbolic && h->dynindx != -1)			  || (h->elf_link_hash_flags			      & ELF_LINK_HASH_DEF_REGULAR) == 0)

⌨️ 快捷键说明

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