FreeBSD/contrib/binutils/bfd/elf.c
/* Create a new bfd section from an ELF program header.

   Since program segments have no names, we generate a synthetic name
   of the form segment<NUM>, where NUM is generally the index in the
   program header table.  For segments that are split (see below) we
   generate the names segment<NUM>a and segment<NUM>b.

   Note that some program segments may have a file size that is different than
   (less than) the memory size.  All this means is that at execution the
   system must allocate the amount of memory specified by the memory size,
   but only initialize it with the first "file size" bytes read from the
   file.  This would occur for example, with program segments consisting
   of combined data+bss.

   To handle the above situation, this routine generates TWO bfd sections
   for the single program segment.  The first has the length specified by
   the file size of the segment, and the second has the length specified
   by the difference between the two sizes.  In effect, the segment is split
   into it's initialized and uninitialized parts.

 */
Create a new bfd section from an ELF program header.
Since program segments have no names, we generate a synthetic name
of the form segment, where NUM is generally the index in the
program header table.  For segments that are split  we
generate the names segmenta and segmentb.

Note that some program segments may have a file size that is different than
(less than) the memory size.  All this means is that at execution the
system must allocate the amount of memory specified by the memory size,
but only initialize it with the first "file size" bytes read from the
file.  This would occur for example, with program segments consisting
of combined data+bss.

To handle the above situation, this routine generates TWO bfd sections
for the single program segment.  The first has the length specified by
the file size of the segment, and the second has the length specified
by the difference between the two sizes.  In effect, the segment is split
into it's initialized and uninitialized parts.
Create a new bfd section from an ELF program header . Since program segments have no names we generate a synthetic name of the form segment where NUM is generally the index in the program header table . For segments that are split we generate the names segmenta and segmentb . Note that some program segments may have a file size that is different than ( less than ) the memory size . All this means is that at execution the system must allocate the amount of memory specified by the memory size but only initialize it with the first " file size " bytes read from the file . This would occur for example with program segments consisting of combined data + bss . To handle the above situation this routine generates TWO bfd sections for the single program segment . The first has the length specified by the file size of the segment and the second has the length specified by the difference between the two sizes . In effect the segment is split into it ' s initialized and uninitialized parts .
bfd_boolean
_bfd_elf_make_section_from_phdr (bfd *abfd,
				 Elf_Internal_Phdr *hdr,
				 int index,
				 const char *typename)
{
  asection *newsect;
  char *name;
  char namebuf[64];
  size_t len;
  int split;
  split = ((hdr->p_memsz > 0)
	    && (hdr->p_filesz > 0)
	    && (hdr->p_memsz > hdr->p_filesz));
  sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
  len = strlen (namebuf) + 1;
  name = bfd_alloc (abfd, len);
  if (!name)
    return FALSE;
  memcpy (name, namebuf, len);
  newsect = bfd_make_section (abfd, name);
  if (newsect == NULL)
    return FALSE;
  newsect->vma = hdr->p_vaddr;
  newsect->lma = hdr->p_paddr;
  newsect->size = hdr->p_filesz;
  newsect->filepos = hdr->p_offset;
  newsect->flags |= SEC_HAS_CONTENTS;
  newsect->alignment_power = bfd_log2 (hdr->p_align);
  if (hdr->p_type == PT_LOAD)
    {
      newsect->flags |= SEC_ALLOC;
      newsect->flags |= SEC_LOAD;
      if (hdr->p_flags & PF_X)
	{
	  newsect->flags |= SEC_CODE;
	}
    }
  if (!(hdr->p_flags & PF_W))
    {
      newsect->flags |= SEC_READONLY;
    }
  if (split)
    {
      sprintf (namebuf, "%s%db", typename, index);
      len = strlen (namebuf) + 1;
      name = bfd_alloc (abfd, len);
      if (!name)
	return FALSE;
      memcpy (name, namebuf, len);
      newsect = bfd_make_section (abfd, name);
      if (newsect == NULL)
	return FALSE;
      newsect->vma = hdr->p_vaddr + hdr->p_filesz;
      newsect->lma = hdr->p_paddr + hdr->p_filesz;
      newsect->size = hdr->p_memsz - hdr->p_filesz;
      if (hdr->p_type == PT_LOAD)
	{
	  newsect->flags |= SEC_ALLOC;
	  if (hdr->p_flags & PF_X)
	    newsect->flags |= SEC_CODE;
	}
      if (!(hdr->p_flags & PF_W))
	newsect->flags |= SEC_READONLY;
    }
  return TRUE;
}
bfd_boolean _bfd_elf_make_section_from_phdr ( bfd * abfd , Elf_Internal_Phdr * hdr , int index , const char * typename ) { asection * newsect ; char * name ; char namebuf [ 64 ] ; size_t len ; int split ; split = ( ( hdr -> p_memsz > 0 ) && ( hdr -> p_filesz > 0 ) && ( hdr -> p_memsz > hdr -> p_filesz ) ) ; sprintf ( namebuf , " " , typename , index , split ? " " : " " ) ; len = strlen ( namebuf ) + 1 ; name = bfd_alloc ( abfd , len ) ; if ( ! name ) return FALSE ; memcpy ( name , namebuf , len ) ; newsect = bfd_make_section ( abfd , name ) ; if ( newsect == NULL ) return FALSE ; newsect -> vma = hdr -> p_vaddr ; newsect -> lma = hdr -> p_paddr ; newsect -> size = hdr -> p_filesz ; newsect -> filepos = hdr -> p_offset ; newsect -> flags |= SEC_HAS_CONTENTS ; newsect -> alignment_power = bfd_log2 ( hdr -> p_align ) ; if ( hdr -> p_type == PT_LOAD ) { newsect -> flags |= SEC_ALLOC ; newsect -> flags |= SEC_LOAD ; if ( hdr -> p_flags & PF_X ) { newsect -> flags |= SEC_CODE ; } } if ( ! ( hdr -> p_flags & PF_W ) ) { newsect -> flags |= SEC_READONLY ; } if ( split ) { sprintf ( namebuf , " " , typename , index ) ; len = strlen ( namebuf ) + 1 ; name = bfd_alloc ( abfd , len ) ; if ( ! name ) return FALSE ; memcpy ( name , namebuf , len ) ; newsect = bfd_make_section ( abfd , name ) ; if ( newsect == NULL ) return FALSE ; newsect -> vma = hdr -> p_vaddr + hdr -> p_filesz ; newsect -> lma = hdr -> p_paddr + hdr -> p_filesz ; newsect -> size = hdr -> p_memsz - hdr -> p_filesz ; if ( hdr -> p_type == PT_LOAD ) { newsect -> flags |= SEC_ALLOC ; if ( hdr -> p_flags & PF_X ) newsect -> flags |= SEC_CODE ; } if ( ! ( hdr -> p_flags & PF_W ) ) newsect -> flags |= SEC_READONLY ; } return TRUE ; }
Create a new bfd section from an ELF program header.
Create a new bfd section from an ELF program header .
/* FIXME: all we known is that it has execute PERMISSION,
	     may be data.  */