Rizin
unix-like reverse engineering framework and cli tools
elf_misc.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 08A <08A@riseup.net>
2 // SPDX-FileCopyrightText: 2021 RizinOrg <info@rizin.re>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include "elf.h"
6 
7 static bool buffer_read_32_signed(ELFOBJ *bin, ut64 *offset, st32 *result) {
8  ut32 tmp;
9  if (!rz_buf_read_ble32_at(bin->b, *offset, &tmp, bin->big_endian)) {
10  return false;
11  }
12 
13  *result = convert_to_two_complement_32(tmp);
14  *offset += 4;
15 
16  return true;
17 }
18 
19 static bool buffer_read_64_signed(ELFOBJ *bin, ut64 *offset, st64 *result) {
20  ut64 tmp;
21 
22  if (!rz_buf_read_ble64_at(bin->b, *offset, &tmp, bin->big_endian)) {
23  return false;
24  }
25 
26  *result = convert_to_two_complement_64(tmp);
27  *offset += 8;
28 
29  return true;
30 }
31 
32 bool Elf_(rz_bin_elf_check_array)(RZ_NONNULL ELFOBJ *bin, Elf_(Off) offset, Elf_(Off) length, Elf_(Off) entry_size) {
33  rz_return_val_if_fail(bin, false);
34 
35  Elf_(Off) array_size;
36  if (!Elf_(rz_bin_elf_mul_off)(&array_size, length, entry_size)) {
37  return false;
38  }
39 
40  Elf_(Off) end_off;
41  if (!Elf_(rz_bin_elf_add_off)(&end_off, offset, array_size)) {
42  return false;
43  }
44 
45  if (!array_size || end_off > bin->size) {
46  return false;
47  }
48 
49  return true;
50 }
51 
53  rz_return_val_if_fail(bin && offset && result, false);
54  return rz_buf_read8_offset(bin->b, offset, result);
55 }
56 
58  rz_return_val_if_fail(bin && offset && result, false);
59  return rz_buf_read_ble16_offset(bin->b, offset, result, bin->big_endian);
60 }
61 
63  rz_return_val_if_fail(bin && offset && result, false);
64  return rz_buf_read_ble32_offset(bin->b, offset, result, bin->big_endian);
65 }
66 
68  rz_return_val_if_fail(bin && offset && result, false);
69  return buffer_read_32_signed(bin, offset, result);
70 }
71 
73  rz_return_val_if_fail(bin && offset && result, false);
74  return rz_buf_read_ble64_offset(bin->b, offset, (ut64 *)result, bin->big_endian);
75 }
76 
78  rz_return_val_if_fail(bin && offset && result, false);
79  return buffer_read_64_signed(bin, offset, (st64 *)result);
80 }
81 
83  rz_return_val_if_fail(bin && offset && result, false);
84 #if RZ_BIN_ELF64
85  return rz_buf_read_ble64_offset(bin->b, offset, (ut64 *)result, bin->big_endian);
86 #else
87  return rz_buf_read_ble32_offset(bin->b, offset, (ut32 *)result, bin->big_endian);
88 #endif
89 }
90 
92  rz_return_val_if_fail(bin && offset && result, false);
93 #if RZ_BIN_ELF64
94  return rz_buf_read_ble64_offset(bin->b, offset, (ut64 *)result, bin->big_endian);
95 #else
96  return rz_buf_read_ble32_offset(bin->b, offset, (ut32 *)result, bin->big_endian);
97 #endif
98 }
99 
101  rz_return_val_if_fail(bin && offset && result, false);
102  return rz_buf_read_ble16_offset(bin->b, offset, (ut16 *)result, bin->big_endian);
103 }
104 
106  rz_return_val_if_fail(bin && offset && result, false);
107  return Elf_(rz_bin_elf_read_half)(bin, offset, result);
108 }
109 
110 #if RZ_BIN_ELF64
112  rz_return_val_if_fail(bin && offset && result, false);
113 
114  return Elf_(rz_bin_elf_read_xword)(bin, offset, result);
115 }
116 #else
118  rz_return_val_if_fail(bin && offset && result, false);
119 
120  return Elf_(rz_bin_elf_read_word)(bin, offset, result);
121 }
122 #endif
123 
124 #if RZ_BIN_ELF64
126  rz_return_val_if_fail(bin && offset && result, false);
127 
128  return Elf_(rz_bin_elf_read_sxword)(bin, offset, result);
129 }
130 #else
132  rz_return_val_if_fail(bin && offset && result, false);
133 
134  return Elf_(rz_bin_elf_read_sword)(bin, offset, result);
135 }
136 #endif
137 
138 bool Elf_(rz_bin_elf_add_addr)(Elf_(Addr) * result, Elf_(Addr) addr, Elf_(Addr) value) {
139 #if RZ_BIN_ELF64
140  return UT64_ADD((ut64 *)result, addr, value);
141 #else
142  return UT32_ADD((ut32 *)result, addr, value);
143 #endif
144 }
145 
146 bool Elf_(rz_bin_elf_add_off)(Elf_(Off) * result, Elf_(Off) addr, Elf_(Off) value) {
147 #if RZ_BIN_ELF64
148  return UT64_ADD((ut64 *)result, addr, value);
149 #else
150  return UT32_ADD((ut32 *)result, addr, value);
151 #endif
152 }
153 
154 bool Elf_(rz_bin_elf_mul_addr)(Elf_(Addr) * result, Elf_(Addr) addr, Elf_(Addr) value) {
155 #if RZ_BIN_ELF64
156  return UT64_MUL((ut64 *)result, addr, value);
157 #else
158  return UT32_MUL((ut32 *)result, addr, value);
159 #endif
160 }
161 
162 bool Elf_(rz_bin_elf_mul_off)(Elf_(Off) * result, Elf_(Off) addr, Elf_(Off) value) {
163 #if RZ_BIN_ELF64
164  return UT64_MUL((ut64 *)result, addr, value);
165 #else
166  return UT32_MUL((ut32 *)result, addr, value);
167 #endif
168 }
static int value
Definition: cmd_api.c:93
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
uint16_t ut16
uint32_t ut32
#define ELFOBJ
Definition: elf.h:24
static bool buffer_read_32_signed(ELFOBJ *bin, ut64 *offset, st32 *result)
Definition: elf_misc.c:7
static bool buffer_read_64_signed(ELFOBJ *bin, ut64 *offset, st64 *result)
Definition: elf_misc.c:19
bool Elf_() rz_bin_elf_read_word_xword(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Word) *result)
Definition: elf_misc.c:117
bool Elf_() rz_bin_elf_add_off(Elf_(Off) *result, Elf_(Off) addr, Elf_(Off) value)
Definition: elf_misc.c:146
bool Elf_() rz_bin_elf_mul_off(Elf_(Off) *result, Elf_(Off) addr, Elf_(Off) value)
Definition: elf_misc.c:162
bool Elf_() rz_bin_elf_read_addr(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Addr) *result)
Definition: elf_misc.c:82
bool Elf_() rz_bin_elf_read_char(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT ut8 *result)
Definition: elf_misc.c:52
bool Elf_() rz_bin_elf_read_off(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Off) *result)
Definition: elf_misc.c:91
bool Elf_() rz_bin_elf_read_sxword(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Sxword) *result)
Definition: elf_misc.c:77
bool Elf_() rz_bin_elf_read_word(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Word) *result)
Definition: elf_misc.c:62
bool Elf_() rz_bin_elf_read_xword(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Xword) *result)
Definition: elf_misc.c:72
bool Elf_() rz_bin_elf_read_half(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Half) *result)
Definition: elf_misc.c:57
bool Elf_() rz_bin_elf_read_section(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Section) *result)
Definition: elf_misc.c:100
bool Elf_() rz_bin_elf_read_sword_sxword(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Sword) *result)
Definition: elf_misc.c:131
bool Elf_() rz_bin_elf_read_versym(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Versym) *result)
Definition: elf_misc.c:105
bool Elf_() rz_bin_elf_add_addr(Elf_(Addr) *result, Elf_(Addr) addr, Elf_(Addr) value)
Definition: elf_misc.c:138
bool Elf_() rz_bin_elf_mul_addr(Elf_(Addr) *result, Elf_(Addr) addr, Elf_(Addr) value)
Definition: elf_misc.c:154
bool Elf_() rz_bin_elf_read_sword(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Sword) *result)
Definition: elf_misc.c:67
bool Elf_() rz_bin_elf_check_array(RZ_NONNULL ELFOBJ *bin, Elf_(Off) offset, Elf_(Off) length, Elf_(Off) entry_size)
Definition: elf_misc.c:32
#define Elf_(name)
Definition: elf_specs.h:32
voidpf uLong offset
Definition: ioapi.h:144
uint8_t ut8
Definition: lh5801.h:11
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define rz_buf_read8_offset(b, offset, result)
Definition: rz_buf.h:274
static int UT64_ADD(ut64 *r, ut64 a, ut64 b)
Definition: rz_endian.h:603
static int UT32_ADD(ut32 *r, ut32 a, ut32 b)
Definition: rz_endian.h:633
static int UT32_MUL(ut32 *r, ut32 a, ut32 b)
Definition: rz_endian.h:643
static int UT64_MUL(ut64 *r, ut64 a, ut64 b)
Definition: rz_endian.h:613
#define RZ_OUT
Definition: rz_types.h:51
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_INOUT
Definition: rz_types.h:52
#define st64
Definition: rz_types_base.h:10
#define st32
Definition: rz_types_base.h:12
Definition: malloc.c:26
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58