Rizin
unix-like reverse engineering framework and cli tools
elf.c File Reference
#include "elf.h"

Go to the source code of this file.

Functions

static bool in_virtual_phdr (RzBinElfSegment *segment, ut64 addr)
 
static bool in_physical_phdr (RzBinElfSegment *segment, ut64 addr)
 
static bool init_phdr_sdb (ELFOBJ *bin)
 
static bool init_phdr_aux (ELFOBJ *bin, RzVector *sections, RzBinObjectLoadOptions *options)
 
static void init_phdr (ELFOBJ *bin, RzVector *sections, RzBinObjectLoadOptions *options)
 
static bool init_ehdr_sdb (ELFOBJ *bin)
 
static bool init_ehdr (ELFOBJ *bin)
 
static bool init_shdr_sdb (ELFOBJ *bin)
 
static bool init_shdr_aux (ELFOBJ *bin, RzBinObjectLoadOptions *options, RzVector *sections)
 
static void init_shdr (ELFOBJ *bin, RzBinObjectLoadOptions *options, RzVector *sections)
 
static bool init_shstrtab_sdb (ELFOBJ *bin, ut64 offset, ut64 size)
 
static bool init_shstrtab_aux (ELFOBJ *bin, RzVector *sections)
 
static void init_shstrtab (ELFOBJ *bin, RzVector *sections)
 
static bool init_dt_dynamic_sdb (ELFOBJ *bin)
 
static bool init_dt_dynamic_aux (ELFOBJ *bin)
 
static void init_dt_dynamic (ELFOBJ *bin)
 
static bool init_dynstr_sdb (ELFOBJ *bin, ut64 strtab_addr, Elf_(Xword) strtab_size)
 
static bool init_dynstr_aux (ELFOBJ *bin)
 
static void init_dynstr (ELFOBJ *bin)
 
static bool init_symbols_info_aux (ELFOBJ *bin)
 
static void init_symbols_info (ELFOBJ *bin)
 
static bool init (ELFOBJ *bin, RzBinObjectLoadOptions *options)
 
RZ_OWN ELFOBJ *Elf_() rz_bin_elf_new_buf (RZ_NONNULL RzBuffer *buf, RZ_NONNULL RzBinObjectLoadOptions *options)
 
void Elf_() rz_bin_elf_free (RZ_NONNULL ELFOBJ *bin)
 Free the elf binary. More...
 
ut64 Elf_() rz_bin_elf_p2v (RZ_NONNULL ELFOBJ *bin, ut64 paddr)
 Convert a physical address to the virtual address. More...
 
ut64 Elf_() rz_bin_elf_v2p (RZ_NONNULL ELFOBJ *bin, ut64 vaddr)
 Convert a virtual address to the physical address. More...
 

Variables

static const char * sdb_elf_p_type_cparse
 
static const char * sdb_elf_p_flags_cparse
 
static const char * sdb_elf_type_cparse
 
static const char * sdb_elf_machine_cparse
 
static const char * sdb_elf_class_cparse = "enum elf_class {ELFCLASSNONE=0, ELFCLASS32=1, ELFCLASS64=2};"
 
static const char * sdb_elf_data_cparse = "enum elf_data {ELFDATANONE=0, ELFDATA2LSB=1, ELFDATA2MSB=2};"
 
static const char * sdb_elf_hdr_version_cparse = "enum elf_hdr_version {EV_NONE=0, EV_CURRENT=1};"
 
static const char * sdb_elf_obj_version_cparse = "enum elf_obj_version {EV_NONE=0, EV_CURRENT=1};"
 
static const char * sdb_elf_ident_format
 
static const char * sdb_elf_s_type_cparse
 
static const char * sdb_elf_phdr_format
 
static const char * sdb_elf_s_flags_32_cparse
 
static const char * sdb_elf_shdr_format
 
static const char * sdb_elf_header_format
 

Function Documentation

◆ in_physical_phdr()

static bool in_physical_phdr ( RzBinElfSegment segment,
ut64  addr 
)
static

Definition at line 109 of file elf.c.

109  {
110  return addr >= segment->data.p_offset && addr < segment->data.p_offset + segment->data.p_filesz;
111 }
static int addr
Definition: z80asm.c:58

References addr.

Referenced by rz_bin_elf_p2v().

◆ in_virtual_phdr()

static bool in_virtual_phdr ( RzBinElfSegment segment,
ut64  addr 
)
static

Definition at line 105 of file elf.c.

105  {
106  return addr >= segment->data.p_vaddr && addr < segment->data.p_vaddr + segment->data.p_filesz;
107 }

References addr.

Referenced by rz_bin_elf_v2p().

◆ init()

static bool init ( ELFOBJ bin,
RzBinObjectLoadOptions options 
)
static

Definition at line 296 of file elf.c.

