Rizin
unix-like reverse engineering framework and cli tools
elf_strtab.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_strtab.h"
6 #include "elf.h"
7 
9  rz_return_val_if_fail(strtab, false);
10 
11  if (!Elf_(rz_bin_elf_strtab_has_index)(strtab, index)) {
12  return NULL;
13  }
14 
15  return strtab->data + index;
16 }
17 
20 
21  if (!size || !Elf_(rz_bin_elf_check_array)(bin, offset, size, sizeof(ut8))) {
22  RZ_LOG_WARN("Invalid strtab at 0x%" PFMT64x " (check array failed).\n", offset);
23  return NULL;
24  }
25 
27  if (!result) {
28  return NULL;
29  }
30 
31  result->size = size;
32  result->data = RZ_NEWS(char, size);
33  if (!result->data) {
35  return NULL;
36  }
37 
38  if (rz_buf_read_at(bin->b, offset, (ut8 *)result->data, size) < 0) {
40  return NULL;
41  }
42 
43  if (result->data[0] != '\0' || result->data[size - 1] != '\0') {
44  RZ_LOG_WARN("String table at 0x%" PFMT64x " should start and end by a NULL byte", offset);
46  return NULL;
47  }
48 
49  return result;
50 }
51 
53  rz_return_val_if_fail(strtab, NULL);
54 
55  if (!Elf_(rz_bin_elf_strtab_has_index)(strtab, index)) {
56  return NULL;
57  }
58 
59  char *result = strdup(strtab->data + index);
60  if (!result) {
61  return NULL;
62  }
63 
64  return result;
65 }
66 
68  rz_return_val_if_fail(strtab, false);
69  return index < strtab->size;
70 }
71 
73  if (!ptr) {
74  return;
75  }
76 
77  free(ptr->data);
78  free(ptr);
79 }
#define NULL
Definition: cris-opc.c:27
#define ELFOBJ
Definition: elf.h:24
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
bool Elf_() rz_bin_elf_strtab_has_index(RZ_NONNULL RzBinElfStrtab *strtab, ut64 index)
Definition: elf_strtab.c:67
RZ_OWN RzBinElfStrtab *Elf_() rz_bin_elf_strtab_new(RZ_NONNULL ELFOBJ *bin, ut64 offset, ut64 size)
Definition: elf_strtab.c:18
void Elf_() rz_bin_elf_strtab_free(RzBinElfStrtab *ptr)
Definition: elf_strtab.c:72
RZ_OWN char *Elf_() rz_bin_elf_strtab_get_dup(RZ_NONNULL RzBinElfStrtab *strtab, ut64 index)
Definition: elf_strtab.c:52
RZ_BORROW const char *Elf_() rz_bin_elf_strtab_get(RZ_NONNULL RzBinElfStrtab *strtab, ut64 index)
Definition: elf_strtab.c:8
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
uint8_t ut8
Definition: lh5801.h:11
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
#define RZ_NEWS(x, y)
Definition: rz_types.h:283
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_NEW(x)
Definition: rz_types.h:285
#define PFMT64x
Definition: rz_types.h:393
#define RZ_BORROW
Definition: rz_types.h:63
Definition: malloc.c:26
ut64(WINAPI *w32_GetEnabledXStateFeatures)()