gelf_rela.c revision 1.2.8.2 1 1.2.8.2 tls /* $NetBSD: gelf_rela.c,v 1.2.8.2 2014/08/19 23:46:45 tls Exp $ */
2 1.2.8.2 tls
3 1.2.8.2 tls /*-
4 1.2.8.2 tls * Copyright (c) 2006,2008 Joseph Koshy
5 1.2.8.2 tls * All rights reserved.
6 1.2.8.2 tls *
7 1.2.8.2 tls * Redistribution and use in source and binary forms, with or without
8 1.2.8.2 tls * modification, are permitted provided that the following conditions
9 1.2.8.2 tls * are met:
10 1.2.8.2 tls * 1. Redistributions of source code must retain the above copyright
11 1.2.8.2 tls * notice, this list of conditions and the following disclaimer.
12 1.2.8.2 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.8.2 tls * notice, this list of conditions and the following disclaimer in the
14 1.2.8.2 tls * documentation and/or other materials provided with the distribution.
15 1.2.8.2 tls *
16 1.2.8.2 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.8.2 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.8.2 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.8.2 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.8.2 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.8.2 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.8.2 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.8.2 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.8.2 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.8.2 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.8.2 tls * SUCH DAMAGE.
27 1.2.8.2 tls */
28 1.2.8.2 tls
29 1.2.8.2 tls #if HAVE_NBTOOL_CONFIG_H
30 1.2.8.2 tls # include "nbtool_config.h"
31 1.2.8.2 tls #endif
32 1.2.8.2 tls
33 1.2.8.2 tls #include <sys/cdefs.h>
34 1.2.8.2 tls
35 1.2.8.2 tls #include <assert.h>
36 1.2.8.2 tls #include <gelf.h>
37 1.2.8.2 tls #include <limits.h>
38 1.2.8.2 tls
39 1.2.8.2 tls #include "_libelf.h"
40 1.2.8.2 tls
41 1.2.8.2 tls __RCSID("$NetBSD: gelf_rela.c,v 1.2.8.2 2014/08/19 23:46:45 tls Exp $");
42 1.2.8.2 tls ELFTC_VCSID("Id: gelf_rela.c 2272 2011-12-03 17:07:31Z jkoshy ");
43 1.2.8.2 tls
44 1.2.8.2 tls GElf_Rela *
45 1.2.8.2 tls gelf_getrela(Elf_Data *ed, int ndx, GElf_Rela *dst)
46 1.2.8.2 tls {
47 1.2.8.2 tls int ec;
48 1.2.8.2 tls Elf *e;
49 1.2.8.2 tls size_t msz;
50 1.2.8.2 tls Elf_Scn *scn;
51 1.2.8.2 tls uint32_t sh_type;
52 1.2.8.2 tls Elf32_Rela *rela32;
53 1.2.8.2 tls Elf64_Rela *rela64;
54 1.2.8.2 tls struct _Libelf_Data *d;
55 1.2.8.2 tls
56 1.2.8.2 tls d = (struct _Libelf_Data *) ed;
57 1.2.8.2 tls
58 1.2.8.2 tls if (d == NULL || ndx < 0 || dst == NULL ||
59 1.2.8.2 tls (scn = d->d_scn) == NULL ||
60 1.2.8.2 tls (e = scn->s_elf) == NULL) {
61 1.2.8.2 tls LIBELF_SET_ERROR(ARGUMENT, 0);
62 1.2.8.2 tls return (NULL);
63 1.2.8.2 tls }
64 1.2.8.2 tls
65 1.2.8.2 tls ec = e->e_class;
66 1.2.8.2 tls assert(ec == ELFCLASS32 || ec == ELFCLASS64);
67 1.2.8.2 tls
68 1.2.8.2 tls if (ec == ELFCLASS32)
69 1.2.8.2 tls sh_type = scn->s_shdr.s_shdr32.sh_type;
70 1.2.8.2 tls else
71 1.2.8.2 tls sh_type = scn->s_shdr.s_shdr64.sh_type;
72 1.2.8.2 tls
73 1.2.8.2 tls if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) {
74 1.2.8.2 tls LIBELF_SET_ERROR(ARGUMENT, 0);
75 1.2.8.2 tls return (NULL);
76 1.2.8.2 tls }
77 1.2.8.2 tls
78 1.2.8.2 tls msz = _libelf_msize(ELF_T_RELA, ec, e->e_version);
79 1.2.8.2 tls
80 1.2.8.2 tls assert(msz > 0);
81 1.2.8.2 tls
82 1.2.8.2 tls if (msz * ndx >= d->d_data.d_size) {
83 1.2.8.2 tls LIBELF_SET_ERROR(ARGUMENT, 0);
84 1.2.8.2 tls return (NULL);
85 1.2.8.2 tls }
86 1.2.8.2 tls
87 1.2.8.2 tls if (ec == ELFCLASS32) {
88 1.2.8.2 tls rela32 = (Elf32_Rela *) d->d_data.d_buf + ndx;
89 1.2.8.2 tls
90 1.2.8.2 tls dst->r_offset = (Elf64_Addr) rela32->r_offset;
91 1.2.8.2 tls dst->r_info = ELF64_R_INFO(
92 1.2.8.2 tls (Elf64_Xword) ELF32_R_SYM(rela32->r_info),
93 1.2.8.2 tls ELF32_R_TYPE(rela32->r_info));
94 1.2.8.2 tls dst->r_addend = (Elf64_Sxword) rela32->r_addend;
95 1.2.8.2 tls
96 1.2.8.2 tls } else {
97 1.2.8.2 tls
98 1.2.8.2 tls rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx;
99 1.2.8.2 tls
100 1.2.8.2 tls *dst = *rela64;
101 1.2.8.2 tls }
102 1.2.8.2 tls
103 1.2.8.2 tls return (dst);
104 1.2.8.2 tls }
105 1.2.8.2 tls
106 1.2.8.2 tls int
107 1.2.8.2 tls gelf_update_rela(Elf_Data *ed, int ndx, GElf_Rela *dr)
108 1.2.8.2 tls {
109 1.2.8.2 tls int ec;
110 1.2.8.2 tls Elf *e;
111 1.2.8.2 tls size_t msz;
112 1.2.8.2 tls Elf_Scn *scn;
113 1.2.8.2 tls uint32_t sh_type;
114 1.2.8.2 tls Elf32_Rela *rela32;
115 1.2.8.2 tls Elf64_Rela *rela64;
116 1.2.8.2 tls struct _Libelf_Data *d;
117 1.2.8.2 tls
118 1.2.8.2 tls d = (struct _Libelf_Data *) ed;
119 1.2.8.2 tls
120 1.2.8.2 tls if (d == NULL || ndx < 0 || dr == NULL ||
121 1.2.8.2 tls (scn = d->d_scn) == NULL ||
122 1.2.8.2 tls (e = scn->s_elf) == NULL) {
123 1.2.8.2 tls LIBELF_SET_ERROR(ARGUMENT, 0);
124 1.2.8.2 tls return (0);
125 1.2.8.2 tls }
126 1.2.8.2 tls
127 1.2.8.2 tls ec = e->e_class;
128 1.2.8.2 tls assert(ec == ELFCLASS32 || ec == ELFCLASS64);
129 1.2.8.2 tls
130 1.2.8.2 tls if (ec == ELFCLASS32)
131 1.2.8.2 tls sh_type = scn->s_shdr.s_shdr32.sh_type;
132 1.2.8.2 tls else
133 1.2.8.2 tls sh_type = scn->s_shdr.s_shdr64.sh_type;
134 1.2.8.2 tls
135 1.2.8.2 tls if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) {
136 1.2.8.2 tls LIBELF_SET_ERROR(ARGUMENT, 0);
137 1.2.8.2 tls return (0);
138 1.2.8.2 tls }
139 1.2.8.2 tls
140 1.2.8.2 tls msz = _libelf_msize(ELF_T_RELA, ec, e->e_version);
141 1.2.8.2 tls assert(msz > 0);
142 1.2.8.2 tls
143 1.2.8.2 tls if (msz * ndx >= d->d_data.d_size) {
144 1.2.8.2 tls LIBELF_SET_ERROR(ARGUMENT, 0);
145 1.2.8.2 tls return (0);
146 1.2.8.2 tls }
147 1.2.8.2 tls
148 1.2.8.2 tls if (ec == ELFCLASS32) {
149 1.2.8.2 tls rela32 = (Elf32_Rela *) d->d_data.d_buf + ndx;
150 1.2.8.2 tls
151 1.2.8.2 tls LIBELF_COPY_U32(rela32, dr, r_offset);
152 1.2.8.2 tls
153 1.2.8.2 tls if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) ||
154 1.2.8.2 tls ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) {
155 1.2.8.2 tls LIBELF_SET_ERROR(RANGE, 0);
156 1.2.8.2 tls return (0);
157 1.2.8.2 tls }
158 1.2.8.2 tls rela32->r_info = ELF32_R_INFO(ELF64_R_SYM(dr->r_info),
159 1.2.8.2 tls ELF64_R_TYPE(dr->r_info));
160 1.2.8.2 tls
161 1.2.8.2 tls LIBELF_COPY_S32(rela32, dr, r_addend);
162 1.2.8.2 tls } else {
163 1.2.8.2 tls rela64 = (Elf64_Rela *) d->d_data.d_buf + ndx;
164 1.2.8.2 tls
165 1.2.8.2 tls *rela64 = *dr;
166 1.2.8.2 tls }
167 1.2.8.2 tls
168 1.2.8.2 tls return (1);
169 1.2.8.2 tls }
170