/*
** ctors.c for elfsh
** 
** Started on  Tue Feb 26 22:07:58 2002 mayhem
** Last update Thu Feb 28 18:37:06 2002 mayhem
*/
#include "elfsh.h"




/* Display the constructor section */
int		display_ctors(Elfsh32_Obj *file, argv_t *args)
{
  regex_t	*tmp;
  int		*ctors;
  int		size;
  int		index;
  int		offset;
  char		*name;
  char		off[30];
  char		buff[256];
  
  tmp = (REGX_IS_USED(args->ctors_use_regx) ? &args->ctors_regx : 
	 REGX_IS_USED(args->gl_use_regx)  ? &args->gl_regx  : NULL);

  
  if ((ctors = elfsh_get_ctors(file, &size)) == NULL)
    RET(-1);
  
  puts(" \n[.CTORS]");
  off[0] = 0;

  for (index = 0; index < size; index++)
    {

      name = elfsh_reverse_symbol(file, ctors[index], &offset);
      if (!name)
	name = elfsh_reverse_dynsymbol(file, ctors[index], &offset);
      else if (off)
	snprintf(off, sizeof(off), " %c %u", 
		 (offset < 0 ? '-' : '+'), 
		 (offset > 0 ? offset : offset - offset - offset));
      
      snprintf(buff, sizeof(buff), "\t\t\t [%02u] %10p \t\t <%s%s>", 
	       index, (void *) ctors[index], (name ? name : "?"), (name && offset ? off : ""));
      
      if (!tmp || (tmp && !regexec(tmp, buff, 0, 0, 0)))
	puts(buff);

    }


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