296  {
297  /* bin is not an ELF */
298  if (!init_ehdr(bin)) {
299  return false;
300  }
301 
303  if (options->elf_load_sections) {
305  }
306 
309  }
310 
313  init_dynstr(bin);
315  }
316 
317  if (bin->ehdr.e_type != ET_CORE) {
318  bin->baddr = Elf_(rz_bin_elf_get_baddr)(bin);
321  }
322 
323  bin->boffset = Elf_(rz_bin_elf_get_boffset)(bin);
324 
325  bin->relocs = Elf_(rz_bin_elf_relocs_new)(bin);
326  bin->reloc_targets_map_base = Elf_(rz_bin_elf_get_targets_map_base)(bin);
327 
328  bin->notes = Elf_(rz_bin_elf_notes_new)(bin);
329 
330  bin->symbols = Elf_(rz_bin_elf_symbols_new)(bin);
331  bin->bits = Elf_(rz_bin_elf_get_bits)(bin);
332  bin->imports = Elf_(rz_bin_elf_analyse_imports)(bin);
333 
335 
336  return true;
337 }
RzList * sections(RzBinFile *bf)
Definition: bin_ne.c:110
#define NULL
Definition: cris-opc.c:27
static void init_phdr(ELFOBJ *bin, RzVector *sections, RzBinObjectLoadOptions *options)
Definition: elf.c:130
static void init_shdr(ELFOBJ *bin, RzBinObjectLoadOptions *options, RzVector *sections)
Definition: elf.c:183
static void init_dynstr(ELFOBJ *bin)
Definition: elf.c:275
static bool init_ehdr(ELFOBJ *bin)
Definition: elf.c:154
static void init_shstrtab(ELFOBJ *bin, RzVector *sections)
Definition: elf.c:213
static void init_dt_dynamic(ELFOBJ *bin)
Definition: elf.c:241
static void init_symbols_info(ELFOBJ *bin)
Definition: elf.c:290
RZ_OWN RzVector *Elf_() rz_bin_elf_relocs_new(RZ_NONNULL ELFOBJ *bin)
Definition: elf_relocs.c:202
bool Elf_() rz_bin_elf_is_static(RZ_NONNULL ELFOBJ *bin)
Check if the binary is statically-linked library.
Definition: elf_info.c:1674
ut64 Elf_() rz_bin_elf_get_baddr(RZ_NONNULL ELFOBJ *bin)
Compute the base address of the binary.
Definition: elf_info.c:1772
RZ_OWN RzVector *Elf_() rz_bin_elf_symbols_new(RZ_NONNULL ELFOBJ *bin)
Definition: elf_symbols.c:523
ut64 Elf_() rz_bin_elf_get_targets_map_base(ELFOBJ *bin)
Definition: elf_map.c:44
RZ_OWN RzVector *Elf_() rz_bin_elf_sections_new(RZ_NONNULL ELFOBJ *bin)
Definition: elf_sections.c:378
RZ_OWN RzVector *Elf_() rz_bin_elf_analyse_imports(RZ_NONNULL ELFOBJ *bin)
Definition: elf_imports.c:329
int Elf_() rz_bin_elf_get_bits(RZ_NONNULL ELFOBJ *bin)
Return the elf bits.
Definition: elf_info.c:1697
RZ_OWN RzVector *Elf_() rz_bin_elf_notes_new(RZ_NONNULL ELFOBJ *bin)
Definition: elf_notes.c:218
bool Elf_() rz_bin_elf_is_relocatable(RZ_NONNULL ELFOBJ *bin)
Check if the elf binary is relocatable.
Definition: elf_info.c:1662
ut64 Elf_() rz_bin_elf_get_boffset(RZ_NONNULL ELFOBJ *bin)
Compute the base offset of the binary.
Definition: elf_info.c:1795
#define Elf_(name)
Definition: elf_specs.h:32
#define ET_CORE
Definition: common.h:92
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
RZ_API void rz_vector_free(RzVector *vec)
Definition: vector.c:75
Definition: malloc.c:26

References Elf_, ET_CORE, init_dt_dynamic(), init_dynstr(), init_ehdr(), init_phdr(), init_shdr(), init_shstrtab(), init_symbols_info(), NULL, options, rz_bin_elf_analyse_imports(), rz_bin_elf_get_baddr(), rz_bin_elf_get_bits(), rz_bin_elf_get_boffset(), rz_bin_elf_get_targets_map_base(), rz_bin_elf_is_relocatable(), rz_bin_elf_is_static(), rz_bin_elf_notes_new(), rz_bin_elf_relocs_new(), rz_bin_elf_sections_new(), rz_bin_elf_symbols_new(), rz_vector_free(), and sections().

Referenced by rz_bin_elf_new_buf().

◆ init_dt_dynamic()

static void init_dt_dynamic ( ELFOBJ bin)
static

Definition at line 241 of file elf.c.

241  {
242  if (!init_dt_dynamic_aux(bin)) {
243  RZ_LOG_INFO("Failed to initialize ELF DT_DYNAMIC.\n");
244  }
245 }
static bool init_dt_dynamic_aux(ELFOBJ *bin)
Definition: elf.c:232
#define RZ_LOG_INFO(fmtstr,...)
Definition: rz_log.h:54

