1/* Get REL relocation information at given index.
2   Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
3   Written by Ulrich Drepper <drepper@redhat.com>, 2000.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, version 2.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software Foundation,
16   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
22#include <gelf.h>
23#include <string.h>
24
25#include "libelfP.h"
26
27
28GElf_Rel *
29gelf_getrel (data, ndx, dst)
30     Elf_Data *data;
31     int ndx;
32     GElf_Rel *dst;
33{
34  Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
35  Elf_Scn *scn;
36  GElf_Rel *result;
37
38  if (data_scn == NULL)
39    return NULL;
40
41  if (unlikely (ndx < 0))
42    {
43      __libelf_seterrno (ELF_E_INVALID_INDEX);
44      return NULL;
45    }
46
47  if (unlikely (data_scn->d.d_type != ELF_T_REL))
48    {
49      __libelf_seterrno (ELF_E_INVALID_HANDLE);
50      return NULL;
51    }
52
53  /* This is the one place where we have to take advantage of the fact
54     that an `Elf_Data' pointer is also a pointer to `Elf_Data_Scn'.
55     The interface is broken so that it requires this hack.  */
56  scn = data_scn->s;
57
58  rwlock_rdlock (scn->elf->lock);
59
60  if (scn->elf->class == ELFCLASS32)
61    {
62      /* We have to convert the data.  */
63      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
64	{
65	  __libelf_seterrno (ELF_E_INVALID_INDEX);
66	  result = NULL;
67	}
68      else
69	{
70	  Elf32_Rel *src = &((Elf32_Rel *) data_scn->d.d_buf)[ndx];
71
72	  dst->r_offset = src->r_offset;
73	  dst->r_info = GELF_R_INFO (ELF32_R_SYM (src->r_info),
74				     ELF32_R_TYPE (src->r_info));
75
76	  result = dst;
77	}
78    }
79  else
80    {
81      /* Simply copy the data after we made sure we are actually getting
82	 correct data.  */
83      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
84	{
85	  __libelf_seterrno (ELF_E_INVALID_INDEX);
86	  result = NULL;
87	}
88      else
89	result = memcpy (dst, &((Elf64_Rel *) data_scn->d.d_buf)[ndx],
90			 sizeof (Elf64_Rel));
91    }
92
93  rwlock_unlock (scn->elf->lock);
94
95  return result;
96}
97