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

Go to the source code of this file.

Classes

struct  type_translation
 
struct  flag_translation
 

Functions

static bool create_section_from_phdr (ELFOBJ *bin, RzVector *result, const char *name, ut64 addr, ut64 sz)
 
static const char * get_plt_name (ELFOBJ *bin)
 
static bool create_section_plt (ELFOBJ *bin, RzVector *result)
 
static void rz_bin_elf_section_free (void *e, RZ_UNUSED void *user)
 
static RzVectorget_sections_from_dt_dynamic (ELFOBJ *bin)
 
static bool get_shdr_entry_aux (ELFOBJ *bin, Elf_(Shdr) *section, ut64 offset)
 
static bool get_shdr_entry (ELFOBJ *bin, Elf_(Shdr) *section, ut64 offset)
 
static bool set_elf_section_name (ELFOBJ *bin, RzBinElfSection *section, Elf_(Shdr) *shdr, size_t id)
 
static bool set_elf_section_aux (ELFOBJ *bin, RzBinElfSection *section, Elf_(Shdr) *shdr, size_t id)
 
static bool verify_shdr_entry (ELFOBJ *bin, Elf_(Shdr) *section)
 
static bool set_elf_section (ELFOBJ *bin, RzBinObjectLoadOptions *option, RzBinElfSection *section, Elf_(Shdr) *shdr, size_t id)
 
RZ_BORROW RzBinElfSection *Elf_() rz_bin_elf_get_section (RZ_NONNULL ELFOBJ *bin, Elf_(Half) index)
 
RZ_BORROW RzBinElfSection *Elf_() rz_bin_elf_get_section_with_name (RZ_NONNULL ELFOBJ *bin, RZ_NONNULL const char *name)
 
RZ_OWN RzList *Elf_() rz_bin_elf_section_flag_to_rzlist (ut64 flag)
 Return a list of string representing flag options. More...
 
static RzBinElfSection convert_elf_section (ELFOBJ *bin, RzBinObjectLoadOptions *options, Elf_(Shdr) *shdr, size_t pos)
 
static RzVectorconvert_sections_from_shdr (ELFOBJ *bin, RzBinObjectLoadOptions *options, RzVector *sections)
 
static RzVectorconvert_sections (ELFOBJ *bin, RzBinObjectLoadOptions *options, RzVector *sections)
 
RZ_OWN RzVector *Elf_() rz_bin_elf_convert_sections (RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RzBinObjectLoadOptions *options, RzVector *sections)
 
RZ_OWN RzVector *Elf_() rz_bin_elf_sections_new (RZ_NONNULL ELFOBJ *bin)
 
RZ_OWN char *Elf_() rz_bin_elf_section_type_to_string (ut64 type)
 Return a string representing the elf type. More...
 
bool Elf_() rz_bin_elf_has_sections (RZ_NONNULL ELFOBJ *bin)
 

Variables

static const struct type_translation type_translation_table []
 
static const struct flag_translation flag_translation_table []
 

Function Documentation

◆ convert_elf_section()

static RzBinElfSection convert_elf_section ( ELFOBJ bin,
RzBinObjectLoadOptions options,
Elf_(Shdr) *  shdr,
size_t  pos 
)
static

Definition at line 316 of file elf_sections.c.

316  {
318 
319  if (!set_elf_section(bin, options, &section, shdr, pos)) {
320  RZ_LOG_WARN("The section %zu at 0x%" PFMT64x " seems to be invalid.\n", pos, section.offset);
321  }
322 
323  return section;
324 }
static bool set_elf_section(ELFOBJ *bin, RzBinObjectLoadOptions *option, RzBinElfSection *section, Elf_(Shdr) *shdr, size_t id)
Definition: elf_sections.c:252
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
#define PFMT64x
Definition: rz_types.h:393
Definition: malloc.c:26
uint32_t offset
int pos
Definition: main.c:11

References section::offset, options, PFMT64x, pos, RZ_LOG_WARN, and set_elf_section().

Referenced by convert_sections_from_shdr().

◆ convert_sections()

static RzVector* convert_sections ( ELFOBJ bin,
RzBinObjectLoadOptions options,
RzVector sections 
)
static