References init_dt_dynamic_aux(), and RZ_LOG_INFO.

Referenced by init().

◆ init_dt_dynamic_aux()

static bool init_dt_dynamic_aux ( ELFOBJ bin)
static

Definition at line 232 of file elf.c.

232  {
233  bin->dt_dynamic = Elf_(rz_bin_elf_dt_dynamic_new)(bin);
234  if (!bin->dt_dynamic) {
235  return false;
236  }
237 
238  return init_dt_dynamic_sdb(bin);
239 }
static bool init_dt_dynamic_sdb(ELFOBJ *bin)
Definition: elf.c:219
RZ_OWN RzBinElfDtDynamic *Elf_() rz_bin_elf_dt_dynamic_new(RZ_NONNULL ELFOBJ *bin)
Definition: elf_dynamic.c:98

References Elf_, init_dt_dynamic_sdb(), and rz_bin_elf_dt_dynamic_new().

Referenced by init_dt_dynamic().

◆ init_dt_dynamic_sdb()

static bool init_dt_dynamic_sdb ( ELFOBJ bin)
static

Definition at line 219 of file elf.c.

219  {
220  switch (Elf_(rz_bin_elf_has_relro)(bin)) {
222  return sdb_set(bin->kv, "elf.relro", "full", 0);
224  return sdb_set(bin->kv, "elf.relro", "partial", 0);
225  default:
226  return sdb_set(bin->kv, "elf.relro", "no", 0);
227  }
228 
229  return false;
230 }
#define RZ_BIN_ELF_FULL_RELRO
Definition: elf.h:22
#define RZ_BIN_ELF_PART_RELRO
Definition: elf.h:21
int Elf_() rz_bin_elf_has_relro(RZ_NONNULL ELFOBJ *bin)
Analyse if the elf binary has relro or partial relro.
Definition: elf_info.c:1733
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611

References Elf_, RZ_BIN_ELF_FULL_RELRO, rz_bin_elf_has_relro(), RZ_BIN_ELF_PART_RELRO, and sdb_set().

Referenced by init_dt_dynamic_aux().

◆ init_dynstr()

static void init_dynstr ( ELFOBJ bin)
static

Definition at line 275 of file elf.c.

275  {
276  if (!init_dynstr_aux(bin)) {
277  RZ_LOG_INFO("Failed to initialize string table for dynamic linking.\n");
278  }
279 }
static bool init_dynstr_aux(ELFOBJ *bin)
Definition: elf.c:252

References init_dynstr_aux(), and RZ_LOG_INFO.

Referenced by init().

◆ init_dynstr_aux()

static bool init_dynstr_aux ( ELFOBJ bin)
static

Definition at line 252 of file elf.c.

252  {
253  ut64 addr;
254  ut64 size;
255 
257  RZ_LOG_INFO("DT_STRTAB or DT_STRSZ key not found.\n");
258  return false;
259  }
260 
262  if (offset == UT64_MAX) {
263  RZ_LOG_INFO("Failed to convert DT_STRTAB to a physical offset.\n");
264  return false;
265  }
266 
268  if (!bin->dynstr) {
269  return false;
270  }
271 
272  return init_dynstr_sdb(bin, offset, size);
273 }
static bool init_dynstr_sdb(ELFOBJ *bin, ut64 strtab_addr, Elf_(Xword) strtab_size)
Definition: elf.c:247
ut64 Elf_() rz_bin_elf_v2p(RZ_NONNULL ELFOBJ *bin, ut64 vaddr)
Convert a virtual address to the physical address.
Definition: elf.c:429
RZ_OWN RzBinElfStrtab *Elf_() rz_bin_elf_strtab_new(RZ_NONNULL ELFOBJ *bin, ut64 offset, ut64 size)
Definition: elf_strtab.c:18
bool Elf_() rz_bin_elf_get_dt_info(RZ_NONNULL ELFOBJ *bin, ut64 key, RZ_OUT ut64 *info)
Definition: elf_dynamic.c:120
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
#define DT_STRSZ
Definition: common.h:547
#define DT_STRTAB
Definition: common.h:542
#define UT64_MAX
Definition: rz_types_base.h:86
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References addr, DT_STRSZ, DT_STRTAB, Elf_, init_dynstr_sdb(), rz_bin_elf_get_dt_info(), rz_bin_elf_strtab_new(), rz_bin_elf_v2p(), RZ_LOG_INFO, ut64(), and UT64_MAX.

Referenced by init_dynstr().

◆ init_dynstr_sdb()

static bool init_dynstr_sdb ( ELFOBJ bin,
ut64  strtab_addr,
Elf_(Xword)  strtab_size 
)
static

Definition at line 247 of file elf.c.

