Skip to content

Commit

Permalink
Fix NE relocation naming
Browse files Browse the repository at this point in the history
* Name NE relocations by combining `src` and `flag`
* Fix old test + add additional tests
  • Loading branch information
Roeegg2 committed Nov 22, 2024
1 parent 266fe6b commit c5d0f32
Show file tree
Hide file tree
Showing 3 changed files with 1,036 additions and 34 deletions.
57 changes: 41 additions & 16 deletions librz/bin/format/ne/ne.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,46 @@ static void ne_sanitize_name(char *name, ut16 count) {
}
}

static const char *get_reloc_type_name(const ut8 src_type, const ut8 flag) {
#define CONCAT(a, b) a b
#define NE_RELOC_TARGET_TYPE(src_type_name, flag) \
switch (flag) { \
case INTERNAL_REF: \
return CONCAT(src_type_name, "_INTERNAL_REF"); \
case IMPORTED_ORD: \
return CONCAT(src_type_name, "_IMPORTED_ORD"); \
case IMPORTED_NAME: \
return CONCAT(src_type_name, "_IMPORTED_NAME"); \
case OSFIXUP: \
return CONCAT(src_type_name, "_OSFIXUP"); \
case ADDITIVE: \
return CONCAT(src_type_name, "_ADDITIVE"); \
default: \
RZ_LOG_ERROR("Unknown NE relocation target flag %d\n", flag); \
return CONCAT(src_type_name, "_UNKNOWN"); \
}

switch (src_type) {
case LOBYTE:
NE_RELOC_TARGET_TYPE("LOBYTE", flag);
case SEL_16:
NE_RELOC_TARGET_TYPE("SEL_16", flag);
case POI_32:
NE_RELOC_TARGET_TYPE("POI_32", flag);
case OFF_16:
NE_RELOC_TARGET_TYPE("OFF_16", flag);
case POI_48:
NE_RELOC_TARGET_TYPE("POI_48", flag);
case OFF_32:
NE_RELOC_TARGET_TYPE("OFF_32", flag);
default:
RZ_LOG_ERROR("Unknown NE relocation source type %d\n", flag);
NE_RELOC_TARGET_TYPE("UNKNOWN", flag);
}
#undef NE_RELOC_TARGET_TYPE
#undef CONCAT
}

RzPVector /*<RzBinSymbol *>*/ *rz_bin_ne_get_symbols(rz_bin_ne_obj_t *bin) {
RzBinSymbol *sym;
ut16 off = bin->ne_header->ResidNamTable + bin->header_offset;
Expand Down Expand Up @@ -537,22 +577,7 @@ RzPVector /*<RzBinReloc *>*/ *rz_bin_ne_get_relocs(rz_bin_ne_obj_t *bin) {
rz_buf_read_le16_offset(bin->buf, &offset, &rel.align1);
rz_buf_read_le16_offset(bin->buf, &offset, &rel.func_ord);
reloc->paddr = seg->paddr + rel.offset;
switch (rel.type) {
case LOBYTE:
reloc->type = RZ_BIN_RELOC_8;
break;
case SEL_16:
case OFF_16:
reloc->type = RZ_BIN_RELOC_16;
break;
case POI_32:
case OFF_32:
reloc->type = RZ_BIN_RELOC_32;
break;
case POI_48:
reloc->type = RZ_BIN_RELOC_64;
break;
}
reloc->print_name = get_reloc_type_name(rel.type & NE_RELOC_SRC_MASK, rel.flags & NE_RELOC_TARGET_MASK);

if (rel.flags & (IMPORTED_ORD | IMPORTED_NAME)) {
RzBinImport *imp = RZ_NEW0(RzBinImport);
Expand Down
3 changes: 3 additions & 0 deletions librz/bin/format/ne/ne_specs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#ifndef NE_SPECS_H
#define NE_SPECS_H

#define NE_RELOC_SRC_MASK 0x0F
#define NE_RELOC_TARGET_MASK 0x03

enum {
LOBYTE = 0,
SEL_16 = 2,
Expand Down
Loading

0 comments on commit c5d0f32

Please sign in to comment.