Definition at line 349 of file elf_sections.c.

349  {
351  if (result) {
352  return result;
353  }
354 
357  }
358 
359  return result;
360 }
RzList * sections(RzBinFile *bf)
Definition: bin_ne.c:110
bool Elf_() rz_bin_elf_has_segments(RZ_NONNULL ELFOBJ *bin)
Definition: elf_segments.c:149
static RzVector * get_sections_from_dt_dynamic(ELFOBJ *bin)
Definition: elf_sections.c:124
static RzVector * convert_sections_from_shdr(ELFOBJ *bin, RzBinObjectLoadOptions *options, RzVector *sections)
Definition: elf_sections.c:326
#define Elf_(name)
Definition: elf_specs.h:32

References convert_sections_from_shdr(), Elf_, get_sections_from_dt_dynamic(), options, rz_bin_elf_has_segments(), and sections().

Referenced by rz_bin_elf_convert_sections().

◆ convert_sections_from_shdr()

static RzVector* convert_sections_from_shdr ( ELFOBJ bin,
RzBinObjectLoadOptions options,
RzVector sections 
)
static

Definition at line 326 of file elf_sections.c.

326  {
327  if (!sections) {
328  return NULL;
329  }
330 
332  if (!result) {
333  return NULL;
334  }
335 
336  size_t i;
337  Elf_(Shdr) * section;
340  if (!rz_vector_push(result, &tmp)) {
341  rz_vector_free(result);
342  return NULL;
343  }
344  }
345 
346  return result;
347 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
static void rz_bin_elf_section_free(void *e, RZ_UNUSED void *user)
Definition: elf_sections.c:119
static RzBinElfSection convert_elf_section(ELFOBJ *bin, RzBinObjectLoadOptions *options, Elf_(Shdr) *shdr, size_t pos)
Definition: elf_sections.c:316
RZ_API void * rz_vector_push(RzVector *vec, void *x)
Definition: vector.c:197
RZ_API void rz_vector_free(RzVector *vec)
Definition: vector.c:75
RZ_API RzVector * rz_vector_new(size_t elem_size, RzVectorFree free, void *free_user)
Definition: vector.c:42
#define rz_vector_enumerate(vec, it, i)
Definition: rz_vector.h:177

References convert_elf_section(), Elf_, i, NULL, options, rz_bin_elf_section_free(), rz_vector_enumerate, rz_vector_free(), rz_vector_new(), rz_vector_push(), sections(), and autogen_x86imm::tmp.

Referenced by convert_sections().

◆ create_section_from_phdr()

static bool create_section_from_phdr ( ELFOBJ bin,
RzVector result,
const char *  name,
ut64  addr,
ut64  sz 
)
static

Definition at line 66 of file elf_sections.c.

66  {
67  RzBinElfSection section = { 0 };
68 
70  if (section.offset == UT64_MAX) {
71  RZ_LOG_WARN("Failed to convert section virtual address to physical address.\n")
72  return false;
73  }
74 
75  section.rva = addr;
76  section.size = sz;
78  if (!section.name) {
79  return false;
80  }
81 
82  if (!rz_vector_push(result, &section)) {
83  return false;
84  }
85 
86  return true;
87 }
static ut64 rva(RzBinObject *o, ut64 paddr, ut64 vaddr, int va)
Definition: cbin.c:77
#define false
ut64 Elf_() rz_bin_elf_v2p(RZ_NONNULL ELFOBJ *bin, ut64 vaddr)
Convert a virtual address to the physical address.
Definition: elf.c:429
voidpf void uLong size
Definition: ioapi.h:138
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 UT64_MAX
Definition: rz_types_base.h:86
Definition: z80asm.h:102
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
static int addr
Definition: z80asm.c:58

References addr, Elf_, section::offset, rz_bin_elf_v2p(), RZ_LOG_WARN, rz_vector_push(), section::size, strdup(), and UT64_MAX.

Referenced by create_section_plt(), and get_sections_from_dt_dynamic().

◆ create_section_plt()

static bool create_section_plt ( ELFOBJ bin,
RzVector result 
)
static

Definition at line 103 of file elf_sections.c.