247  {
248  return sdb_num_set(bin->kv, "elf_dynstr.offset", strtab_addr, 0) &&
249  sdb_num_set(bin->kv, "elf_dynstr.size", strtab_size, 0);
250 }
RZ_API int sdb_num_set(Sdb *s, const char *key, ut64 v, ut32 cas)
Definition: num.c:25

References sdb_num_set().

Referenced by init_dynstr_aux().

◆ init_ehdr()

static bool init_ehdr ( ELFOBJ bin)
static

Definition at line 154 of file elf.c.

154  {
155  if (!Elf_(rz_bin_elf_get_ehdr)(bin)) {
156  return false;
157  }
158 
159  return init_ehdr_sdb(bin);
160 }
static bool init_ehdr_sdb(ELFOBJ *bin)
Definition: elf.c:141
bool Elf_() rz_bin_elf_get_ehdr(RZ_NONNULL ELFOBJ *bin)
Definition: elf_ehdr.c:182

References Elf_, init_ehdr_sdb(), and rz_bin_elf_get_ehdr().

Referenced by init().

◆ init_ehdr_sdb()

static bool init_ehdr_sdb ( ELFOBJ bin)
static

Definition at line 141 of file elf.c.

141  {
142  return sdb_num_set(bin->kv, "elf_header.offset", 0, 0) &&
143  sdb_num_set(bin->kv, "elf_header.size", sizeof(Elf_(Ehdr)), 0) &&
144  sdb_set(bin->kv, "elf_class.cparse", sdb_elf_class_cparse, 0) &&
145  sdb_set(bin->kv, "elf_data.cparse", sdb_elf_data_cparse, 0) &&
146  sdb_set(bin->kv, "elf_hdr_version.cparse", sdb_elf_hdr_version_cparse, 0) &&
147  sdb_set(bin->kv, "elf_header.format", sdb_elf_header_format, 0) &&
148  sdb_set(bin->kv, "elf_ident.format", sdb_elf_ident_format, 0) &&
149  sdb_set(bin->kv, "elf_machine.cparse", sdb_elf_machine_cparse, 0) &&
150  sdb_set(bin->kv, "elf_obj_version.cparse", sdb_elf_obj_version_cparse, 0) &&
151  sdb_set(bin->kv, "elf_type.cparse", sdb_elf_type_cparse, 0);
152 }
static const char * sdb_elf_ident_format
Definition: elf.c:67
static const char * sdb_elf_header_format
Definition: elf.c:100
static const char * sdb_elf_data_cparse
Definition: elf.c:61
static const char * sdb_elf_hdr_version_cparse
Definition: elf.c:63
static const char * sdb_elf_machine_cparse
Definition: elf.c:21
static const char * sdb_elf_type_cparse
Definition: elf.c:17
static const char * sdb_elf_obj_version_cparse
Definition: elf.c:65
static const char * sdb_elf_class_cparse
Definition: elf.c:59

References Elf_, sdb_elf_class_cparse, sdb_elf_data_cparse, sdb_elf_hdr_version_cparse, sdb_elf_header_format, sdb_elf_ident_format, sdb_elf_machine_cparse, sdb_elf_obj_version_cparse, sdb_elf_type_cparse, sdb_num_set(), and sdb_set().

Referenced by init_ehdr().

◆ init_phdr()

static void init_phdr ( ELFOBJ bin,
RzVector sections,
RzBinObjectLoadOptions options 
)
static

Definition at line 130 of file elf.c.

130  {
131  if (!bin->ehdr.e_phnum) {
132  RZ_LOG_WARN("There is no program header.\n");
133  return;
134  }
135 
136  if (!init_phdr_aux(bin, sections, options)) {
137  RZ_LOG_WARN("Failed to initialize program header.\n");
138  }
139 }
static bool init_phdr_aux(ELFOBJ *bin, RzVector *sections, RzBinObjectLoadOptions *options)
Definition: elf.c:121
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56

References init_phdr_aux(), options, RZ_LOG_WARN, and sections().

Referenced by init().

◆ init_phdr_aux()

static bool init_phdr_aux ( ELFOBJ bin,
RzVector sections,
RzBinObjectLoadOptions options 
)
static

Definition at line 121 of file elf.c.

121  {
123  if (!bin->segments) {
124  return false;
125  }
126 
127  return init_phdr_sdb(bin);
128 }
static bool init_phdr_sdb(ELFOBJ *bin)
Definition: elf.c:113
RZ_OWN RzVector *Elf_() rz_bin_elf_segments_new(RZ_NONNULL ELFOBJ *bin, RzVector *sections, RZ_NONNULL RzBinObjectLoadOptions *options)
Definition: elf_segments.c:133

References Elf_, init_phdr_sdb(), options, rz_bin_elf_segments_new(), and sections().

Referenced by init_phdr().

◆ init_phdr_sdb()

static bool init_phdr_sdb ( ELFOBJ bin)
static

Definition at line 113 of file elf.c.

