elf32-m68k.c

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

C
2,163
字号
      switch (ELF32_R_TYPE (rel->r_info))	{	case R_68K_GOT8:	case R_68K_GOT16:	case R_68K_GOT32:	case R_68K_GOT8O:	case R_68K_GOT16O:	case R_68K_GOT32O:	  r_symndx = ELF32_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;		  if (h->got.refcount == 0)		    {		      /* We don't need the .got entry any more.  */		      sgot->_raw_size -= 4;		      srelgot->_raw_size -= sizeof (Elf32_External_Rela);		    }		}	    }	  else if (local_got_refcounts != NULL)	    {	      if (local_got_refcounts[r_symndx] > 0)		{		  --local_got_refcounts[r_symndx];		  if (local_got_refcounts[r_symndx] == 0)		    {		      /* We don't need the .got entry any more.  */		      sgot->_raw_size -= 4;		      if (info->shared)			srelgot->_raw_size -= sizeof (Elf32_External_Rela);		    }		}	    }	  break;	case R_68K_PLT8:	case R_68K_PLT16:	case R_68K_PLT32:	case R_68K_PLT8O:	case R_68K_PLT16O:	case R_68K_PLT32O:	case R_68K_PC8:	case R_68K_PC16:	case R_68K_PC32:	case R_68K_8:	case R_68K_16:	case R_68K_32:	  r_symndx = ELF32_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;	    }	  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 booleanelf_m68k_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	  /* We must always create the plt entry if it was referenced	     by a PLTxxO relocation.  In this case we already recorded	     it as a dynamic symbol.  */	  && h->dynindx == -1)	{	  /* This case can occur if we saw a PLTxx reloc in an input	     file, but the symbol was never referred to by a dynamic	     object.  In such a case, we don't actually need to build	     a procedure linkage table, and we can just do a PCxx	     reloc instead.  */	  BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);	  h->plt.offset = (bfd_vma) -1;	  return true;	}      /* GC may have rendered this entry unused.  */      if (h->plt.refcount <= 0)	{	  h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;	  h->plt.offset = (bfd_vma) -1;	  return true;	}      /* Make sure this symbol is output as a dynamic symbol.  */      if (h->dynindx == -1)	{	  if (! bfd_elf32_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)	{	  if (CPU32_FLAG (dynobj))	    s->_raw_size += PLT_CPU32_ENTRY_SIZE;	  else	    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.  */      if (CPU32_FLAG (dynobj))        s->_raw_size += PLT_CPU32_ENTRY_SIZE;      else        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 += 4;      /* 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;    }  /* Reinitialize the plt offset now that it is not used as a reference     count any more.  */  h->plt.offset = (bfd_vma) -1;  /* 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;  /* 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_68K_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 (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 booleanelf_m68k_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 && info->symbolic)    elf_m68k_link_hash_traverse (elf_m68k_hash_table (info),				 elf_m68k_discard_copies,				 (PTR) NULL);  /* The check_relocs and adjust_dynamic_symbol entry points have     determined the sizes of the various dynamic sections.  Allocate     memory for them.  */  plt = false;  relocs = false;  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;	    }	}      else 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 mostly to handle .rela.bss and		 .rela.plt.  We must create both sections in		 create_dynamic_sections, because they 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	    {	      asection *target;	      /* Remember whether there are any reloc sections other                 than .rela.plt.  */	      if (strcmp (name, ".rela.plt") != 0)		{		  const char *outname;		  relocs = true;		  /* If this relocation section applies to a read only		     section, then we probably need a DT_TEXTREL		     entry.  .rela.plt is actually associated with		     .got.plt, which is never readonly.  */		  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;		}	      /* 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 (strncmp (name, ".got", 4) != 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 elf_m68k_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 (plt)	{	  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;	}

⌨️ 快捷键说明

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