elf32-ppc.c

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

C
1,931
字号
  /* If this is the first time, create the section */  lsect = elf_linker_section (dynobj, which);  if (!lsect)    {      elf_linker_section_t defaults;      static elf_linker_section_t zero_section;      defaults = zero_section;      defaults.which = which;      defaults.hole_written_p = false;      defaults.alignment = 2;      /* Both of these sections are (technically) created by the user	 putting data in them, so they shouldn't be marked	 SEC_LINKER_CREATED.	 The linker creates them so it has somewhere to attach their	 respective symbols. In fact, if they were empty it would	 be OK to leave the symbol set to 0 (or any random number), because	 the appropriate register should never be used.  */      defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS			| SEC_IN_MEMORY);      switch (which)	{	default:	  (*_bfd_error_handler) (_("%s: Unknown special linker type %d"),				 bfd_get_filename (abfd),				 (int) which);	  bfd_set_error (bfd_error_bad_value);	  return (elf_linker_section_t *) 0;	case LINKER_SECTION_SDATA:	/* .sdata/.sbss section */	  defaults.name		  = ".sdata";	  defaults.rel_name	  = ".rela.sdata";	  defaults.bss_name	  = ".sbss";	  defaults.sym_name	  = "_SDA_BASE_";	  defaults.sym_offset	  = 32768;	  break;	case LINKER_SECTION_SDATA2:	/* .sdata2/.sbss2 section */	  defaults.name		  = ".sdata2";	  defaults.rel_name	  = ".rela.sdata2";	  defaults.bss_name	  = ".sbss2";	  defaults.sym_name	  = "_SDA2_BASE_";	  defaults.sym_offset	  = 32768;	  defaults.flags	 |= SEC_READONLY;	  break;	}      lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);    }  return lsect;}/* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we   need to bump up the number of section headers.  */static intppc_elf_additional_program_headers (abfd)     bfd *abfd;{  asection *s;  int ret;  ret = 0;  s = bfd_get_section_by_name (abfd, ".interp");  if (s != NULL)    ++ret;  s = bfd_get_section_by_name (abfd, ".sbss2");  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)    ++ret;  s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)    ++ret;  return ret;}/* Modify the segment map if needed.  */static booleanppc_elf_modify_segment_map (abfd)     bfd *abfd ATTRIBUTE_UNUSED;{  return true;}/* We have to create .dynsbss and .rela.sbss here so that they get mapped   to output sections (just like _bfd_elf_create_dynamic_sections has   to create .dynbss and .rela.bss).  */static booleanppc_elf_create_dynamic_sections (abfd, info)     bfd *abfd;     struct bfd_link_info *info;{  register asection *s;  flagword flags;  if (!_bfd_elf_create_dynamic_sections (abfd, info))    return false;  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY	   | SEC_LINKER_CREATED);  s = bfd_make_section (abfd, ".dynsbss");  if (s == NULL      || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))    return false;  if (! info->shared)    {      s = bfd_make_section (abfd, ".rela.sbss");      if (s == NULL	  || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)	  || ! bfd_set_section_alignment (abfd, s, 2))	return false;    }  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 booleanppc_elf_adjust_dynamic_symbol (info, h)     struct bfd_link_info *info;     struct elf_link_hash_entry *h;{  bfd *dynobj = elf_hash_table (info)->dynobj;  asection *s;  unsigned int power_of_two;  bfd_vma plt_offset;#ifdef DEBUG  fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n", h->root.root.string);#endif  /* 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 (! elf_hash_table (info)->dynamic_sections_created 	  || SYMBOL_CALLS_LOCAL (info, h)	  || (info->shared && h->plt.refcount <= 0))	{	  /* A PLT entry is not required/allowed when:	     1. We are not using ld.so; because then the PLT entry	     can't be set up, so we can't use one.	     2. We know for certain that a call to this symbol	     will go to this object.	     3. GC has rendered the entry unused.	     Note, however, that in an executable all references to the	     symbol go to the PLT, so we can't turn it off in that case.	     ??? The correct thing to do here is to reference count	     all uses of the symbol, not just those to the GOT or PLT.  */	  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_elf32_link_record_dynamic_symbol (info, h))	    return false;	}      BFD_ASSERT (h->dynindx != -1);      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_INITIAL_ENTRY_SIZE;      /* The PowerPC PLT is actually composed of two parts, the first part	 is 2 words (for a load and a jump), and then there is a remaining	 word available at the end.  */      plt_offset = (PLT_INITIAL_ENTRY_SIZE		    + (PLT_SLOT_SIZE		       * ((s->_raw_size - PLT_INITIAL_ENTRY_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 = plt_offset;	}      h->plt.offset = plt_offset;      /* Make room for this entry.  After the 8192nd entry, room         for two entries is allocated.  */      if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE	  >= PLT_NUM_SINGLE_ENTRIES)	s->_raw_size += 2 * PLT_ENTRY_SIZE;      else	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;  /* 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.     Of course, if the symbol is sufficiently small, we must instead     allocate it in .sbss.  FIXME: It would be better to do this if and     only if there were actually SDAREL relocs for that symbol.  */  if (h->size <= elf_gp_size (dynobj))    s = bfd_get_section_by_name (dynobj, ".dynsbss");  else    s = bfd_get_section_by_name (dynobj, ".dynbss");  BFD_ASSERT (s != NULL);  /* We must generate a R_PPC_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;      if (h->size <= elf_gp_size (dynobj))	srel = bfd_get_section_by_name (dynobj, ".rela.sbss");      else	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 > 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 booleanppc_elf_size_dynamic_sections (output_bfd, info)     bfd *output_bfd;     struct bfd_link_info *info;{  bfd *dynobj;  asection *s;  boolean plt;  boolean relocs;  boolean reltext;#ifdef DEBUG  fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");#endif  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, .rela.sdata, and	 .rela.sdata2 sections.  However, if we are not creating the	 dynamic sections, we will not actually use these entries.  Reset	 the size of .rela.got, et al, which will cause it to get	 stripped from the output file below.  */      static char *rela_sections[] = { ".rela.got", ".rela.sdata",				       ".rela.sdata2", ".rela.sbss",				       (char *) 0 };      char **p;      for (p = rela_sections; *p != (char *) 0; p++)	{	  s = bfd_get_section_by_name (dynobj, *p);	  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.  */  plt = false;  relocs = false;  reltext = false;  for (s = dynobj->sectio

⌨️ 快捷键说明

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