113  {
114  return sdb_num_set(bin->kv, "elf_phdr.offset", bin->ehdr.e_phoff, 0) &&
115  sdb_num_set(bin->kv, "elf_phdr.size", sizeof(Elf_(Phdr)), 0) &&
116  sdb_set(bin->kv, "elf_p_flags.cparse", sdb_elf_p_flags_cparse, 0) &&
117  sdb_set(bin->kv, "elf_p_type.cparse", sdb_elf_p_type_cparse, 0) &&
118  sdb_set(bin->kv, "elf_phdr.format", sdb_elf_phdr_format, 0);
119 }
static const char * sdb_elf_phdr_format
Definition: elf.c:90
static const char * sdb_elf_p_type_cparse
Definition: elf.c:9
static const char * sdb_elf_p_flags_cparse
Definition: elf.c:13

References Elf_, sdb_elf_p_flags_cparse, sdb_elf_p_type_cparse, sdb_elf_phdr_format, sdb_num_set(), and sdb_set().

Referenced by init_phdr_aux().

◆ init_shdr()

static void init_shdr ( ELFOBJ bin,
RzBinObjectLoadOptions options,
RzVector sections 
)
static

Definition at line 183 of file elf.c.

183  {
184  if (!init_shdr_aux(bin, options, sections)) {
185  RZ_LOG_WARN("Failed to initialize section header.\n");
186  }
187 }
static bool init_shdr_aux(ELFOBJ *bin, RzBinObjectLoadOptions *options, RzVector *sections)
Definition: elf.c:174

References init_shdr_aux(), options, RZ_LOG_WARN, and sections().

Referenced by init().

◆ init_shdr_aux()

static bool init_shdr_aux ( ELFOBJ bin,
RzBinObjectLoadOptions options,
RzVector sections 
)
static

Definition at line 174 of file elf.c.

174  {
176  if (!bin->sections) {
177  return false;
178  }
179 
180  return init_shdr_sdb(bin);
181 }
static bool init_shdr_sdb(ELFOBJ *bin)
Definition: elf.c:162
RZ_OWN RzVector *Elf_() rz_bin_elf_convert_sections(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RzBinObjectLoadOptions *options, RzVector *sections)
Definition: elf_sections.c:362

References Elf_, init_shdr_sdb(), options, rz_bin_elf_convert_sections(), and sections().

Referenced by init_shdr().

◆ init_shdr_sdb()

static bool init_shdr_sdb ( ELFOBJ bin)
static

Definition at line 162 of file elf.c.

162  {
163  return sdb_num_set(bin->kv, "elf_shdr.offset", bin->ehdr.e_shoff, 0) &&
164  sdb_num_set(bin->kv, "elf_shdr.size", sizeof(Elf_(Shdr)), 0) &&
165 #if RZ_BIN_ELF64
166  sdb_set(bin->kv, "elf_s_flags_64.cparse", sdb_elf_s_flags_64_cparse, 0) &&
167 #else
168  sdb_set(bin->kv, "elf_s_flags_32.cparse", sdb_elf_s_flags_32_cparse, 0) &&
169 #endif
170  sdb_set(bin->kv, "elf_s_type.cparse", sdb_elf_s_type_cparse, 0) &&
171  sdb_set(bin->kv, "elf_shdr.format", sdb_elf_shdr_format, 0);
172 }
static const char * sdb_elf_s_flags_32_cparse
Definition: elf.c:93
static const char * sdb_elf_shdr_format
Definition: elf.c:97
static const char * sdb_elf_s_type_cparse
Definition: elf.c:70

References Elf_, sdb_elf_s_flags_32_cparse, sdb_elf_s_type_cparse, sdb_elf_shdr_format, sdb_num_set(), and sdb_set().

Referenced by init_shdr_aux().

◆ init_shstrtab()

static void init_shstrtab ( ELFOBJ bin,
RzVector sections 
)
static

Definition at line 213 of file elf.c.

213  {
214  if (!init_shstrtab_aux(bin, sections)) {
215  RZ_LOG_WARN("Failed to initialize section string table.\n");
216  }
217 }
static bool init_shstrtab_aux(ELFOBJ *bin, RzVector *sections)
Definition: elf.c:194

References init_shstrtab_aux(), RZ_LOG_WARN, and sections().

Referenced by init().

◆ init_shstrtab_aux()

static bool init_shstrtab_aux ( ELFOBJ bin,
RzVector sections 
)
static

Definition at line 194 of file elf.c.

194  {
195  if (!sections) {
196  return true;
197  }
198 
199  Elf_(Shdr) *section = rz_vector_index_ptr(sections, bin->ehdr.e_shstrndx);
200  if (!section) {
201  RZ_LOG_WARN("Invalid ELF header e_shstrndx value.\n");
202  return false;
203  }
204 
205  bin->shstrtab = Elf_(rz_bin_elf_strtab_new)(bin, section->sh_offset, section->sh_size);
206  if (!bin->shstrtab) {
207  return false;
208  }
209 
210  return init_shstrtab_sdb(bin, section->sh_offset, section->sh_size);
211 }
static bool init_shstrtab_sdb(ELFOBJ *bin, ut64 offset, ut64 size)
Definition: elf.c:189
static void * rz_vector_index_ptr(RzVector *vec, size_t index)
Definition: rz_vector.h:88

