FreeBSD/contrib/binutils/bfd/elf.c
/* Work out the file positions of all the sections.  This is called by
   _bfd_elf_compute_section_file_positions.  All the section sizes and
   VMAs must be known before this is called.

   Reloc sections come in two flavours: Those processed specially as
   "side-channel" data attached to a section to which they apply, and
   those that bfd doesn't process as relocations.  The latter sort are
   stored in a normal bfd section by bfd_section_from_shdr.   We don't
   consider the former sort here, unless they form part of the loadable
   image.  Reloc sections not assigned here will be handled later by
   assign_file_positions_for_relocs.

   We also don't set the positions of the .symtab and .strtab here.  */
Work out the file positions of all the sections.  This is called by
_bfd_elf_compute_section_file_positions.  All the section sizes and
VMAs must be known before this is called.

Reloc sections come in two flavours: Those processed specially as
"side-channel" data attached to a section to which they apply, and
those that bfd doesn't process as relocations.  The latter sort are
stored in a normal bfd section by bfd_section_from_shdr.   We don't
consider the former sort here, unless they form part of the loadable
image.  Reloc sections not assigned here will be handled later by
assign_file_positions_for_relocs.

We also don't set the positions of the .symtab and .strtab here.
Work out the file positions of all the sections . This is called by _bfd_elf_compute_section_file_positions . All the section sizes and VMAs must be known before this is called . Reloc sections come in two flavours : Those processed specially as " side - channel " data attached to a section to which they apply and those that bfd doesn ' t process as relocations . The latter sort are stored in a normal bfd section by bfd_section_from_shdr . We don ' t consider the former sort here unless they form part of the loadable image . Reloc sections not assigned here will be handled later by assign_file_positions_for_relocs . We also don ' t set the positions of the . symtab and . strtab here .
static bfd_boolean
assign_file_positions_except_relocs (bfd *abfd,
				     struct bfd_link_info *link_info)
{
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
  file_ptr off;
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
      && bfd_get_format (abfd) != bfd_core)
    {
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
      unsigned int num_sec = elf_numsections (abfd);
      Elf_Internal_Shdr **hdrpp;
      unsigned int i;
      off = i_ehdrp->e_ehsize;
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
	{
	  Elf_Internal_Shdr *hdr;
	  hdr = *hdrpp;
	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
	       && hdr->bfd_section == NULL)
	      || i == tdata->symtab_section
	      || i == tdata->symtab_shndx_section
	      || i == tdata->strtab_section)
	    {
	      hdr->sh_offset = -1;
	    }
	  else
	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
	  if (i == SHN_LORESERVE - 1)
	    {
	      i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
	      hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
	    }
	}
    }
  else
    {
      unsigned int alloc;
      if (!assign_file_positions_for_load_sections (abfd, link_info))
	return FALSE;
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
	return FALSE;
      if (bed->elf_backend_modify_program_headers != NULL)
	{
	  if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
	    return FALSE;
	}
      alloc = tdata->program_header_size / bed->s->sizeof_phdr;
      if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
	return FALSE;
      off = tdata->next_file_pos;
    }
  off = align_file_position (off, 1 << bed->s->log_file_align);
  i_ehdrp->e_shoff = off;
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
  tdata->next_file_pos = off;
  return TRUE;
}
static bfd_boolean assign_file_positions_except_relocs ( bfd * abfd , struct bfd_link_info * link_info ) { struct elf_obj_tdata * tdata = elf_tdata ( abfd ) ; Elf_Internal_Ehdr * i_ehdrp = elf_elfheader ( abfd ) ; file_ptr off ; const struct elf_backend_data * bed = get_elf_backend_data ( abfd ) ; if ( ( abfd -> flags & ( EXEC_P | DYNAMIC ) ) == 0 && bfd_get_format ( abfd ) != bfd_core ) { Elf_Internal_Shdr * * const i_shdrpp = elf_elfsections ( abfd ) ; unsigned int num_sec = elf_numsections ( abfd ) ; Elf_Internal_Shdr * * hdrpp ; unsigned int i ; off = i_ehdrp -> e_ehsize ; for ( i = 1 , hdrpp = i_shdrpp + 1 ; i < num_sec ; i ++ , hdrpp ++ ) { Elf_Internal_Shdr * hdr ; hdr = * hdrpp ; if ( ( ( hdr -> sh_type == SHT_REL || hdr -> sh_type == SHT_RELA ) && hdr -> bfd_section == NULL ) || i == tdata -> symtab_section || i == tdata -> symtab_shndx_section || i == tdata -> strtab_section ) { hdr -> sh_offset = -1 ; } else off = _bfd_elf_assign_file_position_for_section ( hdr , off , TRUE ) ; if ( i == SHN_LORESERVE - 1 ) { i += SHN_HIRESERVE + 1 - SHN_LORESERVE ; hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE ; } } } else { unsigned int alloc ; if ( ! assign_file_positions_for_load_sections ( abfd , link_info ) ) return FALSE ; if ( ! assign_file_positions_for_non_load_sections ( abfd , link_info ) ) return FALSE ; if ( bed -> elf_backend_modify_program_headers != NULL ) { if ( ! ( * bed -> elf_backend_modify_program_headers ) ( abfd , link_info ) ) return FALSE ; } alloc = tdata -> program_header_size / bed -> s -> sizeof_phdr ; if ( bfd_seek ( abfd , ( bfd_signed_vma ) bed -> s -> sizeof_ehdr , SEEK_SET ) != 0 || bed -> s -> write_out_phdrs ( abfd , tdata -> phdr , alloc ) != 0 ) return FALSE ; off = tdata -> next_file_pos ; } off = align_file_position ( off , 1 << bed -> s -> log_file_align ) ; i_ehdrp -> e_shoff = off ; off += i_ehdrp -> e_shnum * i_ehdrp -> e_shentsize ; tdata -> next_file_pos = off ; return TRUE ; }
Work out the file positions of all the sections.
Work out the file positions of all the sections .
/* Start after the ELF header.  */ /* We are not creating an executable, which means that we are
	 not creating a program header, and that the actual order of
	 the sections in the file is unimportant.  */ /* Assign file positions for the loaded sections based on the
	 assignment of sections to segments.  */ /* And for non-load sections.  */ /* Write out the program headers.  */ /* Place the section headers.  */