/*
** sht.c for elfsh
** 
** Started on  Fri Nov  2 15:17:58 2001 mayhem
** Last update Sun Mar 17 02:31:29 2002 mayhem
*/
#include "elfsh.h"









/* Display The Section header table (SHT) */
int		display_sht(Elfsh32_Obj *file, argv_t *args)
{
   Elf32_Shdr	*shdr;
   regex_t	*tmp;
   u_int	num;
   u_int	index;
   u_int	typenum;
   char		*name;
   char		*type;
   char		buff[256];

   if ((shdr = elfsh_get_sht(file, &num)) == NULL)
     QUIT(-1);

   tmp = (REGX_IS_USED(args->sht_use_regx) ? &args->sht_regx : 
	  REGX_IS_USED(args->gl_use_regx)  ? &args->gl_regx  : NULL);
   
   printf("\n\n[SECTION HEADER TABLE (SHT) for object %s] \n", file->name);
   
   for (index = 0; index < num; index++)
     {

       typenum = elfsh_get_section_type(&shdr[index]);
       type = (typenum >= SHTYPE_MAX ? "Unknown" : elfsh_sh_type[typenum]);
       
       if ((name = elfsh_get_section_name(file, &shdr[index])) == NULL && index)
	 RET(-1);
       
       
       snprintf(buff, sizeof(buff), 
		" [%02u] %-10p %c%c%c%c%c%c%c %-30s "
		"foffset(%08u) size(%08u) link(%02u) info(%04u) entsize(%04u) align(%04u) => %s",
		index, (void *) elfsh_get_section_vaddr(shdr + index),
		(elfsh_section_is_allocatable(shdr + index) ? 'a' : '-'),
		(elfsh_section_is_writable(shdr + index)    ? 'w' : '-'),
		(elfsh_section_is_executable(shdr + index)  ? 'x' : '-'),
		(elfsh_section_is_mergeable(shdr + index)   ? 'm' : '-'),
		(elfsh_section_is_strtab(shdr + index)      ? 's' : '-'),
		(elfsh_section_has_link(shdr + index)       ? 'l' : '-'),
		(elfsh_section_has_order(shdr + index)      ? 'o' : '-'),
		(name ? name : ELFSH_NULL_STRING),
		elfsh_get_section_foffset(shdr + index),
		elfsh_get_section_size(shdr + index),
		elfsh_get_section_link(shdr + index),
		elfsh_get_section_info(shdr + index),
		elfsh_get_section_entsize(shdr + index),
		elfsh_get_section_align(shdr + index),
		type);

	 if (!tmp || (tmp && !regexec(tmp, buff, 0, 0, 0)))
	   puts(buff);

     }

   puts("");
   return (0);
}