103  {
104  ut64 addr;
105  ut64 size;
106 
107  const char *plt_name = get_plt_name(bin);
108  if (!plt_name) {
109  return true;
110  }
111 
113  return true;
114  }
115 
116  return create_section_from_phdr(bin, result, plt_name, addr, size);
117 }
bool Elf_() rz_bin_elf_get_dt_info(RZ_NONNULL ELFOBJ *bin, ut64 key, RZ_OUT ut64 *info)
Definition: elf_dynamic.c:120
static const char * get_plt_name(ELFOBJ *bin)
Definition: elf_sections.c:89
static bool create_section_from_phdr(ELFOBJ *bin, RzVector *result, const char *name, ut64 addr, ut64 sz)
Definition: elf_sections.c:66
#define DT_PLTRELSZ
Definition: common.h:539
#define DT_JMPREL
Definition: common.h:560
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References addr, create_section_from_phdr(), DT_JMPREL, DT_PLTRELSZ, Elf_, get_plt_name(), rz_bin_elf_get_dt_info(), and ut64().

Referenced by get_sections_from_dt_dynamic().

◆ get_plt_name()

static const char* get_plt_name ( ELFOBJ bin)
static

Definition at line 89 of file elf_sections.c.

89  {
90  ut64 dt_pltrel;
91 
92  if (!Elf_(rz_bin_elf_get_dt_info)(bin, DT_PLTREL, &dt_pltrel)) {
93  return NULL;
94  }
95 
96  if (dt_pltrel == DT_REL) {
97  return ".rel.plt";
98  }
99 
100  return ".rela.plt";
101 }
#define DT_PLTREL
Definition: common.h:557
#define DT_REL
Definition: common.h:554

References DT_PLTREL, DT_REL, Elf_, NULL, rz_bin_elf_get_dt_info(), and ut64().

Referenced by create_section_plt().

◆ get_sections_from_dt_dynamic()

static RzVector* get_sections_from_dt_dynamic ( ELFOBJ bin)
static

Definition at line 124 of file elf_sections.c.

124  {
125  ut64 addr;
126  ut64 size;
127 
129  if (!result) {
130  return NULL;
131  }
132 
133  // There is no info about the got size
135  if (!create_section_from_phdr(bin, result, ".got.plt", addr, 0)) {
136  rz_vector_free(result);
137  return NULL;
138  }
139  }
140 
142  if (!create_section_from_phdr(bin, result, ".rel.dyn", addr, size)) {
143  rz_vector_free(result);
144  return NULL;
145  }
146  }
147 
149  if (!create_section_from_phdr(bin, result, ".rela.dyn", addr, size)) {
150  rz_vector_free(result);
151  return NULL;
152  }
153  }
154 
155  if (!create_section_plt(bin, result)) {
156  rz_vector_free(result);
157  return NULL;
158  }
159 
160  if (!rz_vector_len(result)) {
161  rz_vector_free(result);
162  return NULL;
163  }
164 
165  return result;
166 }
static bool create_section_plt(ELFOBJ *bin, RzVector *result)
Definition: elf_sections.c:103
#define DT_PLTGOT
Definition: common.h:540
#define DT_RELSZ
Definition: common.h:555
#define DT_RELASZ
Definition: common.h:545
#define DT_RELA
Definition: common.h:544
static size_t rz_vector_len(const RzVector *vec)
Definition: rz_vector.h:82

References addr, create_section_from_phdr(), create_section_plt(), DT_PLTGOT, DT_REL, DT_RELA, DT_RELASZ, DT_RELSZ, Elf_, NULL, rz_bin_elf_get_dt_info(), rz_bin_elf_section_free(), rz_vector_free(), rz_vector_len(), rz_vector_new(), and ut64().

Referenced by convert_sections().

◆ get_shdr_entry()

static bool get_shdr_entry ( ELFOBJ bin,
Elf_(Shdr) *  section,
ut64  offset 
)
static

Definition at line 181 of file elf_sections.c.

181  {
183  RZ_LOG_WARN("Failed to read section entry at 0x%" PFMT64x ".\n", offset);
184  return false;
185  }
186 
187  return true;
188 }
static bool get_shdr_entry_aux(ELFOBJ *bin, Elf_(Shdr) *section, ut64 offset)
Definition: elf_sections.c:168
voidpf uLong offset
Definition: ioapi.h:144

