elf32-i370.c

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

C
1,723
字号
  return true;}/* Handle an i370 specific section when reading an object file.  This   is called when elfcode.h finds a section with an unknown type.  *//* XXX hack alert bogus This routine is mostly all junk and almost * certainly does the wrong thing.  Its here simply because it does * just enough to allow glibc-2.1 ld.so to compile & link. */static booleani370_elf_section_from_shdr (abfd, hdr, name)     bfd *abfd;     Elf32_Internal_Shdr *hdr;     char *name;{  asection *newsect;  flagword flags;  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))    return false;  newsect = hdr->bfd_section;  flags = bfd_get_section_flags (abfd, newsect);  if (hdr->sh_flags & SHF_EXCLUDE)    flags |= SEC_EXCLUDE;  if (hdr->sh_type == SHT_ORDERED)    flags |= SEC_SORT_ENTRIES;  bfd_set_section_flags (abfd, newsect, flags);  return true;}/* Set up any other section flags and such that may be necessary.  *//* XXX hack alert bogus This routine is mostly all junk and almost * certainly does the wrong thing.  Its here simply because it does * just enough to allow glibc-2.1 ld.so to compile & link. */static booleani370_elf_fake_sections (abfd, shdr, asect)     bfd *abfd ATTRIBUTE_UNUSED;     Elf32_Internal_Shdr *shdr;     asection *asect;{  if ((asect->flags & SEC_EXCLUDE) != 0)    shdr->sh_flags |= SHF_EXCLUDE;  if ((asect->flags & SEC_SORT_ENTRIES) != 0)    shdr->sh_type = SHT_ORDERED;  return true;}#if 0/* Create a special linker section *//* XXX hack alert bogus This routine is mostly all junk and almost * certainly does the wrong thing.  Its here simply because it does * just enough to allow glibc-2.1 ld.so to compile & link. */static elf_linker_section_t *i370_elf_create_linker_section (abfd, info, which)     bfd *abfd;     struct bfd_link_info *info;     enum elf_linker_section_enum which;{  bfd *dynobj = elf_hash_table (info)->dynobj;  elf_linker_section_t *lsect;  /* Record the first bfd section that needs the special section */  if (!dynobj)    dynobj = elf_hash_table (info)->dynobj = abfd;  /* 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;}#endif/* 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).  *//* XXX hack alert bogus This routine is mostly all junk and almost * certainly does the wrong thing.  Its here simply because it does * just enough to allow glibc-2.1 ld.so to compile & link. */static booleani370_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;    }   /* xxx beats me, seem to need a rela.text ...  */   s = bfd_make_section (abfd, ".rela.text");   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.  *//* XXX hack alert bogus This routine is mostly all junk and almost * certainly does the wrong thing.  Its here simply because it does * just enough to allow glibc-2.1 ld.so to compile & link. */static booleani370_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;#ifdef DEBUG  fprintf (stderr, "i370_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)));  s = bfd_get_section_by_name (dynobj, ".rela.text");  BFD_ASSERT (s != NULL);  s->_raw_size += sizeof (Elf32_External_Rela);  /* 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_I370_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;}/* Increment the index of a dynamic symbol by a given amount.  Called   via elf_link_hash_traverse.  *//* XXX hack alert bogus This routine is mostly all junk and almost * certainly does the wrong thing.  Its here simply because it does * just enough to allow glibc-2.1 ld.so to compile & link. */static booleani370_elf_adjust_dynindx (h, cparg)     struct elf_link_hash_entry *h;     PTR cparg;{  int *cp = (int *) cparg;#ifdef DEBUG  fprintf (stderr,	   "i370_elf_adjust_dynindx called, h->dynindx = %d, *cp = %d\n",	   h->dynindx, *cp);#endif  if (h->dynindx != -1)    h->dynindx += *cp;  return true;}/* Set the sizes of the dynamic sections.  *//* XXX hack alert bogus This routine is mostly all junk and almost * certainly does the wrong thing.  Its here simply because it does * just enough to allow glibc-2.1 ld.so to compile & link. */static booleani370_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, "i370_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->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

⌨️ 快捷键说明

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