References Elf_, init_shstrtab_sdb(), rz_bin_elf_strtab_new(), RZ_LOG_WARN, rz_vector_index_ptr(), and sections().

Referenced by init_shstrtab().

◆ init_shstrtab_sdb()

static bool init_shstrtab_sdb ( ELFOBJ bin,
ut64  offset,
ut64  size 
)
static

Definition at line 189 of file elf.c.

189  {
190  return sdb_num_set(bin->kv, "elf_shstrtab.offset", offset, 0) &&
191  sdb_num_set(bin->kv, "elf_shstrtab.size", size, 0);
192 }

References sdb_num_set().

Referenced by init_shstrtab_aux().

◆ init_symbols_info()

static void init_symbols_info ( ELFOBJ bin)
static

Definition at line 290 of file elf.c.

290  {
291  if (!init_symbols_info_aux(bin)) {
292  RZ_LOG_INFO("Failed to initialize GNU symbols information.\n")
293  }
294 }
static bool init_symbols_info_aux(ELFOBJ *bin)
Definition: elf.c:281

References init_symbols_info_aux(), and RZ_LOG_INFO.

Referenced by init().

◆ init_symbols_info_aux()

static bool init_symbols_info_aux ( ELFOBJ bin)
static

Definition at line 281 of file elf.c.

281  {
283  if (!info) {
284  return false;
285  }
286 
287  return sdb_ns_set(bin->kv, "versioninfo", info);
288 }
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
RZ_OWN Sdb *Elf_() rz_bin_elf_get_symbols_info(RZ_NONNULL ELFOBJ *bin)
Definition: elf_info.c:1301
RZ_API int sdb_ns_set(Sdb *s, const char *name, Sdb *r)
Definition: ns.c:156
Definition: sdb.h:63

References Elf_, info(), rz_bin_elf_get_symbols_info(), and sdb_ns_set().

Referenced by init_symbols_info().

◆ rz_bin_elf_free()

void Elf_() rz_bin_elf_free ( RZ_NONNULL ELFOBJ bin)

Free the elf binary.

Parameters
elfbinary

...

Definition at line 366 of file elf.c.

366  {
368 
369  rz_buf_free(bin->b);
370  rz_buf_free(bin->buf_patched);
371 
372  sdb_free(bin->kv);
373 
374  rz_vector_free(bin->segments);
375  rz_vector_free(bin->sections);
376 
377  Elf_(rz_bin_elf_dt_dynamic_free)(bin->dt_dynamic);
378 
379  Elf_(rz_bin_elf_strtab_free)(bin->dynstr);
380  Elf_(rz_bin_elf_strtab_free)(bin->shstrtab);
381 
382  rz_vector_free(bin->relocs);
383 
384  rz_vector_free(bin->notes);
385 
386  rz_vector_free(bin->symbols);
387  rz_vector_free(bin->imports);
388 
389  free(bin);
390 }
void Elf_() rz_bin_elf_dt_dynamic_free(RzBinElfDtDynamic *ptr)
Definition: elf_dynamic.c:135
void Elf_() rz_bin_elf_strtab_free(RzBinElfStrtab *ptr)
Definition: elf_strtab.c:72
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206

References Elf_, free(), rz_bin_elf_dt_dynamic_free(), rz_bin_elf_strtab_free(), rz_buf_free(), rz_return_if_fail, rz_vector_free(), and sdb_free().

Referenced by get_gnu_debugdata_elf_symbols(), and rz_bin_elf_new_buf().

◆ rz_bin_elf_new_buf()

RZ_OWN ELFOBJ* Elf_() rz_bin_elf_new_buf ( RZ_NONNULL RzBuffer buf,
RZ_NONNULL RzBinObjectLoadOptions options 
)

Definition at line 339 of file elf.c.

339  {
341 
342  ELFOBJ *bin = RZ_NEW0(ELFOBJ);
343  if (!bin) {
344  return NULL;
345  }
346 
347  bin->b = rz_buf_ref(buf);
348  bin->kv = sdb_new0();
349 
350  bin->size = rz_buf_size(buf);
351 
352  if (!init(bin, options)) {
354  return NULL;
355  }
356 
357  return bin;
358 }
static bool init(ELFOBJ *bin, RzBinObjectLoadOptions *options)
Definition: elf.c:296
void Elf_() rz_bin_elf_free(RZ_NONNULL ELFOBJ *bin)
Free the elf binary.
Definition: elf.c:366
#define ELFOBJ
Definition: elf.h:24
voidpf void * buf
Definition: ioapi.h:138
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API Sdb * sdb_new0(void)
Definition: sdb.c:43