References get_shdr_entry_aux(), PFMT64x, and RZ_LOG_WARN.

Referenced by rz_bin_elf_sections_new().

◆ get_shdr_entry_aux()

static bool get_shdr_entry_aux ( ELFOBJ bin,
Elf_(Shdr) *  section,
ut64  offset 
)
static

Definition at line 168 of file elf_sections.c.

168  {
169  return Elf_(rz_bin_elf_read_word)(bin, &offset, &section->sh_name) &&
170  Elf_(rz_bin_elf_read_word)(bin, &offset, &section->sh_type) &&
172  Elf_(rz_bin_elf_read_addr)(bin, &offset, &section->sh_addr) &&
173  Elf_(rz_bin_elf_read_off)(bin, &offset, &section->sh_offset) &&
175  Elf_(rz_bin_elf_read_word)(bin, &offset, &section->sh_link) &&
176  Elf_(rz_bin_elf_read_word)(bin, &offset, &section->sh_info) &&
177  Elf_(rz_bin_elf_read_word_xword)(bin, &offset, &section->sh_addralign) &&
179 }
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_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_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_word(RZ_NONNULL ELFOBJ *bin, RZ_NONNULL RZ_INOUT ut64 *offset, RZ_NONNULL RZ_OUT Elf_(Word) *result)
Definition: elf_misc.c:62

References Elf_, rz_bin_elf_read_addr(), rz_bin_elf_read_off(), rz_bin_elf_read_word(), and rz_bin_elf_read_word_xword().

Referenced by get_shdr_entry().

◆ rz_bin_elf_convert_sections()

RZ_OWN RzVector* Elf_() rz_bin_elf_convert_sections ( RZ_NONNULL ELFOBJ bin,
RZ_NONNULL RzBinObjectLoadOptions options,
RzVector sections 
)

Definition at line 362 of file elf_sections.c.

362  {
364 
366  if (!result) {
367  return NULL;
368  }
369 
370  if (!rz_vector_len(result)) {
371  rz_vector_free(result);
372  return NULL;
373  }
374 
375  return result;
376 }
static RzVector * convert_sections(ELFOBJ *bin, RzBinObjectLoadOptions *options, RzVector *sections)
Definition: elf_sections.c:349
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References convert_sections(), NULL, options, rz_return_val_if_fail, rz_vector_free(), rz_vector_len(), and sections().

Referenced by init_shdr_aux().

◆ rz_bin_elf_get_section()

RZ_BORROW RzBinElfSection* Elf_() rz_bin_elf_get_section ( RZ_NONNULL ELFOBJ bin,
Elf_(Half)  index 
)

Definition at line 264 of file elf_sections.c.

264  {
266 
267  if (!bin->sections) {
268  return NULL;
269  }
270 
271  if (index < rz_vector_len(bin->sections)) {
272  return rz_vector_index_ptr(bin->sections, index);
273  }
274 
275  return NULL;
276 }
static void * rz_vector_index_ptr(RzVector *vec, size_t index)
Definition: rz_vector.h:88

References NULL, rz_return_val_if_fail, rz_vector_index_ptr(), and rz_vector_len().

Referenced by convert_elf_symbol_entry(), fix_rva_and_offset_relocable_file(), and get_section_elf_symbols().

◆ rz_bin_elf_get_section_with_name()

RZ_BORROW RzBinElfSection* Elf_() rz_bin_elf_get_section_with_name ( RZ_NONNULL ELFOBJ bin,
RZ_NONNULL const char *  name 
)

Definition at line 278 of file elf_sections.c.

278  {
280 
283  if (section->is_valid && section->name && !strcmp(section->name, name)) {
284  return section;
285  }
286  }
287 
288  return NULL;
289 }
#define rz_bin_elf_foreach_sections(bin, section)
Definition: elf.h:30

References NULL, rz_bin_elf_foreach_sections, and rz_return_val_if_fail.

Referenced by get_entry_offset_from_shdr(), get_gnu_debugdata_elf_symbols(), get_import_addr_x86(), get_import_addr_x86_manual(), and rz_bin_elf_get_compiler().

