1/*-
2 * Copyright (c) 2006 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <limits.h>
28
29#include <assert.h>
30#include "gelf.h"
31
32#include "_libelf.h"
33
34GElf_Rel *
35gelf_getrel(Elf_Data *d, int ndx, GElf_Rel *dst)
36{
37        int ec;
38        Elf *e;
39        Elf_Scn *scn;
40        Elf32_Rel *rel32;
41        Elf64_Rel *rel64;
42        size_t msz;
43        uint32_t sh_type;
44
45        if (d == NULL || ndx < 0 || dst == NULL ||
46            (scn = d->d_scn) == NULL ||
47            (e = scn->s_elf) == NULL) {
48                LIBELF_SET_ERROR(ARGUMENT, 0);
49                return (NULL);
50        }
51
52        ec = e->e_class;
53        assert(ec == ELFCLASS32 || ec == ELFCLASS64);
54
55        if (ec == ELFCLASS32)
56                sh_type = scn->s_shdr.s_shdr32.sh_type;
57        else
58                sh_type = scn->s_shdr.s_shdr64.sh_type;
59
60        if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
61                LIBELF_SET_ERROR(ARGUMENT, 0);
62                return (NULL);
63        }
64
65        msz = _libelf_msize(ELF_T_REL, ec, e->e_version);
66
67        assert(msz > 0);
68
69        if (msz * ndx >= d->d_size) {
70                LIBELF_SET_ERROR(ARGUMENT, 0);
71                return (NULL);
72        }
73
74        if (ec == ELFCLASS32) {
75                rel32 = (Elf32_Rel *) d->d_buf + ndx;
76
77                dst->r_offset = (Elf64_Addr) rel32->r_offset;
78                dst->r_info   = (Elf64_Xword) rel32->r_info;
79
80        } else {
81
82                rel64 = (Elf64_Rel *) d->d_buf + ndx;
83
84                *dst = *rel64;
85        }
86
87        return (dst);
88}
89
90int
91gelf_update_rel(Elf_Data *d, int ndx, GElf_Rel *dr)
92{
93        int ec;
94        Elf *e;
95        Elf_Scn *scn;
96        Elf32_Rel *rel32;
97        Elf64_Rel *rel64;
98        size_t msz;
99        uint32_t sh_type;
100
101        if (d == NULL || ndx < 0 || dr == NULL ||
102            (scn = d->d_scn) == NULL ||
103            (e = scn->s_elf) == NULL) {
104                LIBELF_SET_ERROR(ARGUMENT, 0);
105                return (0);
106        }
107
108        ec = e->e_class;
109        assert(ec == ELFCLASS32 || ec == ELFCLASS64);
110
111        if (ec == ELFCLASS32)
112                sh_type = scn->s_shdr.s_shdr32.sh_type;
113        else
114                sh_type = scn->s_shdr.s_shdr64.sh_type;
115
116        if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
117                LIBELF_SET_ERROR(ARGUMENT, 0);
118                return (0);
119        }
120
121        msz = _libelf_msize(ELF_T_REL, ec, e->e_version);
122        assert(msz > 0);
123
124        if (msz * ndx >= d->d_size) {
125                LIBELF_SET_ERROR(ARGUMENT, 0);
126                return (0);
127        }
128
129        if (ec == ELFCLASS32) {
130                rel32 = (Elf32_Rel *) d->d_buf + ndx;
131
132                LIBELF_COPY_U32(rel32, dr, r_offset);
133                LIBELF_COPY_U32(rel32, dr, r_info);
134        } else {
135                rel64 = (Elf64_Rel *) d->d_buf + ndx;
136
137                *rel64 = *dr;
138        }
139
140        return (1);
141}
142