Rizin
unix-like reverse engineering framework and cli tools
omf.h File Reference
#include <rz_util.h>
#include <rz_types.h>
#include <rz_bin.h>
#include "omf_specs.h"

Go to the source code of this file.

Classes

struct  OMF_record_handler
 
struct  OMF_multi_datas
 
struct  OMF_DATA
 
struct  OMF_segment
 
struct  OMF_symbol
 
struct  rz_bin_omf_obj
 

Macros

#define OMF_BASE_ADDR   0x1000
 

Typedefs

typedef struct OMF_record_handler OMF_record_handler
 
typedef struct OMF_DATA OMF_data
 

Functions

bool rz_bin_checksum_omf_ok (const ut8 *buf, ut64 buf_size)
 
rz_bin_omf_objrz_bin_internal_omf_load (const ut8 *buf, ut64 size)
 
void rz_bin_free_all_omf_obj (rz_bin_omf_obj *obj)
 
bool rz_bin_omf_get_entry (rz_bin_omf_obj *obj, RzBinAddr *addr)
 
int rz_bin_omf_get_bits (rz_bin_omf_obj *obj)
 
int rz_bin_omf_send_sections (RzList *list, OMF_segment *section, rz_bin_omf_obj *obj)
 
ut64 rz_bin_omf_get_paddr_sym (rz_bin_omf_obj *obj, OMF_symbol *sym)
 
ut64 rz_bin_omf_get_vaddr_sym (rz_bin_omf_obj *obj, OMF_symbol *sym)
 

Macro Definition Documentation

◆ OMF_BASE_ADDR

#define OMF_BASE_ADDR   0x1000

Definition at line 61 of file omf.h.

Typedef Documentation

◆ OMF_data

typedef struct OMF_DATA OMF_data

◆ OMF_record_handler

Function Documentation

◆ rz_bin_checksum_omf_ok()

bool rz_bin_checksum_omf_ok ( const ut8 buf,
ut64  buf_size 
)

Definition at line 30 of file omf.c.

30  {
31  ut16 size;
32  ut8 checksum = 0;
33 
34  if (buf_size < 3) {
35  RZ_LOG_ERROR("Invalid record (too short)\n");
36  return false;
37  }
38  size = rz_read_le16(buf + 1);
39  if (buf_size < size + 3) {
40  RZ_LOG_ERROR("Invalid record (too short)\n");
41  return false;
42  }
43  // Some compiler set checksum to 0
44  if (!buf[size + 2]) {
45  return true;
46  }
47  size += 3;
48  for (; size; size--) {
49  if (buf_size < size) {
50  RZ_LOG_ERROR("Invalid record (too short)\n");
51  return false;
52  }
53  checksum += buf[size - 1];
54  }
55  if (checksum) {
56  // RZ_LOG_ERROR("Invalid record checksum\n");
57  }
58  return !checksum ? true : false;
59 }
static int buf_size
Definition: debug_qnx.c:35
uint16_t ut16
#define true
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
static ut16 rz_read_le16(const void *src)
Definition: rz_endian.h:206
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58

References buf_size, RZ_LOG_ERROR, rz_read_le16(), and true.

Referenced by check_buffer(), and load_record_omf().

◆ rz_bin_free_all_omf_obj()

void rz_bin_free_all_omf_obj ( rz_bin_omf_obj obj)

Definition at line 665 of file omf.c.

