/*
** reloc.c for elfsh
** 
** Started on  Tue Feb 27 14:34:50 2001 mayhem
** Last update Thu Feb 28 16:46:25 2002 mayhem
*/
#include "libelfsh.h"




/* 
** Return the linked list of t_rel structures . 
** Look at the elfsh.h file for further information .
*/
Elfsh32_Sect	*elfsh_get_reloc(Elfsh32_Obj *file, u_int range, char *name, u_int *nbr)
{
  Elfsh32_Sect	*s;
  int		index;
  int		size;

  s = elfsh_get_section_by_type(file, SHT_REL, &index, NULL, &size, range);
  if (!s)
    return (NULL);

  s->data = elfsh_load_section(file, s->shdr);
  s->nbr  = s->shdr->sh_size / sizeof(Elf32_Rel);
  
  if (nbr)
    *nbr = s->nbr;

  return (s);

}








/* Return the symbol name associated with the relocation entry */
Elf32_Sym	*elfsh_get_symbol_from_reloc(Elfsh32_Obj *file, Elf32_Rel *r)
{
  int		tmp;
  char		ismod;
  
  if (!file->hdr && !elfsh_get_hdr(file))
    return (NULL);
  
  tmp = ELF32_R_SYM(r->r_info);
  ismod = (file->hdr->e_type == ET_REL ? 1 : 0);
  
  if (ismod)
    {
      if (!file->symtab && !elfsh_get_symtab(file, NULL))
	return (NULL);
      return ((Elf32_Sym *) file->symtab->data + tmp);
    }
  else
    {
      if (!file->dynsym && !elfsh_get_dynsymtab(file, NULL))
	return (NULL);
      return ((Elf32_Sym *) file->dynsym->data + tmp);
    }

}









/* Return the relocation type */
u_int	elfsh_get_reloc_type(Elf32_Rel *r)
{
  int	t;
  
  return ((t = ELF32_R_TYPE(r->r_info)));
}





/* Return the relocation offset */
void	*elfsh_get_reloffset(Elfsh32_Obj *file, Elf32_Rel *r)
{
  return ((void *) r->r_offset);
}


























