/*
** fixup.c for elfsh
** 
** Started on  Fri Jul 27 04:56:06 2001 mayhem
** Last update Thu Mar 21 04:11:06 2002 mayhem
*/
#include "libelfsh.h"


/* Put the bss physically in the file */
int		elfsh_fixup_bss(Elfsh32_Obj *file)
{
  Elfsh32_Sect	*bss;
  Elfsh32_Sect	*actual;

  /* Find the bss and increase his size in the file if necessary */
  bss = elfsh_get_section_by_name(file, ELFSH_SECTION_NAME_BSS, NULL, NULL, NULL);
  if (!bss)
    ELFSH_SETERROR("libelfsh: Cant find .bss section . \n", -1);
  
  /* Add the bss data in the file in order to be able to insert sections after it */
  if (!bss->data)
    {
      XALLOC(bss->data, bss->shdr->sh_size, -1);

      /* Extend the segment for .bss */
      bss->phdr->p_filesz += bss->shdr->sh_size;

      /* Update the file offset for the section header table if necessary */
      if (file->hdr->e_shoff > bss->shdr->sh_offset)
	file->hdr->e_shoff += bss->shdr->sh_size;

      /* Update the file offset for all next sections */
      for (actual = bss->next; actual; actual = actual->next)
	actual->shdr->sh_offset += bss->shdr->sh_size;
    }

  return (0);
}