References Elf_, ELFOBJ, init(), NULL, options, rz_bin_elf_free(), rz_buf_ref(), rz_buf_size(), RZ_NEW0, rz_return_val_if_fail, and sdb_new0().

Referenced by get_gnu_debugdata_elf_symbols().

◆ rz_bin_elf_p2v()

ut64 Elf_() rz_bin_elf_p2v ( RZ_NONNULL ELFOBJ bin,
ut64  paddr 
)

Convert a physical address to the virtual address.

Parameters
elfbinary
Returns
virtual addr

Converts a physical address to the virtual address, looking at the program headers in the binary bin

Definition at line 400 of file elf.c.

400  {
402 
405  return bin->baddr + paddr;
406  }
407 
408  return UT64_MAX;
409  }
410 
411  RzBinElfSegment *segment;
412  rz_bin_elf_foreach_segments(bin, segment) {
413  if (segment->data.p_type == PT_LOAD && in_physical_phdr(segment, paddr)) {
414  return segment->data.p_vaddr + paddr - segment->data.p_offset;
415  }
416  }
417 
418  return UT64_MAX;
419 }
static bool in_physical_phdr(RzBinElfSegment *segment, ut64 addr)
Definition: elf.c:109
#define rz_bin_elf_foreach_segments(bin, segment)
Definition: elf.h:26
RzBinElfSegment
Definition: elf.h:131
bool Elf_() rz_bin_elf_has_segments(RZ_NONNULL ELFOBJ *bin)
Definition: elf_segments.c:149
#define PT_LOAD
Definition: common.h:303

References Elf_, in_physical_phdr(), PT_LOAD, rz_bin_elf_foreach_segments, rz_bin_elf_has_segments(), rz_bin_elf_is_relocatable(), rz_return_val_if_fail, RzBinElfSegment, and UT64_MAX.

Referenced by convert_elf_symbol_entry(), fix_rva_and_offset_relocable_file(), get_main_offset_arm64(), get_main_offset_linux_64_pie(), get_main_offset_x86_gcc(), and get_main_offset_x86_pie().

◆ rz_bin_elf_v2p()

ut64 Elf_() rz_bin_elf_v2p ( RZ_NONNULL ELFOBJ bin,
ut64  vaddr 
)

Convert a virtual address to the physical address.

Parameters
elfbinary
Returns
virtual addr

Converts a virtual address to the relative physical address, looking at the program headers in the binary bin

Definition at line 429 of file elf.c.

429  {
431 
433  if (Elf_(rz_bin_elf_is_relocatable)(bin) && vaddr > bin->baddr) {
434  return vaddr - bin->baddr;
435  }
436 
437  return UT64_MAX;
438  }
439 
440  RzBinElfSegment *segment;
441  rz_bin_elf_foreach_segments(bin, segment) {
442  if (segment->data.p_type == PT_LOAD && in_virtual_phdr(segment, vaddr)) {
443  return segment->data.p_offset + vaddr - segment->data.p_vaddr;
444  }
445  }
446 
447  return UT64_MAX;
448 }
static bool in_virtual_phdr(RzBinElfSegment *segment, ut64 addr)
Definition: elf.c:105

References Elf_, in_virtual_phdr(), PT_LOAD, rz_bin_elf_foreach_segments, rz_bin_elf_has_segments(), rz_bin_elf_is_relocatable(), rz_return_val_if_fail, RzBinElfSegment, and UT64_MAX.

Referenced by convert_elf_symbol_entry(), convert_elf_symbol_to_elf_import(), create_section_from_phdr(), Elf_(), fix_rva_and_offset_exec_file(), get_dynamic_elf_symbols(), get_gnu_verneed(), get_gnu_versym(), get_import_addr_mips(), get_import_addr_ppc(), get_import_addr_x86_manual(), get_main_offset_arm64(), get_main_offset_arm_glibc_non_thumb(), get_main_offset_arm_glibc_thumb(), get_main_offset_linux_64_pie(), get_main_offset_mips(), get_main_offset_x86_gcc(), get_main_offset_x86_non_pie(), get_relocs_entry_from_dt_dynamic_aux(), get_versym_entry_sdb_from_verdef(), get_versym_entry_sdb_from_verneed(), init_dt_dynamic(), init_dynstr_aux(), rz_bin_elf_get_entry_offset(), rz_bin_elf_get_fini_offset(), rz_bin_elf_get_gnu_hash_table(), rz_bin_elf_get_hash_table(), and rz_bin_elf_get_init_offset().

Variable Documentation

◆ sdb_elf_class_cparse

const char* sdb_elf_class_cparse = "enum elf_class {ELFCLASSNONE=0, ELFCLASS32=1, ELFCLASS64=2};"
static

Definition at line 59 of file elf.c.

Referenced by init_ehdr_sdb().

◆ sdb_elf_data_cparse

const char* sdb_elf_data_cparse = "enum elf_data {ELFDATANONE=0, ELFDATA2LSB=1, ELFDATA2MSB=2};"
static

Definition at line 61 of file elf.c.