◆ rz_bin_elf_has_sections()

bool Elf_() rz_bin_elf_has_sections ( RZ_NONNULL ELFOBJ bin)

Definition at line 440 of file elf_sections.c.

440  {
441  rz_return_val_if_fail(bin, false);
442 
443  return bin->sections;
444 }

References rz_return_val_if_fail.

Referenced by rz_bin_elf_is_stripped().

◆ rz_bin_elf_section_flag_to_rzlist()

RZ_OWN RzList* Elf_() rz_bin_elf_section_flag_to_rzlist ( ut64  flag)

Return a list of string representing flag options.

Parameters
elfflag
Returns
RzList of string representing flag options

Compare the flag to common option such as SHF_WRITE, SHF_ALLOC, ect

Definition at line 298 of file elf_sections.c.

298  {
299  RzList *flag_list = rz_list_new();
300  if (!flag_list) {
301  return NULL;
302  }
303 
304  for (size_t i = 0; i < RZ_ARRAY_SIZE(flag_translation_table); i++) {
305  if (flag & flag_translation_table[i].flag) {
306  if (!rz_list_append(flag_list, flag_translation_table[i].name)) {
307  rz_list_free(flag_list);
308  return NULL;
309  }
310  }
311  }
312 
313  return flag_list;
314 }
static const struct flag_translation flag_translation_table[]
Definition: elf_sections.c:51
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300

References flag_translation::flag, flag_translation_table, i, NULL, RZ_ARRAY_SIZE, rz_list_append(), rz_list_free(), and rz_list_new().

◆ rz_bin_elf_section_free()

static void rz_bin_elf_section_free ( void *  e,
RZ_UNUSED void *  user 
)
static

Definition at line 119 of file elf_sections.c.

119  {
120  RzBinElfSection *ptr = e;
121  free(ptr->name);
122 }
#define e(frag)
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References e, free(), and rz_bin_elf_section_t::name.

Referenced by convert_sections_from_shdr(), and get_sections_from_dt_dynamic().

◆ rz_bin_elf_section_type_to_string()

RZ_OWN char* Elf_() rz_bin_elf_section_type_to_string ( ut64  type)

Return a string representing the elf type.

Parameters
elftype
Returns
allocated string

Compare the type SHT_NULL, SHT_PROGBITS, etc and return the string representation

Definition at line 422 of file elf_sections.c.

422  {
423  for (size_t i = 0; i < RZ_ARRAY_SIZE(type_translation_table); i++) {
424  if (type == type_translation_table[i].type) {
426  }
427  }
428 
429  if (type >= SHT_LOPROC && type <= SHT_HIPROC) {
430  return rz_str_newf("LOPROC+0x%08" PFMT64x, type - SHT_LOPROC);
431  }
432 
433  if (type >= SHT_LOUSER && type <= SHT_HIUSER) {
434  return rz_str_newf("LOUSER+0x%08" PFMT64x, type - SHT_LOUSER);
435  }
436 
437  return rz_str_newf("0x%" PFMT64x, type);
438 }
static const struct type_translation type_translation_table[]
Definition: elf_sections.c:19
#define SHT_LOPROC
Definition: common.h:368
#define SHT_LOUSER
Definition: common.h:370
#define SHT_HIPROC
Definition: common.h:369
#define SHT_HIUSER
Definition: common.h:372
int type
Definition: mipsasm.c:17
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865

References i, PFMT64x, RZ_ARRAY_SIZE, rz_str_new(), rz_str_newf(), SHT_HIPROC, SHT_HIUSER, SHT_LOPROC, SHT_LOUSER, type, and type_translation_table.

◆ rz_bin_elf_sections_new()

RZ_OWN RzVector* Elf_() rz_bin_elf_sections_new ( RZ_NONNULL ELFOBJ bin)

Definition at line 378 of file elf_sections.c.

