/*
** got.c for elfsh
** 
** Started on  Fri Nov  2 15:20:18 2001 mayhem
** Last update Thu Feb 28 18:36:50 2002 mayhem
*/
#include "elfsh.h"


int		display_got(Elfsh32_Obj *file, argv_t *av)
{
  regex_t	*tmp;
  int		*got;
  int		size;
  int		index;
  int		offset;
  char		*name;
  char		off[30];
  char		buff[256];

  tmp = (REGX_IS_USED(av->got_use_regx) ? &av->got_regx : 
	 REGX_IS_USED(av->gl_use_regx)  ? &av->gl_regx  : NULL);

  if ((got = elfsh_get_got(file, &size)) == NULL)
    RET(-1);
  
  puts(" \n[Global Offset Table]");
  off[0] = 0;

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

      name = elfsh_reverse_symbol(file, got[index], &offset);

      if (!name)
	name = elfsh_reverse_dynsymbol(file, got[index], &offset);

      if (off)
	snprintf(off, sizeof(off), " %c %u", (offset < 0 ? '-' : '+'), 
		 (offset > 0 ? offset : offset - offset - offset));
      else
	off[0] = 0;

      snprintf(buff, sizeof(buff), "\t\t\t [%02u] %10p \t\t <%s%s>", 
	       index, (void *) got[index], (name ? name : "?"), 
	       (off[0] && name && offset ? off : ""));

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

    }

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