Referenced by init_ehdr_sdb().

◆ sdb_elf_hdr_version_cparse

const char* sdb_elf_hdr_version_cparse = "enum elf_hdr_version {EV_NONE=0, EV_CURRENT=1};"
static

Definition at line 63 of file elf.c.

Referenced by init_ehdr_sdb().

◆ sdb_elf_header_format

const char* sdb_elf_header_format
static
Initial value:
= "?[2]E[2]E[4]ExxxxN2N2N2N2N2N2"
" (elf_ident)ident (elf_type)type (elf_machine)machine (elf_obj_version)version"
" entry phoff shoff flags ehsize phentsize phnum shentsize shnum shstrndx"

Definition at line 100 of file elf.c.

Referenced by init_ehdr_sdb().

◆ sdb_elf_ident_format

const char* sdb_elf_ident_format
static
Initial value:
= "[4]z[1]E[1]E[1]E.::"
" magic (elf_class)class (elf_data)data (elf_hdr_version)version"

Definition at line 67 of file elf.c.

Referenced by init_ehdr_sdb().

◆ sdb_elf_machine_cparse

const char* sdb_elf_machine_cparse
static

Definition at line 21 of file elf.c.

Referenced by init_ehdr_sdb().

◆ sdb_elf_obj_version_cparse

const char* sdb_elf_obj_version_cparse = "enum elf_obj_version {EV_NONE=0, EV_CURRENT=1};"
static

Definition at line 65 of file elf.c.

Referenced by init_ehdr_sdb().

◆ sdb_elf_p_flags_cparse

const char* sdb_elf_p_flags_cparse
static
Initial value:
= "enum elf_p_flags {PF_None=0,PF_Exec=1,"
"PF_Write=2,PF_Write_Exec=3,PF_Read=4,PF_Read_Exec=5,PF_Read_Write=6,"
"PF_Read_Write_Exec=7};"

Definition at line 13 of file elf.c.

Referenced by init_phdr_sdb().

◆ sdb_elf_p_type_cparse

const char* sdb_elf_p_type_cparse
static
Initial value:
= "enum elf_p_type {PT_NULL=0,PT_LOAD=1,PT_DYNAMIC=2,"
"PT_INTERP=3,PT_NOTE=4,PT_SHLIB=5,PT_PHDR=6,PT_LOOS=0x60000000,"
"PT_HIOS=0x6fffffff,PT_LOPROC=0x70000000,PT_HIPROC=0x7fffffff};"

Definition at line 9 of file elf.c.

Referenced by init_phdr_sdb().

◆ sdb_elf_phdr_format

const char* sdb_elf_phdr_format
static
Initial value:
= "[4]Exxxxx[4]Ex (elf_p_type)type offset vaddr paddr"
" filesz memsz (elf_p_flags)flags align"

Definition at line 90 of file elf.c.

Referenced by init_phdr_sdb().

◆ sdb_elf_s_flags_32_cparse

const char* sdb_elf_s_flags_32_cparse
static
Initial value:
= "enum elf_s_flags_32 {SF32_None=0,SF32_Exec=1,"
"SF32_Alloc=2,SF32_Alloc_Exec=3,SF32_Write=4,SF32_Write_Exec=5,"
"SF32_Write_Alloc=6,SF32_Write_Alloc_Exec=7};"

Definition at line 93 of file elf.c.

Referenced by init_shdr_sdb().

◆ sdb_elf_s_type_cparse

const char* sdb_elf_s_type_cparse
static
Initial value:
= "enum elf_s_type {SHT_NULL=0,SHT_PROGBITS=1,"
"SHT_SYMTAB=2,SHT_STRTAB=3,SHT_RELA=4,SHT_HASH=5,SHT_DYNAMIC=6,SHT_NOTE=7,"
"SHT_NOBITS=8,SHT_REL=9,SHT_SHLIB=10,SHT_DYNSYM=11,SHT_LOOS=0x60000000,"
"SHT_HIOS=0x6fffffff,SHT_LOPROC=0x70000000,SHT_HIPROC=0x7fffffff};"

Definition at line 70 of file elf.c.

Referenced by init_shdr_sdb().

◆ sdb_elf_shdr_format

const char* sdb_elf_shdr_format
static
Initial value:
= "x[4]E[4]Exxxxxxx name (elf_s_type)type"
" (elf_s_flags_32)flags addr offset size link info addralign entsize"

Definition at line 97 of file elf.c.

Referenced by init_shdr_sdb().

◆ sdb_elf_type_cparse

const char* sdb_elf_type_cparse
static
Initial value:
= "enum elf_type { ET_NONE=0, ET_REL=1,"
" ET_EXEC=2, ET_DYN=3, ET_CORE=4, ET_LOOS=0xfe00, ET_HIOS=0xfeff,"
" ET_LOPROC=0xff00, ET_HIPROC=0xffff };"

Definition at line 17 of file elf.c.

Referenced by init_ehdr_sdb().