378  {
380 
381  if (!bin->ehdr.e_shnum) {
382  return NULL;
383  }
384 
385  if (!Elf_(rz_bin_elf_check_array)(bin, bin->ehdr.e_shoff, bin->ehdr.e_shnum, sizeof(Elf_(Phdr)))) {
386  RZ_LOG_WARN("Invalid section header (check array failed).\n");
387  return NULL;
388  }
389 
390  RzVector *result = rz_vector_new(sizeof(Elf_(Shdr)), NULL, NULL);
391  if (!result) {
392  return NULL;
393  }
394 
395  ut64 offset = bin->ehdr.e_shoff;
396 
397  for (size_t i = 0; i < bin->ehdr.e_shnum; i++) {
398  Elf_(Shdr) *section = rz_vector_push(result, NULL);
399  if (!section) {
400  rz_vector_free(result);
401  return NULL;
402  }
403 
404  if (!get_shdr_entry(bin, section, offset)) {
405  rz_vector_free(result);
406  return NULL;
407  }
408 
409  offset += sizeof(Elf_(Shdr));
410  }
411 
412  return result;
413 }
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
static bool get_shdr_entry(ELFOBJ *bin, Elf_(Shdr) *section, ut64 offset)
Definition: elf_sections.c:181

References Elf_, get_shdr_entry(), i, NULL, rz_bin_elf_check_array(), RZ_LOG_WARN, rz_return_val_if_fail, rz_vector_free(), rz_vector_new(), rz_vector_push(), and ut64().

Referenced by init().

◆ set_elf_section()

static bool set_elf_section ( ELFOBJ bin,
RzBinObjectLoadOptions option,
RzBinElfSection section,
Elf_(Shdr) *  shdr,
size_t  id 
)
static

Definition at line 252 of file elf_sections.c.

252  {
253  bool tmp = set_elf_section_aux(bin, section, shdr, id);
254 
255  if (!option->elf_checks_sections) {
256  section->is_valid = true;
257  return true;
258  }
259 
260  section->is_valid = tmp && verify_shdr_entry(bin, shdr);
261  return section->is_valid;
262 }
static bool set_elf_section_aux(ELFOBJ *bin, RzBinElfSection *section, Elf_(Shdr) *shdr, size_t id)
Definition: elf_sections.c:210
static bool verify_shdr_entry(ELFOBJ *bin, Elf_(Shdr) *section)
Definition: elf_sections.c:231
Definition: getopt.h:84

References set_elf_section_aux(), autogen_x86imm::tmp, and verify_shdr_entry().

Referenced by convert_elf_section().

◆ set_elf_section_aux()

static bool set_elf_section_aux ( ELFOBJ bin,
RzBinElfSection section,
Elf_(Shdr) *  shdr,
size_t  id 
)
static

Definition at line 210 of file elf_sections.c.

210  {
211  section->offset = shdr->sh_offset;
212  section->size = shdr->sh_size;
213  section->align = shdr->sh_addralign;
214  section->flags = shdr->sh_flags;
215  section->link = shdr->sh_link;
216  section->info = shdr->sh_info;
217  section->type = shdr->sh_type;
218 
220  section->rva = bin->baddr + shdr->sh_offset;
221  } else {
222  if (shdr->sh_flags & SHF_ALLOC) {
223  section->rva = shdr->sh_addr;
224  } else {
225  section->rva = UT64_MAX;
226  }
227  }
228  return set_elf_section_name(bin, section, shdr, id);
229 }
bool Elf_() rz_bin_elf_is_relocatable(RZ_NONNULL ELFOBJ *bin)
Check if the elf binary is relocatable.
Definition: elf_info.c:1662
static bool set_elf_section_name(ELFOBJ *bin, RzBinElfSection *section, Elf_(Shdr) *shdr, size_t id)
Definition: elf_sections.c:190
#define SHF_ALLOC
Definition: common.h:377
uint32_t flags
uint32_t size
uint32_t align

References section::align, Elf_, section::flags, section::offset, rz_bin_elf_is_relocatable(), set_elf_section_name(), SHF_ALLOC, section::size, and UT64_MAX.

Referenced by set_elf_section().

◆ set_elf_section_name()

static bool set_elf_section_name ( ELFOBJ bin,
RzBinElfSection section,
Elf_(Shdr) *  shdr,
size_t  id 
)
static

Definition at line 190 of file elf_sections.c.