665  {
666  if (obj) {
667  if (obj->records) {
669  }
670  if (obj->sections) {
672  }
673  if (obj->symbols) {
675  }
676  if (obj->names) {
677  free_all_omf_names(obj);
678  }
679  free(obj);
680  }
681 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static void free_all_omf_names(rz_bin_omf_obj *obj)
Definition: omf.c:655
static void free_all_omf_records(rz_bin_omf_obj *obj)
Definition: omf.c:609
static void free_all_omf_sections(rz_bin_omf_obj *obj)
Definition: omf.c:628
static void free_all_omf_symbols(rz_bin_omf_obj *obj)
Definition: omf.c:644
char ** names
Definition: omf.h:50
OMF_segment ** sections
Definition: omf.h:52
OMF_symbol ** symbols
Definition: omf.h:54
OMF_record_handler * records
Definition: omf.h:56

References free(), free_all_omf_names(), free_all_omf_records(), free_all_omf_sections(), free_all_omf_symbols(), rz_bin_omf_obj::names, rz_bin_omf_obj::records, rz_bin_omf_obj::sections, and rz_bin_omf_obj::symbols.

Referenced by destroy(), and rz_bin_internal_omf_load().

◆ rz_bin_internal_omf_load()

rz_bin_omf_obj* rz_bin_internal_omf_load ( const ut8 buf,
ut64  size 
)

Definition at line 683 of file omf.c.

683  {
684  rz_bin_omf_obj *ret = NULL;
685 
686  if (!(ret = RZ_NEW0(rz_bin_omf_obj))) {
687  return NULL;
688  }
689  if (!load_all_omf_records(ret, buf, size)) {
691  return NULL;
692  }
693  if (!(get_omf_infos(ret))) {
695  return NULL;
696  }
698  return ret;
699 }
#define NULL
Definition: cris-opc.c:27
void rz_bin_free_all_omf_obj(rz_bin_omf_obj *obj)
Definition: omf.c:665
static int load_all_omf_records(rz_bin_omf_obj *obj, const ut8 *buf, ut64 size)
Definition: omf.c:418
static int get_omf_infos(rz_bin_omf_obj *obj)
Definition: omf.c:565
#define RZ_NEW0(x)
Definition: rz_types.h:284

References free_all_omf_records(), get_omf_infos(), load_all_omf_records(), NULL, rz_bin_free_all_omf_obj(), and RZ_NEW0.

Referenced by load_buffer().

◆ rz_bin_omf_get_bits()

int rz_bin_omf_get_bits ( rz_bin_omf_obj obj)

Definition at line 731 of file omf.c.

731  {
732  ut32 ct_sec = 0;
733  if (!obj) {
734  return 32;
735  }
736 
737  // we assume if one segdef define a 32 segment all opcodes are 32bits
738  while (ct_sec < obj->nb_section) {
739  if (obj->sections[ct_sec++]->bits == 32) {
740  return 32;
741  }
742  }
743  return 16;
744 }
uint32_t ut32
ut8 bits
Definition: omf.h:37

References OMF_segment::bits, and rz_bin_omf_obj::sections.

Referenced by info().

◆ rz_bin_omf_get_entry()

bool rz_bin_omf_get_entry ( rz_bin_omf_obj obj,
RzBinAddr addr 
)

Definition at line 701 of file omf.c.

701  {
702  ut32 ct_sym = 0;
703  OMF_data *data;
704  ut32 offset = 0;
705 
706  if (!obj) {
707  return false;
708  }
709  while (ct_sym < obj->nb_symbol) {
710  if (!strcmp(obj->symbols[ct_sym]->name, "_start")) {
711  if (obj->symbols[ct_sym]->seg_idx - 1 > obj->nb_section) {
712  RZ_LOG_ERROR("Invalid segment index for symbol _start\n");
713  return false;
714  }
715  addr->vaddr = obj->sections[obj->symbols[ct_sym]->seg_idx - 1]->vaddr + obj->symbols[ct_sym]->offset + OMF_BASE_ADDR;
716  data = obj->sections[obj->symbols[ct_sym]->seg_idx - 1]->data;
717  while (data) {
718  offset += data->size;
719  if (obj->symbols[ct_sym]->offset < offset) {
720  addr->paddr = (obj->symbols[ct_sym]->offset - data->offset) + data->paddr;
721  return true;
722  }
723  data = data->next;
724  }
725  }
726  ct_sym++;
727  }
728  return false;
729 }
voidpf uLong offset
Definition: ioapi.h:144
#define OMF_BASE_ADDR
Definition: omf.h:61
Definition: omf.h:24
ut32 offset
Definition: omf.h:27
struct OMF_DATA * next
Definition: omf.h:29
ut64 size
Definition: omf.h:26
ut64 paddr
Definition: omf.h:25
OMF_data * data
Definition: omf.h:39
ut64 vaddr
Definition: omf.h:38
ut32 offset
Definition: omf.h:45
char * name
Definition: omf.h:43
ut16 seg_idx
Definition: omf.h:44
ut32 nb_section
Definition: omf.h:53
static int addr
Definition: z80asm.c:58

References addr, OMF_segment::data, OMF_symbol::name, rz_bin_omf_obj::nb_section, OMF_DATA::next, OMF_DATA::offset, OMF_symbol::offset, OMF_BASE_ADDR, OMF_DATA::paddr, RZ_LOG_ERROR, rz_bin_omf_obj::sections, OMF_symbol::seg_idx, OMF_DATA::size, rz_bin_omf_obj::symbols, and OMF_segment::vaddr.

Referenced by entries().

◆ rz_bin_omf_get_paddr_sym()

ut64 rz_bin_omf_get_paddr_sym ( rz_bin_omf_obj obj,
OMF_symbol sym 
)

Definition at line 774 of file omf.c.

774  {
775  ut64 offset = 0;
776  if (!obj->sections) {
777  return 0LL;
778  }
779  if (sym->seg_idx - 1 > obj->nb_section) {
780  return 0LL;
781  }
782  int sidx = sym->seg_idx - 1;
783  if (sidx >= obj->nb_section) {
784  return 0LL;
785  }
786  OMF_data *data = obj->sections[sidx]->data;
787  while (data) {
788  offset += data->size;
789  if (sym->offset < offset) {
790  return sym->offset - data->offset + data->paddr;
791  }
792  data = data->next;
793  }
794  return 0;
795 }
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References OMF_segment::data, rz_bin_omf_obj::nb_section, OMF_DATA::next, OMF_DATA::offset, OMF_symbol::offset, OMF_DATA::paddr, rz_bin_omf_obj::sections, OMF_symbol::seg_idx, OMF_DATA::size, and ut64().

Referenced by symbols().

◆ rz_bin_omf_get_vaddr_sym()

ut64 rz_bin_omf_get_vaddr_sym ( rz_bin_omf_obj obj,
OMF_symbol sym 
)

Definition at line 797 of file omf.c.

797  {
798  if (!obj->sections) {
799  return 0LL;
800  }
801  if (sym->seg_idx >= obj->nb_section) {
802  RZ_LOG_ERROR("Invalid segment index for symbol %s\n", sym->name);
803  return 0;
804  }
805  if (sym->seg_idx == 0) {
806  return 0;
807  }
808  return obj->sections[sym->seg_idx - 1]->vaddr + sym->offset + OMF_BASE_ADDR;
809 }

References OMF_symbol::name, rz_bin_omf_obj::nb_section, OMF_symbol::offset, OMF_BASE_ADDR, RZ_LOG_ERROR, rz_bin_omf_obj::sections, OMF_symbol::seg_idx, and OMF_segment::vaddr.

Referenced by symbols().

◆ rz_bin_omf_send_sections()

int rz_bin_omf_send_sections ( RzList list,
OMF_segment section,
rz_bin_omf_obj obj 
)

Definition at line 746 of file omf.c.

746  {
747  RzBinSection *new;
748  OMF_data *data = section->data;
749  ut32 ct_name = 1;
750 
751  while (data) {
752  if (!(new = RZ_NEW0(RzBinSection))) {
753  return false;
754  }
755 
756  // if index == 0, it's mean there is no name
757  if (section->name_idx && section->name_idx - 1 < obj->nb_name) {
758  new->name = rz_str_newf("%s_%d", obj->names[section->name_idx - 1], ct_name++);
759  } else {
760  new->name = rz_str_newf("no_name_%d", ct_name++);
761  }
762 
763  new->size = data->size;
764  new->vsize = data->size;
765  new->paddr = data->paddr;
766  new->vaddr = section->vaddr + data->offset + OMF_BASE_ADDR;
767  new->perm = RZ_PERM_RWX;
768  rz_list_append(list, new);
769  data = data->next;
770  }
771  return true;
772 }
static void list(RzEgg *egg)
Definition: rz-gg.c:52
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 char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_PERM_RWX
Definition: rz_types.h:98
ut32 nb_name
Definition: omf.h:51

References list(), rz_bin_omf_obj::names, rz_bin_omf_obj::nb_name, OMF_DATA::next, OMF_DATA::offset, OMF_BASE_ADDR, OMF_DATA::paddr, rz_list_append(), RZ_NEW0, RZ_PERM_RWX, rz_str_newf(), and OMF_DATA::size.

Referenced by sections().