190  {
191  if (!bin->shstrtab || !Elf_(rz_bin_elf_strtab_has_index)(bin->shstrtab, shdr->sh_name)) {
192  section->name = rz_str_newf("invalid%zu", id);
193  return false;
194  }
195 
196  if (shdr->sh_type == SHT_NULL) {
197  section->name = NULL;
198  return true;
199  }
200 
201  section->name = Elf_(rz_bin_elf_strtab_get_dup)(bin->shstrtab, shdr->sh_name);
202  if (section->name) {
203  return true;
204  }
205 
206  section->name = NULL;
207  return false;
208 }
bool Elf_() rz_bin_elf_strtab_has_index(RZ_NONNULL RzBinElfStrtab *strtab, ut64 index)
Definition: elf_strtab.c:67
RZ_OWN char *Elf_() rz_bin_elf_strtab_get_dup(RZ_NONNULL RzBinElfStrtab *strtab, ut64 index)
Definition: elf_strtab.c:52
#define SHT_NULL
Definition: common.h:331

References Elf_, NULL, rz_bin_elf_strtab_get_dup(), rz_bin_elf_strtab_has_index(), rz_str_newf(), and SHT_NULL.

Referenced by set_elf_section_aux().

◆ verify_shdr_entry()

static bool verify_shdr_entry ( ELFOBJ bin,
Elf_(Shdr) *  section 
)
static

Definition at line 231 of file elf_sections.c.

231  {
232  if (section->sh_link != SHT_SUNW_COMDAT && section->sh_link >= bin->ehdr.e_shnum) {
233  return false;
234  }
235 
236  Elf_(Off) end_off;
237  if (!Elf_(rz_bin_elf_add_off)(&end_off, section->sh_offset, section->sh_size)) {
238  return false;
239  }
240 
241  if (section->sh_type != SHT_NOBITS && end_off > bin->size) {
242  return false;
243  }
244 
245  if (!Elf_(rz_bin_elf_add_addr)(NULL, section->sh_addr, section->sh_size)) {
246  return false;
247  }
248 
249  return true;
250 }
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_add_addr(Elf_(Addr) *result, Elf_(Addr) addr, Elf_(Addr) value)
Definition: elf_misc.c:138
#define SHT_SUNW_COMDAT
Definition: glibc_elf.h:456
#define SHT_NOBITS
Definition: common.h:339

References Elf_, NULL, rz_bin_elf_add_addr(), rz_bin_elf_add_off(), SHT_NOBITS, and SHT_SUNW_COMDAT.

Referenced by set_elf_section().

Variable Documentation

◆ flag_translation_table

const struct flag_translation flag_translation_table[]
static
Initial value:
= {
{ SHF_WRITE, "write" },
{ SHF_ALLOC, "alloc" },
{ SHF_EXECINSTR, "execute" },
{ SHF_MERGE, "merge" },
{ SHF_STRINGS, "strings" },
{ SHF_INFO_LINK, "info" },
{ SHF_LINK_ORDER, "link_order" },
{ SHF_OS_NONCONFORMING, "extra_os_processing_reqd" },
{ SHF_GROUP, "group" },
{ SHF_TLS, "TLS" },
{ SHF_EXCLUDE, "exclude" },
{ SHF_COMPRESSED, "compressed" }
}
#define SHF_COMPRESSED
Definition: glibc_elf.h:480
#define SHF_EXCLUDE
Definition: glibc_elf.h:484
#define SHF_WRITE
Definition: common.h:376
#define SHF_TLS
Definition: common.h:385
#define SHF_LINK_ORDER
Definition: common.h:382
#define SHF_MERGE
Definition: common.h:379
#define SHF_OS_NONCONFORMING
Definition: common.h:383
#define SHF_INFO_LINK
Definition: common.h:381
#define SHF_STRINGS
Definition: common.h:380
#define SHF_EXECINSTR
Definition: common.h:378
#define SHF_GROUP
Definition: common.h:384

Definition at line 1 of file elf_sections.c.

Referenced by rz_bin_elf_section_flag_to_rzlist().

◆ type_translation_table

const struct type_translation type_translation_table[]
static

Definition at line 1 of file elf_sections.c.

Referenced by rz_bin_elf_section_type_to_string().