From 2e68499fb6303d5560d286fed3480d34358dc66b Mon Sep 17 00:00:00 2001 From: pancake Date: Fri, 3 Jan 2025 18:57:04 +0100 Subject: [PATCH] Use packed RSysBits everywhere and deprecate R_SYS_BITS_X ##util --- binr/r2r/load.c | 5 +- binr/r2r/r2r.c | 11 +- binr/r2r/r2r.h | 9 +- libr/arch/arch_config.c | 8 +- libr/arch/p/arm/plugin.c | 25 +-- libr/bin/format/le/le.c | 4 +- libr/bin/format/ne/ne.c | 4 +- libr/bin/format/pe/dotnet.c | 126 ++++++------ libr/bin/format/pe/dotnet.h | 192 +++++++++--------- libr/bin/p/bin_te.c | 2 +- libr/bin/p/pe.mk | 2 +- libr/core/cbin.c | 2 +- libr/core/cconfig.c | 17 +- libr/core/cfile.c | 2 + libr/core/cio.c | 14 +- libr/core/cmd.c | 7 +- libr/core/cmd_debug.inc.c | 18 +- libr/core/core.c | 12 +- libr/core/dmh_windows.inc.c | 16 +- libr/core/visual.c | 4 +- libr/debug/debug.c | 33 ++- libr/debug/dmap.c | 11 +- libr/debug/dreg.c | 6 +- libr/debug/p/debug_bf.c | 2 +- libr/debug/p/debug_bochs.c | 2 +- libr/debug/p/debug_esil.c | 2 +- libr/debug/p/debug_evm.c | 2 +- libr/debug/p/debug_gdb.c | 2 +- libr/debug/p/debug_io.c | 2 +- libr/debug/p/debug_native.c | 20 +- libr/debug/p/debug_null.c | 4 +- libr/debug/p/debug_qnx.c | 2 +- libr/debug/p/debug_rap.c | 2 +- libr/debug/p/debug_rv32ima.c | 2 +- libr/debug/p/debug_windbg.c | 4 +- libr/debug/p/debug_winkd.c | 9 +- libr/debug/p/native/bt.c | 4 +- libr/debug/p/native/linux/linux_debug.c | 28 ++- libr/debug/p/native/reg.c | 2 +- libr/debug/p/native/windows/windows_debug.c | 29 +-- libr/debug/p/native/windows/windows_message.c | 2 +- libr/debug/p/native/xnu/trap_arm.c | 2 +- libr/debug/p/native/xnu/trap_x86.c | 2 +- libr/debug/p/native/xnu/xnu_debug.c | 16 +- libr/debug/p/native/xnu/xnu_debug.h | 8 +- libr/debug/p/native/xnu/xnu_threads.c | 22 +- libr/include/r_core.h | 2 +- libr/include/r_debug.h | 6 +- libr/include/r_reg.h | 5 +- libr/include/r_types.h | 108 +++++----- libr/include/r_types_base.h | 32 +++ libr/include/r_util/r_bitmap.h | 2 +- libr/include/r_util/r_str.h | 2 +- libr/include/r_util/r_sys.h | 27 --- libr/io/p/io_ptrace.c | 5 +- libr/main/rafind2.c | 2 +- libr/main/ragg2.c | 4 +- libr/main/rasm2.c | 4 +- libr/util/str.c | 21 +- libr/util/sys.c | 1 + shlr/winkd/winkd.c | 2 +- test/unit/test_egg.c | 2 +- 62 files changed, 446 insertions(+), 479 deletions(-) diff --git a/binr/r2r/load.c b/binr/r2r/load.c index c741dc83dfd7b..de0e50a9a0859 100644 --- a/binr/r2r/load.c +++ b/binr/r2r/load.c @@ -594,6 +594,7 @@ static bool database_load(R2RTestDatabase *db, const char *path, int depth) { return false; } if (r_file_is_directory (path)) { + const char *archos = getarchos (); RList *dir = r_sys_dir (path); if (!dir) { return false; @@ -619,8 +620,8 @@ static bool database_load(R2RTestDatabase *db, const char *path, int depth) { continue; } bool is_archos_folder = !strcmp (path, "archos") || r_str_endswith (path, R_SYS_DIR"archos"); - if (is_archos_folder && (skip_archos || strcmp (subname, R2R_ARCH_OS))) { - R_LOG_ERROR ("Skipping %s"R_SYS_DIR"%s because it does not match the current platform \"%s\"", path, subname, R2R_ARCH_OS); + if (is_archos_folder && (skip_archos || strcmp (subname, archos))) { + R_LOG_ERROR ("Skipping %s"R_SYS_DIR"%s because it does not match the current platform \"%s\"", path, subname, archos); continue; } r_strbuf_setf (&subpath, "%s%s%s", path, R_SYS_DIR, subname); diff --git a/binr/r2r/r2r.c b/binr/r2r/r2r.c index c24157a65f8e5..e0706ce8a6b60 100644 --- a/binr/r2r/r2r.c +++ b/binr/r2r/r2r.c @@ -44,6 +44,15 @@ static void interact_break(R2RTestResultInfo *result, RPVector *fixup_results); static void interact_commands(R2RTestResultInfo *result, RPVector *fixup_results); static void interact_diffchar(R2RTestResultInfo *result); +R_IPI const char *getarchos(void) { + if (R_SYS_BITS_CHECK (R_SYS_BITS, 64)) { + return R_SYS_OS "-"R_SYS_ARCH "_64"; + } else if (R_SYS_BITS_CHECK (R_SYS_BITS, 32)) { + return R_SYS_OS "-"R_SYS_ARCH "_32"; + } + return R_SYS_OS "-"R_SYS_ARCH; +} + static void parse_skip(const char *arg) { if (strstr (arg, "arch")) { r_sys_setenv ("R2R_SKIP_ARCHOS", "1"); @@ -104,7 +113,7 @@ static int help(bool verbose, int workers_count) { helpvars (workers_count); printf ("\n" "Supported test types: @asm @json @unit @fuzz @arch @cmd\n" - "OS/Arch for archos tests: "R2R_ARCH_OS"\n"); + "OS/Arch for archos tests: %s\n", getarchos ()); } return 1; } diff --git a/binr/r2r/r2r.h b/binr/r2r/r2r.h index ebbf4e1e2db68..a425c7188b54d 100644 --- a/binr/r2r/r2r.h +++ b/binr/r2r/r2r.h @@ -5,14 +5,6 @@ #include -#if R_SYS_BITS & R_SYS_BITS_64 -#define R2R_ARCH_OS R_SYS_OS "-"R_SYS_ARCH "_64" -#elif R_SYS_BITS & R_SYS_BITS_32 -#define R2R_ARCH_OS R_SYS_OS "-"R_SYS_ARCH "_32" -#elif -#define R2R_ARCH_OS R_SYS_OS "-"R_SYS_ARCH -#endif - typedef struct r2r_cmd_test_string_record { char *value; ut64 line_begin; // inclusive @@ -217,5 +209,6 @@ R_API char *r2r_test_name(R2RTest *test); R_API bool r2r_test_broken(R2RTest *test); R_API R2RTestResultInfo *r2r_run_test(R2RRunConfig *config, R2RTest *test); R_API void r2r_test_result_info_free(R2RTestResultInfo *result); +R_IPI const char *getarchos(void); #endif //RADARE2_R2R_H diff --git a/libr/arch/arch_config.c b/libr/arch/arch_config.c index 5fec550503c8d..d4ca84d72305d 100644 --- a/libr/arch/arch_config.c +++ b/libr/arch/arch_config.c @@ -88,13 +88,7 @@ R_API RArchConfig *r_arch_config_new(void) { return NULL; } ac->arch = strdup (R_SYS_ARCH); -#if R_SYS_BITS == R_SYS_BITS_32 - ac->bits = 32; -#elif R_SYS_BITS == R_SYS_BITS_64 - ac->bits = 64; -#else - ac->bits = 64; -#endif + ac->bits = R_SYS_BITS_CHECK (R_SYS_BITS, 64)? 64: 32; ac->bitshift = 0; ac->syntax = R_ARCH_SYNTAX_INTEL; r_ref_init (ac, &_ac_free); diff --git a/libr/arch/p/arm/plugin.c b/libr/arch/p/arm/plugin.c index 61c339b7a923a..70f712192ba5b 100644 --- a/libr/arch/p/arm/plugin.c +++ b/libr/arch/p/arm/plugin.c @@ -7,20 +7,13 @@ static bool encode(RArchSession *s, RAnalOp *op, ut32 mask) { int bits = s->config->bits; - if (bits & R_SYS_BITS_32) { - bits = 32; - } else if (bits & R_SYS_BITS_16) { - bits = 16; - } -#if 0 - if (s->config->bits & R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (bits, 64)) { bits = 64; - } else if (s->config->bits & R_SYS_BITS_32) { + } else if (R_SYS_BITS_CHECK (bits, 32)) { bits = 32; - } else if (s->config->bits & R_SYS_BITS_16) { + } else if (R_SYS_BITS_CHECK (bits, 16)) { bits = 16; } -#endif const bool is_thumb = (bits == 16); int opsize; ut32 opcode = UT32_MAX; @@ -51,19 +44,11 @@ static bool encode(RArchSession *s, RAnalOp *op, ut32 mask) { r_write_be32 (opbuf, opcode); } } else if (opsize == 2) { - if (be) { - r_write_le16 (opbuf, opcode & UT16_MAX); - } else { - r_write_be16 (opbuf, opcode & UT16_MAX); - } + r_write_ble16 (opbuf, opcode & UT16_MAX, !be); } } else { opsize = 4; - if (be) { - r_write_le32 (opbuf, opcode); - } else { - r_write_be32 (opbuf, opcode); - } + r_write_ble32 (opbuf, opcode, !be); } r_anal_op_set_bytes (op, op->addr, opbuf, opsize); // r_strbuf_setbin (&op->buf, opbuf, opsize); diff --git a/libr/bin/format/le/le.c b/libr/bin/format/le/le.c index fccd5ad1f9f31..0e33c80d9119c 100644 --- a/libr/bin/format/le/le.c +++ b/libr/bin/format/le/le.c @@ -381,9 +381,9 @@ R_IPI RList *r_bin_le_get_sections(RBinLEObj *bin) { sec->perm |= R_PERM_X; } if (entry->flags & O_BIG_BIT) { - sec->bits = R_SYS_BITS_32; + sec->bits = R_SYS_BITS_PACK (32); } else { - sec->bits = R_SYS_BITS_16; + sec->bits = R_SYS_BITS_PACK (16); } sec->is_data = (entry->flags & O_RESOURCE) || !(sec->perm & R_PERM_X); if (!entry->page_tbl_entries) { diff --git a/libr/bin/format/ne/ne.c b/libr/bin/format/ne/ne.c index e5edc42dcd63f..6eb4038277f89 100644 --- a/libr/bin/format/ne/ne.c +++ b/libr/bin/format/ne/ne.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2019-2022 - GustavoLCR */ +/* radare - LGPL - Copyright 2019-2024 - GustavoLCR */ #include "ne.h" @@ -89,7 +89,7 @@ RList *r_bin_ne_get_segments(r_bin_ne_obj_t *bin) { NE_image_segment_entry *se = &bin->segment_entries[i]; bs->size = se->length; bs->vsize = se->minAllocSz ? se->minAllocSz : 64000; - bs->bits = R_SYS_BITS_16; + bs->bits = R_SYS_BITS_PACK (16); bs->is_data = se->flags & IS_DATA; bs->perm = __translate_perms (se->flags); bs->paddr = (ut64)se->offset * bin->alignment; diff --git a/libr/bin/format/pe/dotnet.c b/libr/bin/format/pe/dotnet.c index c348da255a2f3..b97f0994b391b 100644 --- a/libr/bin/format/pe/dotnet.c +++ b/libr/bin/format/pe/dotnet.c @@ -1,5 +1,5 @@ /* -Forked by pancake in 2017 +Forked by pancake in 2017-2024 Copyright (c) 2015. The YARA Authors. All Rights Reserved. @@ -23,18 +23,14 @@ limitations under the License. #include #include #include +#include +#include -#define ULONGLONG ut64 -#define DWORD uint32_t -#define WORD uint16_t -#define BYTE uint8_t +typedef struct R_IMAGE_DATA_DIRECTORY { + ut32 VirtualAddress; + ut32 Size; +} R_IMAGE_DATA_DIRECTORY, *R_PIMAGE_DATA_DIRECTORY; -typedef struct _IMAGE_DATA_DIRECTORY { - DWORD VirtualAddress; - DWORD Size; -} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY; - -#include "r_types.h" #include "pe_specs.h" #include "dotnet.h" @@ -65,18 +61,18 @@ typedef struct _PE { void* object; } PE; -PIMAGE_DATA_DIRECTORY pe_get_directory_entry( PE* pe, int entry) { +R_PIMAGE_DATA_DIRECTORY pe_get_directory_entry( PE* pe, int entry) { #if 0 - PIMAGE_DATA_DIRECTORY result = IS_64BITS_PE(pe) + R_PIMAGE_DATA_DIRECTORY result = IS_64BITS_PE(pe) ? &pe->header64->OptionalHeader.DataDirectory[entry] : &pe->header->OptionalHeader.DataDirectory[entry]; #else - PIMAGE_DATA_DIRECTORY result = {0}; + R_PIMAGE_DATA_DIRECTORY result = {0}; #endif return result; } -char* pe_get_dotnet_string( PE* pe, const uint8_t* string_offset, DWORD string_index) { +char* pe_get_dotnet_string( PE* pe, const uint8_t* string_offset, ut32 string_index) { // Start of string must be within boundary if (!(string_offset + string_index >= pe->data && string_offset + string_index < pe->data + pe->data_size)) { @@ -87,7 +83,7 @@ char* pe_get_dotnet_string( PE* pe, const uint8_t* string_offset, DWORD string_i // Search for a NULL terminator from start of string, up to remaining. char *start = (char*) (string_offset + string_index); - char *eos = (char*) memmem((void*) start, remaining, "\0", 1); + char *eos = (char*) r_mem_mem((void*) start, remaining, (void*)"\0", 1); return eos? start: NULL; } @@ -119,7 +115,7 @@ void dotnet_parse_guid( PE* pe, ut64 metadata_root, PSTREAM_HEADER guid_header) int i = 0; const uint8_t* guid_offset = pe->data + metadata_root + guid_header->Offset; - DWORD guid_size = guid_header->Size; + ut32 guid_size = guid_header->Size; // Parse GUIDs if we have them. // GUIDs are 16 bytes each. @@ -169,7 +165,7 @@ BLOB_PARSE_RESULT dotnet_parse_blob_entry( PE* pe, const uint8_t* offset) { } if ((*offset & 0x80) == 0x00) { - result.length = (DWORD) *offset; + result.length = (ut32) *offset; result.size = 1; } else if ((*offset & 0xC0) == 0x80) { // Make sure we have one more byte. @@ -247,7 +243,7 @@ STREAMS dotnet_parse_stream_headers( PE* pe, ut64 offset, ut64 metadata_root, - DWORD num_streams) + ut32 num_streams) { PSTREAM_HEADER stream_header; STREAMS headers; @@ -271,7 +267,7 @@ STREAMS dotnet_parse_stream_headers( if (!fits_in_pe(pe, start, DOTNET_STREAM_NAME_SIZE)) break; - eos = (char*) memmem((void*) start, DOTNET_STREAM_NAME_SIZE, "\0", 1); + eos = (char*) r_mem_mem((void*) start, DOTNET_STREAM_NAME_SIZE, (void*)"\0", 1); if (eos == NULL) break; @@ -344,7 +340,7 @@ void dotnet_parse_tilde_2( PMODULEREF_TABLE moduleref_table; PCUSTOMATTRIBUTE_TABLE customattribute_table; PCONSTANT_TABLE constant_table; - DWORD resource_size, implementation; + ut32 resource_size, implementation; char *name; char typelib[MAX_TYPELIB_SIZE + 1]; @@ -390,11 +386,11 @@ void dotnet_parse_tilde_2( uint8_t* typeref_row = NULL; uint8_t* memberref_row = NULL; - DWORD type_index; - DWORD class_index; + ut32 type_index; + ut32 class_index; BLOB_PARSE_RESULT blob_result; - DWORD blob_index; - DWORD blob_length; + ut32 blob_index; + ut32 blob_length; // These are used to determine the size of coded indexes, which are the // dynamically sized columns for some tables. The coded indexes are @@ -584,10 +580,10 @@ void dotnet_parse_tilde_2( blob_offset = ((uint8_t*) constant_table) + 2 + index_size; if (index_sizes.blob == 4) - blob_index = *(DWORD*) blob_offset; + blob_index = *(ut32*) blob_offset; else // Cast the value (index into blob table) to a 32bit value. - blob_index = (DWORD) (*(WORD*) blob_offset); + blob_index = (ut32) (*(ut16*) blob_offset); // Everything checks out. Make sure the index into the blob field // is valid (non-null and within range). @@ -687,7 +683,7 @@ void dotnet_parse_tilde_2( // Low 5 bits tell us what this is an index into. Remaining bits // tell us the index value. // Parent must be an index into the Assembly (0x0E) table. - if ((*(DWORD*) customattribute_table & 0x1F) != 0x0E) + if ((*(ut32*) customattribute_table & 0x1F) != 0x0E) { row_ptr += row_size; continue; @@ -698,7 +694,7 @@ void dotnet_parse_tilde_2( // Low 5 bits tell us what this is an index into. Remaining bits // tell us the index value. // Parent must be an index into the Assembly (0x0E) table. - if ((*(WORD*) customattribute_table & 0x1F) != 0x0E) + if ((*(ut16*) customattribute_table & 0x1F) != 0x0E) { row_ptr += row_size; continue; @@ -714,27 +710,27 @@ void dotnet_parse_tilde_2( // Low 3 bits tell us what this is an index into. Remaining bits // tell us the index value. Only values 2 and 3 are defined. // Type must be an index into the MemberRef table. - if ((*(DWORD*) customattribute_table & 0x07) != 0x03) + if ((*(ut32*) customattribute_table & 0x07) != 0x03) { row_ptr += row_size; continue; } - type_index = *(DWORD*) customattribute_table >> 3; + type_index = *(ut32*) customattribute_table >> 3; } else { // Low 3 bits tell us what this is an index into. Remaining bits // tell us the index value. Only values 2 and 3 are defined. // Type must be an index into the MemberRef table. - if ((*(WORD*) customattribute_table & 0x07) != 0x03) + if ((*(ut16*) customattribute_table & 0x07) != 0x03) { row_ptr += row_size; continue; } // Cast the index to a 32bit value. - type_index = (DWORD) ((*(WORD*) customattribute_table >> 3)); + type_index = (ut32) ((*(ut16*) customattribute_table >> 3)); } if (type_index > 0) @@ -748,27 +744,27 @@ void dotnet_parse_tilde_2( // Low 3 bits tell us what this is an index into. Remaining bits // tell us the index value. Class must be an index into the // TypeRef table. - if ((*(DWORD*) memberref_row & 0x07) != 0x01) + if ((*(ut32*) memberref_row & 0x07) != 0x01) { row_ptr += row_size; continue; } - class_index = *(DWORD*) memberref_row >> 3; + class_index = *(ut32*) memberref_row >> 3; } else { // Low 3 bits tell us what this is an index into. Remaining bits // tell us the index value. Class must be an index into the // TypeRef table. - if ((*(WORD*) memberref_row & 0x07) != 0x01) + if ((*(ut16*) memberref_row & 0x07) != 0x01) { row_ptr += row_size; continue; } // Cast the index to a 32bit value. - class_index = (DWORD) (*(WORD*) memberref_row >> 3); + class_index = (ut32) (*(ut16*) memberref_row >> 3); } if (class_index > 0) @@ -793,12 +789,12 @@ void dotnet_parse_tilde_2( if (index_sizes.string == 4) { name = pe_get_dotnet_string( - pe, string_offset, *(DWORD*) typeref_row); + pe, string_offset, *(ut32*) typeref_row); } else { name = pe_get_dotnet_string( - pe, string_offset, *(WORD*) typeref_row); + pe, string_offset, *(ut16*) typeref_row); } if (name && strncmp (name, "GuidAttribute", 13) != 0) @@ -812,10 +808,10 @@ void dotnet_parse_tilde_2( (row_ptr + index_size + index_size2); if (index_sizes.blob == 4) - blob_index = *(DWORD*) customattribute_table; + blob_index = *(ut32*) customattribute_table; else // Cast the value (index into blob table) to a 32bit value. - blob_index = (DWORD) (*(WORD*) customattribute_table); + blob_index = (ut32) (*(ut16*) customattribute_table); // Everything checks out. Make sure the index into the blob field // is valid (non-null and within range). @@ -850,7 +846,7 @@ void dotnet_parse_tilde_2( } // Custom attributes MUST have a 16 bit prolog of 0x0001 - if (*(WORD*) blob_offset != 0x0001) + if (*(ut16*) blob_offset != 0x0001) { row_ptr += row_size; continue; @@ -1074,14 +1070,14 @@ void dotnet_parse_tilde_2( name = pe_get_dotnet_string( pe, string_offset, - *(DWORD*) ( + *(ut32*) ( row_ptr + 4 + 2 + 2 + 2 + 2 + 4 + index_sizes.blob)); else name = pe_get_dotnet_string( pe, string_offset, - *(WORD*) ( + *(ut16*) ( row_ptr + 4 + 2 + 2 + 2 + 2 + 4 + index_sizes.blob)); @@ -1094,7 +1090,7 @@ void dotnet_parse_tilde_2( name = pe_get_dotnet_string( pe, string_offset, - *(DWORD*) ( + *(ut32*) ( row_ptr + 4 + 2 + 2 + 2 + 2 + 4 + index_sizes.blob + index_sizes.string)); @@ -1104,7 +1100,7 @@ void dotnet_parse_tilde_2( name = pe_get_dotnet_string( pe, string_offset, - *(WORD*) ( + *(ut16*) ( row_ptr + 4 + 2 + 2 + 2 + 2 + 4 + index_sizes.blob + index_sizes.string)); @@ -1179,11 +1175,11 @@ void dotnet_parse_tilde_2( if (index_sizes.string == 4) name = pe_get_dotnet_string(pe, string_offset, - *(DWORD*) (row_ptr + 2 + 2 + 2 + 2 + 4 + index_sizes.blob)); + *(ut32*) (row_ptr + 2 + 2 + 2 + 2 + 4 + index_sizes.blob)); else name = pe_get_dotnet_string(pe, string_offset, - *(WORD*) (row_ptr + 2 + 2 + 2 + 2 + 4 + index_sizes.blob)); + *(ut16*) (row_ptr + 2 + 2 + 2 + 2 + 4 + index_sizes.blob)); if (name) set_string(name, pe->object, "assembly_refs[%i].name", i); @@ -1233,7 +1229,7 @@ void dotnet_parse_tilde_2( // it would give an inaccurate count in that case. counter = 0; row_ptr = table_offset; - // First DWORD is the offset. + // First ut32 is the offset. for (i = 0; i < num_rows; i++) { if (!fits_in_pe(pe, row_ptr, row_size)) @@ -1246,9 +1242,9 @@ void dotnet_parse_tilde_2( // Can't use manifestresource_table here because the Name and // Implementation fields are variable size. if (index_size == 4) - implementation = *(DWORD*) (row_ptr + 4 + 4 + index_sizes.string); + implementation = *(ut32*) (row_ptr + 4 + 4 + index_sizes.string); else - implementation = *(WORD*) (row_ptr + 4 + 4 + index_sizes.string); + implementation = *(ut16*) (row_ptr + 4 + 4 + index_sizes.string); if (implementation != 0) { @@ -1259,13 +1255,13 @@ void dotnet_parse_tilde_2( if (!fits_in_pe( pe, pe->data + resource_base + resource_offset, - sizeof (DWORD))) + sizeof (ut32))) { row_ptr += row_size; continue; } - resource_size = *(DWORD*)(pe->data + resource_base + resource_offset); + resource_size = *(ut32*)(pe->data + resource_base + resource_offset); if (!fits_in_pe( pe, pe->data + resource_base + @@ -1522,13 +1518,13 @@ void dotnet_parse_tilde( #endif void dotnet_parse_com(PE* pe, ut64 baddr) { - PIMAGE_DATA_DIRECTORY directory; + R_PIMAGE_DATA_DIRECTORY directory; PCLI_HEADER cli_header; PNET_METADATA metadata; ut64 metadata_root; char* end; STREAMS headers; - WORD num_streams; + ut16 num_streams; directory = pe_get_directory_entry (pe, PE_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR); st64 offset = pe_rva_to_offset (pe, directory->VirtualAddress); @@ -1542,13 +1538,15 @@ void dotnet_parse_com(PE* pe, ut64 baddr) { offset = metadata_root = pe_rva_to_offset( pe, cli_header->MetaData.VirtualAddress); - if (!struct_fits_in_pe(pe, pe->data + offset, NET_METADATA)) + if (!struct_fits_in_pe(pe, pe->data + offset, NET_METADATA)) { return; + } metadata = (PNET_METADATA) (pe->data + offset); - if (metadata->Magic != NET_METADATA_MAGIC) + if (metadata->Magic != NET_METADATA_MAGIC) { return; + } // Version length must be between 1 and 255, and be a multiple of 4. // Also make sure it fits in pe. @@ -1563,12 +1561,12 @@ void dotnet_parse_com(PE* pe, ut64 baddr) { // The length includes the NULL terminator and is rounded up to a multiple of // 4. We need to exclude the terminator and the padding, so search for the // first NULL byte. - end = (char*) memmem((void*) metadata->Version, metadata->Length, "\0", 1); - if (end) + end = (char*) r_mem_mem((void*) metadata->Version, metadata->Length, (void*)"\0", 1); + if (end) { set_sized_string(metadata->Version, - (end - metadata->Version), - pe->object, - "version"); + (end - metadata->Version), + pe->object, "version"); + } // The metadata structure has some variable length records after the version. // We must manually parse things from here on out. @@ -1580,7 +1578,7 @@ void dotnet_parse_com(PE* pe, ut64 baddr) { if (!fits_in_pe(pe, pe->data + offset, 2)) return; - num_streams = (WORD) *(pe->data + offset); + num_streams = (ut16) *(pe->data + offset); offset += 2; headers = dotnet_parse_stream_headers(pe, offset, metadata_root, num_streams); @@ -1603,6 +1601,6 @@ void dotnet_parse_com(PE* pe, ut64 baddr) { // entrypoint void dotnet_parse(const ut8 *buf, int size, ut64 baddr) { - PE pe = { buf, (DWORD)size, NULL}; + PE pe = { buf, (ut32)size, NULL}; dotnet_parse_com (&pe, baddr); } diff --git a/libr/bin/format/pe/dotnet.h b/libr/bin/format/pe/dotnet.h index 405bfcb3f8af9..7932a736c23ce 100644 --- a/libr/bin/format/pe/dotnet.h +++ b/libr/bin/format/pe/dotnet.h @@ -20,18 +20,18 @@ // ECMA-335 Section II.25.3.3 // typedef struct _CLI_HEADER { - DWORD Size; // Called "Cb" in documentation. - WORD MajorRuntimeVersion; - WORD MinorRuntimeVersion; - IMAGE_DATA_DIRECTORY MetaData; - DWORD Flags; - DWORD EntryPointToken; - IMAGE_DATA_DIRECTORY Resources; - IMAGE_DATA_DIRECTORY StrongNameSignature; - ULONGLONG CodeManagerTable; - IMAGE_DATA_DIRECTORY VTableFixups; - ULONGLONG ExportAddressTableJumps; - ULONGLONG ManagedNativeHeader; + ut32 Size; // Called "Cb" in documentation. + ut16 MajorRuntimeVersion; + ut16 MinorRuntimeVersion; + R_IMAGE_DATA_DIRECTORY MetaData; + ut32 Flags; + ut32 EntryPointToken; + R_IMAGE_DATA_DIRECTORY Resources; + R_IMAGE_DATA_DIRECTORY StrongNameSignature; + ut64 CodeManagerTable; + R_IMAGE_DATA_DIRECTORY VTableFixups; + ut64 ExportAddressTableJumps; + ut64 ManagedNativeHeader; } CLI_HEADER, *PCLI_HEADER; #define NET_METADATA_MAGIC 0x424a5342 @@ -43,11 +43,11 @@ typedef struct _CLI_HEADER { // Note: This is only part of the struct, as the rest of it is variable length. // typedef struct _NET_METADATA { - DWORD Magic; - WORD MajorVersion; - WORD MinorVersion; - DWORD Reserved; - DWORD Length; + ut32 Magic; + ut16 MajorVersion; + ut16 MinorVersion; + ut32 Reserved; + ut32 Length; char Version[0]; } NET_METADATA, *PNET_METADATA; @@ -58,8 +58,8 @@ typedef struct _NET_METADATA { // ECMA-335 Section II.24.2.2 // typedef struct _STREAM_HEADER { - DWORD Offset; - DWORD Size; + ut32 Offset; + ut32 Size; char Name[0]; } STREAM_HEADER, *PSTREAM_HEADER; @@ -69,13 +69,13 @@ typedef struct _STREAM_HEADER { // ECMA-335 Section II.24.2.6 // typedef struct _TILDE_HEADER { - DWORD Reserved1; - BYTE MajorVersion; - BYTE MinorVersion; - BYTE HeapSizes; - BYTE Reserved2; - ULONGLONG Valid; - ULONGLONG Sorted; + ut32 Reserved1; + ut8 MajorVersion; + ut8 MinorVersion; + ut8 HeapSizes; + ut8 Reserved2; + ut64 Valid; + ut64 Sorted; } TILDE_HEADER, *PTILDE_HEADER; // These are the bit positions in Valid which will be set if the table @@ -152,22 +152,22 @@ typedef struct _TILDE_HEADER { // ECMA-335 Section II.22.30 // typedef struct _MODULE_TABLE { - WORD Generation; + ut16 Generation; union { - WORD Name_Short; - DWORD Name_Long; + ut16 Name_Short; + ut32 Name_Long; } Name; union { - WORD Mvid_Short; - DWORD Mvid_Long; + ut16 Mvid_Short; + ut32 Mvid_Long; } Mvid; union { - WORD EncId_Short; - DWORD EncId_Long; + ut16 EncId_Short; + ut32 EncId_Long; } EncId; union { - WORD EncBaseId_Short; - DWORD EncBaseId_Long; + ut16 EncBaseId_Short; + ut32 EncBaseId_Long; } EncBaseId; } MODULE_TABLE, *PMODULE_TABLE; @@ -176,19 +176,19 @@ typedef struct _MODULE_TABLE { // ECMA-335 Section II.22.2 // typedef struct _ASSEMBLY_TABLE { - DWORD HashAlgId; - WORD MajorVersion; - WORD MinorVersion; - WORD BuildNumber; - WORD RevisionNumber; - DWORD Flags; + ut32 HashAlgId; + ut16 MajorVersion; + ut16 MinorVersion; + ut16 BuildNumber; + ut16 RevisionNumber; + ut32 Flags; union { - WORD PublicKey_Short; - DWORD PublicKey_Long; + ut16 PublicKey_Short; + ut32 PublicKey_Long; } PublicKey; union { - WORD Name_Short; - DWORD Name_Long; + ut16 Name_Short; + ut32 Name_Long; } Name; } ASSEMBLY_TABLE, *PASSEMBLY_TABLE; @@ -198,18 +198,18 @@ typedef struct _ASSEMBLY_TABLE { // ECMA-335 Section II.22.5 // typedef struct _ASSEMBLYREF_TABLE { - WORD MajorVersion; - WORD MinorVersion; - WORD BuildNumber; - WORD RevisionNumber; - DWORD Flags; + ut16 MajorVersion; + ut16 MinorVersion; + ut16 BuildNumber; + ut16 RevisionNumber; + ut32 Flags; union { - WORD PublicKeyOrToken_Short; - DWORD PublicKeyOrToken_Long; + ut16 PublicKeyOrToken_Short; + ut32 PublicKeyOrToken_Long; } PublicKeyOrToken; union { - WORD Name_Short; - DWORD Name_Long; + ut16 Name_Short; + ut32 Name_Long; } Name; } ASSEMBLYREF_TABLE, *PASSEMBLYREF_TABLE; @@ -219,15 +219,15 @@ typedef struct _ASSEMBLYREF_TABLE { // ECMA-335 Section II.22.24 // typedef struct _MANIFESTRESOURCE_TABLE { - DWORD Offset; - DWORD Flags; + ut32 Offset; + ut32 Flags; union { - WORD Name_Short; - DWORD Name_Long; + ut16 Name_Short; + ut32 Name_Long; } Name; union { - WORD Implementation_Short; - DWORD Implementation_Long; + ut16 Implementation_Short; + ut32 Implementation_Long; } Implementation; } MANIFESTRESOURCE_TABLE, *PMANIFESTRESOURCE_TABLE; @@ -239,8 +239,8 @@ typedef struct _MANIFESTRESOURCE_TABLE { // typedef struct _MODULEREF_TABLE { union { - WORD Name_Short; - DWORD Name_Long; + ut16 Name_Short; + ut32 Name_Long; } Name; } MODULEREF_TABLE, *PMODULEREF_TABLE; @@ -251,16 +251,16 @@ typedef struct _MODULEREF_TABLE { // typedef struct _CUSTOMATTRIBUTE_TABLE { union { - WORD Parent_Short; - DWORD Parent_Long; + ut16 Parent_Short; + ut32 Parent_Long; } Parent; union { - WORD Type_Short; - DWORD Type_Long; + ut16 Type_Short; + ut32 Type_Long; } Type; union { - WORD Value_Short; - DWORD Value_Long; + ut16 Value_Short; + ut32 Value_Long; } Value; } CUSTOMATTRIBUTE_TABLE, *PCUSTOMATTRIBUTE_TABLE; @@ -270,14 +270,14 @@ typedef struct _CUSTOMATTRIBUTE_TABLE { // ECMA-335 Section II.22.9 // typedef struct _CONSTANT_TABLE { - WORD Type; + ut16 Type; union { - WORD Parent_Short; - DWORD Parent_Long; + ut16 Parent_Short; + ut32 Parent_Long; } Parent; union { - WORD Value_Short; - DWORD Value_Long; + ut16 Value_Short; + ut32 Value_Long; } Value; } CONSTANT_TABLE, *PCONSTANT_TABLE; @@ -296,34 +296,34 @@ typedef struct _STREAMS { // ECMA-335 Section II.24.2.4 typedef struct _BLOB_PARSE_RESULT { uint8_t size; // Number of bytes parsed. This is the new offset. - DWORD length; // Value of the bytes parsed. This is the blob length. + ut32 length; // Value of the bytes parsed. This is the blob length. } BLOB_PARSE_RESULT, *PBLOB_PARSE_RESULT; // Used to store the number of rows of each table. typedef struct _ROWS { - uint32_t module; - uint32_t moduleref; - uint32_t assemblyref; - uint32_t typeref; - uint32_t methoddef; - uint32_t memberref; - uint32_t typedef_; - uint32_t typespec; - uint32_t field; - uint32_t param; - uint32_t property; - uint32_t interfaceimpl; - uint32_t event; - uint32_t standalonesig; - uint32_t assembly; - uint32_t file; - uint32_t exportedtype; - uint32_t manifestresource; - uint32_t genericparam; - uint32_t genericparamconstraint; - uint32_t methodspec; - uint32_t assemblyrefprocessor; + ut32 module; + ut32 moduleref; + ut32 assemblyref; + ut32 typeref; + ut32 methoddef; + ut32 memberref; + ut32 typedef_; + ut32 typespec; + ut32 field; + ut32 param; + ut32 property; + ut32 interfaceimpl; + ut32 event; + ut32 standalonesig; + ut32 assembly; + ut32 file; + ut32 exportedtype; + ut32 manifestresource; + ut32 genericparam; + ut32 genericparamconstraint; + ut32 methodspec; + ut32 assemblyrefprocessor; } ROWS, *PROWS; diff --git a/libr/bin/p/bin_te.c b/libr/bin/p/bin_te.c index 1ab46400d07e1..9991882f44cd7 100644 --- a/libr/bin/p/bin_te.c +++ b/libr/bin/p/bin_te.c @@ -105,7 +105,7 @@ static RList *sections(RBinFile *bf) { /* All TE files have _TEXT_RE section, which is 16-bit, because of * CPU start in this mode */ if (!strncmp (ptr->name, "_TEXT_RE", 8)) { - ptr->bits = R_SYS_BITS_16; + ptr->bits = R_SYS_BITS_PACK (16); } r_list_append (ret, ptr); } diff --git a/libr/bin/p/pe.mk b/libr/bin/p/pe.mk index 4eef1e0a86f45..a46cdfeab30f4 100644 --- a/libr/bin/p/pe.mk +++ b/libr/bin/p/pe.mk @@ -1,5 +1,5 @@ OBJ_PE=bin_pe.o bin_write_pe.o ../format/pe/pe.o ../format/pe/pe_write.o -#OBJ_PE+=../format/pe/dotnet.o +OBJ_PE+=../format/pe/dotnet.o STATIC_OBJ+=${OBJ_PE} TARGET_PE=bin_pe.${EXT_SO} diff --git a/libr/core/cbin.c b/libr/core/cbin.c index 9089b02cbefe5..32855043c36a9 100644 --- a/libr/core/cbin.c +++ b/libr/core/cbin.c @@ -4850,7 +4850,7 @@ R_API bool r_core_bin_info(RCore *core, int action, PJ *pj, int mode, int va, RC return ret; } -R_API bool r_core_bin_set_arch_bits(RCore *r, const char *name, const char *_arch, ut16 bits) { +R_API bool r_core_bin_set_arch_bits(RCore *r, const char *name, const char *_arch, RSysBits bits) { int fd = r_io_fd_get_current (r->io); RIODesc *desc = r_io_desc_get (r->io, fd); if (!name) { diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c index 9b822e793a8b3..00f6bdf91c616 100644 --- a/libr/core/cconfig.c +++ b/libr/core/cconfig.c @@ -769,7 +769,6 @@ static bool cb_asmarch(void *user, void *data) { r_config_set_i (core->config, "asm.bits", bits); } - //r_debug_set_arch (core->dbg, r_sys_arch_id (node->value), bits); r_debug_set_arch (core->dbg, node->value, bits); if (!r_config_set (core->config, "anal.arch", node->value)) { char *p, *s = strdup (node->value); @@ -878,9 +877,9 @@ static bool cb_asmbits(void *user, void *data) { // XXX. that should depend on the plugin, not the host os #if R2__WINDOWS__ #if !defined(_WIN64) - core->dbg->bits = R_SYS_BITS_32; + core->dbg->bits = R_SYS_BITS_PACK (32); #else - core->dbg->bits = R_SYS_BITS_64; + core->dbg->bits = R_SYS_BITS_PACK (64); #endif #endif char *rp = plugin->reg_profile (core->dbg); @@ -3655,11 +3654,7 @@ R_API int r_core_config_init(RCore *core) { SETDESC (n, "select the instruction decoder to use"); update_archdecoder_options (core, n); r_config_set_getter (cfg, "arch.decoder", (RConfigCallback)cb_archdecoder_getter); -#if R_SYS_BITS == R_SYS_BITS_64 - SETICB ("arch.bits", 64, &cb_archbits, "word size in bits at arch decoder"); -#else - SETICB ("arch.bits", 32, &cb_archbits, "word size in bits at arch decoder"); -#endif + SETICB ("arch.bits", R_SYS_BITS, &cb_archbits, "word size in bits at arch decoder"); r_config_set_getter (cfg, "arch.bits", (RConfigCallback)cb_archbits_getter); SETCB ("arch.platform", "", &cb_arch_platform, "define arch platform to use"); n = NODECB ("arch.endian", R_SYS_ENDIAN? "big": "little", &cb_archendian); @@ -3883,11 +3878,7 @@ R_API int r_core_config_init(RCore *core) { SETOPTIONS (n, "att", "intel", "masm", "jz", "regnum", NULL); SETI ("asm.nbytes", 6, "number of bytes for each opcode at disassembly"); SETBPREF ("asm.bytes.space", "false", "separate hexadecimal bytes with a whitespace"); -#if R_SYS_BITS == R_SYS_BITS_64 - SETICB ("asm.bits", 64, &cb_asmbits, "word size in bits at assembler"); -#else - SETICB ("asm.bits", 32, &cb_asmbits, "word size in bits at assembler"); -#endif + SETICB ("asm.bits", R_SYS_BITS, &cb_asmbits, "word size in bits at assembler"); n = r_config_node_get(cfg, "asm.bits"); update_asmbits_options (core, n); SETBPREF ("asm.functions", "true", "show functions in disassembly"); diff --git a/libr/core/cfile.c b/libr/core/cfile.c index eec54273019df..a257bf08c54b1 100644 --- a/libr/core/cfile.c +++ b/libr/core/cfile.c @@ -196,6 +196,8 @@ R_API bool r_core_file_reopen(RCore *core, const char *args, int perm, int loadb } r_core_seek (core, origoff, true); if (isdebug) { + r_core_cmd0 (core, "arp>$_"); // Fixes a bug where registers are not synced wtf + // r_debug_reg_sync (core->dbg, R_REG_TYPE_GPR, false); // does nothing r_core_cmd0 (core, ".dm*"); r_core_cmd0 (core, ".dr*"); r_core_cmd_call (core, "sr PC"); diff --git a/libr/core/cio.c b/libr/core/cio.c index ead0ba3ff870f..120c45efb9803 100644 --- a/libr/core/cio.c +++ b/libr/core/cio.c @@ -347,8 +347,6 @@ R_API int r_core_write_op(RCore *core, const char *arg, char op) { return ret; } -// Get address-specific bits and arch at a certain address. -// If there are no specific infos (i.e. asm.bits and asm.arch should apply), the bits and arch will be 0 or NULL respectively! R_API void r_core_arch_bits_at(RCore *core, ut64 addr, R_OUT R_NULLABLE int *bits, R_OUT R_BORROW R_NULLABLE const char **arch) { int bitsval = 0; const char *archval = NULL; @@ -361,12 +359,12 @@ R_API void r_core_arch_bits_at(RCore *core, ut64 addr, R_OUT R_NULLABLE int *bit } if (!core->fixedbits && s->bits) { // only enforce if there's one bits set - switch (s->bits) { - case R_SYS_BITS_16: - case R_SYS_BITS_32: - case R_SYS_BITS_64: - bitsval = s->bits * 8; - break; + if (R_SYS_BITS_CHECK (s->bits, 16)) { + bitsval = 16; + } else if (R_SYS_BITS_CHECK (s->bits, 32)) { + bitsval = 32; + } else if (R_SYS_BITS_CHECK (s->bits, 64)) { + bitsval = 64; } } } diff --git a/libr/core/cmd.c b/libr/core/cmd.c index d43a4a7549f50..e51617025ad8c 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -625,15 +625,16 @@ static int cmd_uname(void *data, const char *input) { // "uniq" } RSysInfo *si = r_sys_info (); if (si) { + const bool sysbits = R_SYS_BITS_CHECK (R_SYS_BITS, 64)? 64: 32; if (strstr (input, "-a")) { r_cons_printf ("%s %s %s-%d", si->sysname, si->release, - R_SYS_ARCH, (R_SYS_BITS & R_SYS_BITS_64)? 64: 32); + R_SYS_ARCH, sysbits); } else if (strstr (input, "-j")) { PJ *pj = r_core_pj_new (core); pj_o (pj); pj_ks (pj, "platform", si->sysname); pj_ks (pj, "arch", R_SYS_ARCH); - pj_kn (pj, "bits", (R_SYS_BITS & R_SYS_BITS_64)? 64: 32); + pj_kn (pj, "bits", sysbits); pj_end (pj); char *s = pj_drain (pj); r_cons_printf ("%s", s); @@ -641,7 +642,7 @@ static int cmd_uname(void *data, const char *input) { // "uniq" } else if (strstr (input, "-m")) { r_cons_printf ("%s", R_SYS_ARCH); } else if (strstr (input, "-b")) { - r_cons_printf ("%d", (R_SYS_BITS & R_SYS_BITS_64)? 64: 32); + r_cons_printf ("%d", sysbits); } else { r_cons_printf ("%s", si->sysname); if (strstr (input, "-r")) { diff --git a/libr/core/cmd_debug.inc.c b/libr/core/cmd_debug.inc.c index 7ef0a7ce3ef61..5be37722cadef 100644 --- a/libr/core/cmd_debug.inc.c +++ b/libr/core/cmd_debug.inc.c @@ -1153,12 +1153,12 @@ static void cmd_debug_pid(RCore *core, const char *input) { break; case 'a': // "dpa" if (input[2]) { - r_debug_attach (core->dbg, (int) r_num_math ( - core->num, input + 2)); + const int pid = (int) r_num_math (core->num, input + 2); + r_debug_attach (core->dbg, pid); } else { if (core->io && core->io->desc) { - r_debug_attach (core->dbg, - r_io_fd_get_pid (core->io, core->io->desc->fd)); + const int pid = r_io_fd_get_pid (core->io, core->io->desc->fd); + r_debug_attach (core->dbg, pid); } } r_debug_select (core->dbg, core->dbg->pid, core->dbg->tid); @@ -2533,9 +2533,9 @@ static void cmd_debug_reg(RCore *core, const char *str) { RRegItem *r; const char *name; int size, type = R_REG_TYPE_GPR; - int bits = (core->dbg->bits & R_SYS_BITS_64)? 64: 32; - int use_colors = r_config_get_i (core->config, "scr.color"); - int newbits = atoi ((str&&*str)? str + 1: ""); + int bits = R_SYS_BITS_CHECK (core->dbg->bits, 64)? 64: 32; + const bool use_colors = r_config_get_i (core->config, "scr.color") > 0; + int newbits = atoi (R_STR_ISNOTEMPTY (str)? str + 1: ""); if (newbits > 0) { bits = newbits; } @@ -3440,10 +3440,10 @@ static void get_backtrace_info(RCore* core, RDebugFrame* frame, ut64 addr, char* } } if (pcstr && spstr) { - if (core->dbg->bits & R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (core->dbg->bits, 64)) { *pcstr = r_str_newf ("0x%-16" PFMT64x, frame->addr); *spstr = r_str_newf ("0x%-16" PFMT64x, frame->sp); - } else if (core->dbg->bits & R_SYS_BITS_32) { + } else if (R_SYS_BITS_CHECK (core->dbg->bits, 32)) { *pcstr = r_str_newf ("0x%-8" PFMT64x, frame->addr); *spstr = r_str_newf ("0x%-8" PFMT64x, frame->sp); } else { diff --git a/libr/core/core.c b/libr/core/core.c index f7cf921edfcd7..09c4b2e88e929 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -2759,9 +2759,9 @@ R_API bool r_core_init(RCore *core) { r_asm_use (core->rasm, R_SYS_ARCH); r_anal_use (core->anal, R_SYS_ARCH); #endif - if (R_SYS_BITS & R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (R_SYS_BITS, 64)) { r_config_set_i (core->config, "asm.bits", 64); - } else if (R_SYS_BITS & R_SYS_BITS_32) { + } else if (R_SYS_BITS_CHECK (R_SYS_BITS, 32)) { r_config_set_i (core->config, "asm.bits", 32); } r_config_set (core->config, "asm.arch", R_SYS_ARCH); @@ -3024,11 +3024,9 @@ static void set_prompt(RCore *r) { prompt_sec (r, sec, sizeof (sec)); } if (!promptset) { - if (r->print->wide_offsets && r->dbg->bits & R_SYS_BITS_64) { - snprintf (p, sizeof (p), "0x%016" PFMT64x, r->offset); - } else { - snprintf (p, sizeof (p), "0x%08" PFMT64x, r->offset); - } + const char *fmt = (r->print->wide_offsets && R_SYS_BITS_CHECK (r->dbg->bits, 64)) + ? "0x%016" PFMT64x : "0x%08" PFMT64x; + snprintf (p, sizeof (p), fmt, r->offset); } snprintf (tmp, sizeof (tmp), "%s%s", sec, p); } diff --git a/libr/core/dmh_windows.inc.c b/libr/core/dmh_windows.inc.c index 0a7304fc17596..13e7732619fc0 100644 --- a/libr/core/dmh_windows.inc.c +++ b/libr/core/dmh_windows.inc.c @@ -355,11 +355,12 @@ static bool GetLFHKey(RDebug *dbg, HANDLE h_proc, bool segment, WPARAM *lfhKey) static bool DecodeHeapEntry(RDebug *dbg, PHEAP heap, PHEAP_ENTRY entry) { R_RETURN_VAL_IF_FAIL (heap && entry, false); - if (dbg->bits == R_SYS_BITS_64) { + const bool is64 = R_SYS_BITS_CHECK (dbg->bits, 64); + if (is64) { entry = (PHEAP_ENTRY)((ut8 *)entry + dbg->bits); } if (heap->EncodeFlagMask && (*(UINT32 *)entry & heap->EncodeFlagMask)) { - if (dbg->bits == R_SYS_BITS_64) { + if (is64) { heap = (PHEAP)((ut8 *)heap + dbg->bits); } *(WPARAM *)entry ^= *(WPARAM *)&heap->Encoding; @@ -369,10 +370,10 @@ static bool DecodeHeapEntry(RDebug *dbg, PHEAP heap, PHEAP_ENTRY entry) { static bool DecodeLFHEntry(RDebug *dbg, PHEAP heap, PHEAP_ENTRY entry, PHEAP_USERDATA_HEADER userBlocks, WPARAM key, WPARAM addr) { R_RETURN_VAL_IF_FAIL (heap && entry, false); - if (dbg->bits == R_SYS_BITS_64) { + const bool is64 = R_SYS_BITS_CHECK (dbg->bits, 64); + if (is64) { entry = (PHEAP_ENTRY)((ut8 *)entry + dbg->bits); } - if (heap->EncodeFlagMask) { *(DWORD *)entry ^= PtrToInt (heap->BaseAddress) ^ (DWORD)(((DWORD)addr - PtrToInt (userBlocks)) << 0xC) ^ (DWORD)key ^ (addr >> 4); } @@ -411,7 +412,8 @@ static RList *GetListOfHeaps(RDebug *dbg, HANDLE ph) { PVOID heapAddress; PVOID *processHeaps; ULONG numberOfHeaps; - if (dbg->bits == R_SYS_BITS_64) { + const bool is64 = R_SYS_BITS_CHECK (dbg->bits, 64); + if (is64) { processHeaps = *((PVOID *)(((ut8 *)&peb) + 0xF0)); numberOfHeaps = *((ULONG *)(((ut8 *)& peb) + 0xE8)); } else { @@ -724,7 +726,7 @@ static PDEBUG_BUFFER GetHeapBlocks(DWORD pid, RDebug *dbg) { x86 vs x64 vs WOW64 (use dbg->bits or new structs or just a big union with both versions) */ #if defined (_M_X64) - if (dbg->bits == R_SYS_BITS_32) { + if (R_SYS_BITS_CHECK (dbg->bits, 32)) { return NULL; // Nope nope nope } #endif @@ -1127,7 +1129,7 @@ static PHeapBlock GetSingleBlock(RDebug *dbg, ut64 offset) { if (entry.UnusedBytes & 0x80) { tmpEntry = entry; WPARAM userBlocksOffset; - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { *(((WPARAM *)&tmpEntry) + 1) ^= PtrToInt (h.BaseAddress) ^ (entryOffset >> 0x4) ^ (DWORD)NtLFHKey; userBlocksOffset = entryOffset - (USHORT)((*(((WPARAM *)&tmpEntry) + 1)) >> 0xC); } else { diff --git a/libr/core/visual.c b/libr/core/visual.c index 479da91cb5200..01a2b5f92f4d6 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -1464,7 +1464,7 @@ R_API int r_core_visual_refs(RCore *core, bool xref, bool fcnInsteadOfAddr) { r_cons_clear00 (); r_cons_gotoxy (1, 1); { - char *address = (core->dbg->bits & R_SYS_BITS_64) + char *address = R_SYS_BITS_CHECK (core->dbg->bits, 64) ? r_str_newf ("0x%016"PFMT64x, addr) : r_str_newf ("0x%08"PFMT64x, addr); r_cons_printf ("[%s%srefs]> %s # (TAB/jk/q/?) ", @@ -4230,7 +4230,7 @@ static void visual_title(RCore *core, int color) { } { char *title; - char *address = (core->print->wide_offsets && core->dbg->bits & R_SYS_BITS_64) + char *address = (core->print->wide_offsets && R_SYS_BITS_CHECK (core->dbg->bits, 64)) ? r_str_newf ("0x%016"PFMT64x, core->offset) : r_str_newf ("0x%08"PFMT64x, core->offset); if (core->visual.ime) { diff --git a/libr/debug/debug.c b/libr/debug/debug.c index c44828e7f18af..70c31ff4cadbd 100644 --- a/libr/debug/debug.c +++ b/libr/debug/debug.c @@ -487,33 +487,27 @@ R_API bool r_debug_set_arch(RDebug *dbg, const char *arch, int bits) { } switch (bits) { case 16: - if (plugin->bits == 16) { - dbg->bits = R_SYS_BITS_16; + if (R_SYS_BITS_CHECK (plugin->bits, 16)) { + dbg->bits = R_SYS_BITS_PACK (16); } break; case 27: - if (plugin->bits == 27) { - dbg->bits = R_SYS_BITS_27; + if (R_SYS_BITS_CHECK (plugin->bits, 27)) { + dbg->bits = R_SYS_BITS_PACK (27); } break; case 32: - if (plugin->bits & R_SYS_BITS_32) { - dbg->bits = R_SYS_BITS_32; + if (R_SYS_BITS_CHECK (plugin->bits, 32)) { + dbg->bits = R_SYS_BITS_PACK (32); } break; case 64: - dbg->bits = R_SYS_BITS_64; + dbg->bits = R_SYS_BITS_PACK (64); break; } - if (!plugin->bits) { - dbg->bits = plugin->bits; - } else if (!(plugin->bits & dbg->bits)) { - dbg->bits = plugin->bits & R_SYS_BITS_64; - if (!dbg->bits) { - dbg->bits = plugin->bits & R_SYS_BITS_32; - } - if (!dbg->bits) { - dbg->bits = R_SYS_BITS_32; + if (plugin->bits) { + if (R_SYS_BITS_CHECK (plugin->bits, bits)) { + dbg->bits = R_SYS_BITS_PACK (bits); } } free (dbg->arch); @@ -540,7 +534,6 @@ R_API bool r_debug_execute(RDebug *dbg, const ut8 *buf, int len, R_OUT ut64 *ret R_LOG_WARN ("Child is dead"); return false; } - #if 0 if (restore && !ignore_stack) { R_LOG_ERROR ("r_debug_execute: Cannot get stack pointer"); @@ -882,7 +875,7 @@ R_API int r_debug_step_soft(RDebug *dbg) { switch (op.type) { case R_ANAL_OP_TYPE_RET: dbg->iob.read_at (dbg->iob.io, sp, (ut8 *)&sp_top, 8); - next[0] = (dbg->bits == R_SYS_BITS_32) ? sp_top.r32[0] : sp_top.r64; + next[0] = R_SYS_BITS_CHECK (dbg->bits, 64) ? sp_top.r64 : sp_top.r32[0]; br = 1; break; case R_ANAL_OP_TYPE_CJMP: @@ -908,7 +901,7 @@ R_API int r_debug_step_soft(RDebug *dbg) { if (!dbg->iob.read_at (dbg->iob.io, r, (ut8*)&memval, 8)) { next[0] = op.addr + op.size; } else { - next[0] = (dbg->bits == R_SYS_BITS_32) ? memval.r32[0] : memval.r64; + next[0] = R_SYS_BITS_CHECK (dbg->bits, 64) ? memval.r64 : memval.r32[0]; } br = 1; break; @@ -922,7 +915,7 @@ R_API int r_debug_step_soft(RDebug *dbg) { if (!dbg->iob.read_at (dbg->iob.io, r*op.scale + op.disp, (ut8*)&memval, 8)) { next[0] = op.addr + op.size; } else { - next[0] = (dbg->bits == R_SYS_BITS_32) ? memval.r32[0] : memval.r64; + next[0] = R_SYS_BITS_CHECK (dbg->bits, 64) ? memval.r64: memval.r32[0]; } br = 1; break; diff --git a/libr/debug/dmap.c b/libr/debug/dmap.c index 30f8c4cc632fb..02eabb307be50 100644 --- a/libr/debug/dmap.c +++ b/libr/debug/dmap.c @@ -42,7 +42,7 @@ static void print_debug_map_line(RDebug *dbg, RDebugMap *map, ut64 addr, const c ); free (name); } else { - const char *fmtstr = dbg->bits & R_SYS_BITS_64 + const char *fmtstr = R_SYS_BITS_CHECK (dbg->bits, 64) ? "0x%016" PFMT64x " - 0x%016" PFMT64x " %c %s %6s %c %s %s %s%s%s\n" : "0x%08" PFMT64x " - 0x%08" PFMT64x " %c %s %6s %c %s %s %s%s%s\n"; const char *type = map->shared ? "sys": "usr"; @@ -228,7 +228,8 @@ static void print_debug_maps_ascii_art(RDebug *dbg, RList *maps, ut64 addr, int mul = findMinMax (maps, &min, &max, skip, width); // Recalculate minmax } skip++; - fmtstr = dbg->bits & R_SYS_BITS_64 // Prefix formatting string (before bar) + const bool is64 = R_SYS_BITS_CHECK (dbg->bits, 64); + fmtstr = is64 ? "map %4.8s %c %s0x%016" PFMT64x "%s |" : "map %4.8s %c %s0x%08" PFMT64x "%s |"; dbg->cb_printf (fmtstr, humansz, @@ -245,9 +246,9 @@ static void print_debug_maps_ascii_art(RDebug *dbg, RList *maps, ut64 addr, int dbg->cb_printf ("-"); } } - fmtstr = dbg->bits & R_SYS_BITS_64 ? // Suffix formatting string (after bar) - "| %s0x%016" PFMT64x "%s %s %s\n" : - "| %s0x%08" PFMT64x "%s %s %s\n"; + fmtstr = is64 // Suffix formatting string (after bar) + ? "| %s0x%016" PFMT64x "%s %s %s\n" + : "| %s0x%08" PFMT64x "%s %s %s\n"; dbg->cb_printf (fmtstr, color_prefix, map->addr_end, color_suffix, r_str_rwx_i (map->perm), map->name); last = map->addr; diff --git a/libr/debug/dreg.c b/libr/debug/dreg.c index c318fd4dd44e6..6fea31369e7b2 100644 --- a/libr/debug/dreg.c +++ b/libr/debug/dreg.c @@ -103,7 +103,7 @@ R_API bool r_debug_reg_list(RDebug *dbg, int type, int size, PJ *pj, int rad, co RRegItem *item; ut64 diff; char strvalue[256]; - bool isJson = (rad == 'j' || rad == 'J'); + bool isJson = tolower (rad) == 'j'; R_RETURN_VAL_IF_FAIL (!isJson || (isJson && pj), false); if (dbg->coreb.core) { @@ -119,7 +119,7 @@ R_API bool r_debug_reg_list(RDebug *dbg, int type, int size, PJ *pj, int rad, co size = 16; } } - if (dbg->bits & R_SYS_BITS_64) { + if (size == 64) { fmt = "%s = %s%s"; fmt2 = "%s%6s%s %s%s"; kwhites = " "; @@ -202,7 +202,7 @@ R_API bool r_debug_reg_list(RDebug *dbg, int type, int size, PJ *pj, int rad, co if (isJson) { pj_kn (pj, item->name, value); } else { - if (pr && pr->wide_offsets && dbg->bits & R_SYS_BITS_64) { + if (pr && pr->wide_offsets && R_SYS_BITS_CHECK (dbg->bits, 64)) { snprintf (strvalue, sizeof (strvalue), "0x%016"PFMT64x, value); } else { snprintf (strvalue, sizeof (strvalue),"0x%08"PFMT64x, value); diff --git a/libr/debug/p/debug_bf.c b/libr/debug/p/debug_bf.c index 4e7d3368abd54..5aabe710e78cc 100644 --- a/libr/debug/p/debug_bf.c +++ b/libr/debug/p/debug_bf.c @@ -234,7 +234,7 @@ RDebugPlugin r_debug_plugin_bf = { .license = "LGPL-3.0-only", }, .arch = "bf", - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), .init_plugin = init_plugin, .fini_plugin = fini_plugin, .step = r_debug_bf_step, diff --git a/libr/debug/p/debug_bochs.c b/libr/debug/p/debug_bochs.c index a13f6368ff369..550d6847a20a1 100644 --- a/libr/debug/p/debug_bochs.c +++ b/libr/debug/p/debug_bochs.c @@ -611,7 +611,7 @@ RDebugPlugin r_debug_plugin_bochs = { .license = "LGPL-3.0-only", }, .arch = "x86", - .bits = R_SYS_BITS_16 | R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK3 (16, 32, 64), .init_plugin = init_plugin, .fini_plugin = fini_plugin, .step = r_debug_bochs_step, diff --git a/libr/debug/p/debug_esil.c b/libr/debug/p/debug_esil.c index 163f7dcfb0b0c..09285ee0106d2 100644 --- a/libr/debug/p/debug_esil.c +++ b/libr/debug/p/debug_esil.c @@ -132,7 +132,7 @@ RDebugPlugin r_debug_plugin_esil = { .license = "LGPL-3.0-only", }, .arch = "any", // TODO: exception! - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), .init_debugger = __esil_init, .step = __esil_step, .step_over = __esil_step_over, diff --git a/libr/debug/p/debug_evm.c b/libr/debug/p/debug_evm.c index deffa559bdbd4..3436a841bb9e1 100644 --- a/libr/debug/p/debug_evm.c +++ b/libr/debug/p/debug_evm.c @@ -240,7 +240,7 @@ RDebugPlugin r_debug_plugin_evm = { .desc = "evm debugger backend", }, .arch = "evm", - .bits = R_SYS_BITS_16 | R_SYS_BITS_8, + .bits = R_SYS_BITS_PACK2 (8, 16), .step = r_debug_evm_step, .cont = r_debug_evm_continue, .attach = &r_debug_evm_attach, diff --git a/libr/debug/p/debug_gdb.c b/libr/debug/p/debug_gdb.c index df85164a3a6df..a6984a1f64292 100644 --- a/libr/debug/p/debug_gdb.c +++ b/libr/debug/p/debug_gdb.c @@ -679,7 +679,7 @@ RDebugPlugin r_debug_plugin_gdb = { }, /* TODO: Add support for more architectures here */ .arch = "x86,arm,sh,mips,avr,lm32,v850,ba2", - .bits = R_SYS_BITS_16 | R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK3 (16, 32, 64), .init_plugin = init_plugin, .fini_plugin = fini_plugin, .step = r_debug_gdb_step, diff --git a/libr/debug/p/debug_io.c b/libr/debug/p/debug_io.c index 43f7c0f7ab0c4..0788836937b2c 100644 --- a/libr/debug/p/debug_io.c +++ b/libr/debug/p/debug_io.c @@ -147,7 +147,7 @@ RDebugPlugin r_debug_plugin_io = { .desc = "io debug plugin", }, .arch = "any", // TODO: exception! - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), .step = __io_step, .map_get = __io_maps, .attach = &__io_attach, diff --git a/libr/debug/p/debug_native.c b/libr/debug/p/debug_native.c index 54938d4eb1a08..78245c4b41e95 100644 --- a/libr/debug/p/debug_native.c +++ b/libr/debug/p/debug_native.c @@ -1658,23 +1658,23 @@ RDebugPlugin r_debug_plugin_native = { .desc = "native debug plugin", }, #if __i386__ - .bits = R_SYS_BITS_32, + .bits = R_SYS_BITS_PACK (32), .arch = "x86", .canstep = true, #elif __s390x__ || __s390__ - .bits = R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK (64), .arch = "s390", .canstep = true, #elif __riscv || __riscv__ || __riscv64__ - .bits = R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK (64), .arch = "riscv", .canstep = false, #elif __x86_64__ - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), .arch = "x86", .canstep = true, // XXX it's 1 on some platforms... #elif __aarch64__ || __arm64__ || __arm64e__ - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), .arch = "arm", #if __APPLE__ .canstep = true, @@ -1682,22 +1682,22 @@ RDebugPlugin r_debug_plugin_native = { .canstep = false, #endif #elif __arm__ - .bits = R_SYS_BITS_16 | R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK3 (16, 32, 64), .arch = "arm", .canstep = false, #elif __mips__ - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), .arch = "mips", .canstep = false, #elif __loongarch - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), .arch = "loongarch", .canstep = false, #elif __powerpc__ # if __powerpc64__ - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), # else - .bits = R_SYS_BITS_32, + .bits = R_SYS_BITS_PACK (32), #endif .arch = "ppc", .canstep = true, diff --git a/libr/debug/p/debug_null.c b/libr/debug/p/debug_null.c index a737b489ec685..d2b8458b1f47a 100644 --- a/libr/debug/p/debug_null.c +++ b/libr/debug/p/debug_null.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2016-2017 pancake */ +/* radare - LGPL - Copyright 2016-2024 pancake */ #include #include @@ -11,7 +11,7 @@ RDebugPlugin r_debug_plugin_null = { .license = "MIT", }, .arch = "any", - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), }; #ifndef R2_PLUGIN_INCORE diff --git a/libr/debug/p/debug_qnx.c b/libr/debug/p/debug_qnx.c index 0445872f6ec8a..e873e269a651a 100644 --- a/libr/debug/p/debug_qnx.c +++ b/libr/debug/p/debug_qnx.c @@ -399,7 +399,7 @@ RDebugPlugin r_debug_plugin_qnx = { .license = "LGPL-3.0-only", }, .arch = "x86,arm", - .bits = R_SYS_BITS_32, + .bits = R_SYS_BITS_PACK (32), .init_plugin = init_plugin, .fini_plugin = fini_plugin, .step = r_debug_qnx_step, diff --git a/libr/debug/p/debug_rap.c b/libr/debug/p/debug_rap.c index ccf06d9cada14..e3b7419c42459 100644 --- a/libr/debug/p/debug_rap.c +++ b/libr/debug/p/debug_rap.c @@ -71,7 +71,7 @@ RDebugPlugin r_debug_plugin_rap = { .license = "LGPL-3.0-only", }, .arch = "any", - .bits = R_SYS_BITS_32, + .bits = R_SYS_BITS_PACK (32), .step = __rap_step, .cont = __rap_continue, .attach = &__rap_attach, diff --git a/libr/debug/p/debug_rv32ima.c b/libr/debug/p/debug_rv32ima.c index 71f37961f7e8d..1d102237fc5ae 100644 --- a/libr/debug/p/debug_rv32ima.c +++ b/libr/debug/p/debug_rv32ima.c @@ -249,7 +249,7 @@ RDebugPlugin r_debug_plugin_rv32ima = { .license = "MIT", }, .arch = "riscv", - .bits = R_SYS_BITS_32, + .bits = R_SYS_BITS_PACK (32), .init_plugin = init_plugin, .fini_plugin = fini_plugin, .init_debugger = __rv32ima_init, diff --git a/libr/debug/p/debug_windbg.c b/libr/debug/p/debug_windbg.c index b36fb8419c198..8b0107ca9e195 100644 --- a/libr/debug/p/debug_windbg.c +++ b/libr/debug/p/debug_windbg.c @@ -256,7 +256,7 @@ static char *windbg_reg_profile(RDebug *dbg) { DbgEngContext *idbg = dbg->user; ULONG type; if (!idbg || !idbg->initialized || FAILED (ITHISCALL (dbgCtrl, GetActualProcessorType, &type))) { - if (dbg->bits & R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { #include "native/reg/windows-x64.h" } else { #include "native/reg/windows-x86.h" @@ -613,7 +613,7 @@ RDebugPlugin r_debug_plugin_windbg = { .author = "pancake", .desc = "comunicate with a windbg", }, - .bits = R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK (64), .arch = "x86,x64,arm,arm64", .canstep = 1, .init_debugger = windbg_init, diff --git a/libr/debug/p/debug_winkd.c b/libr/debug/p/debug_winkd.c index 51e0ceb03d091..0218913b7bd89 100644 --- a/libr/debug/p/debug_winkd.c +++ b/libr/debug/p/debug_winkd.c @@ -151,10 +151,10 @@ static char *r_debug_winkd_reg_profile(RDebug *dbg) { return NULL; } r_debug_winkd_attach (dbg, 0); - if (dbg->bits == R_SYS_BITS_32) { -#include "native/reg/windows-x86.h" - } else if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { #include "native/reg/windows-x64.h" + } else if (R_SYS_BITS_CHECK (dbg->bits, 32)) { +#include "native/reg/windows-x86.h" } return NULL; } @@ -309,7 +309,6 @@ static RList *r_debug_winkd_modules(RDebug *dbg) { static bool init_plugin(RDebug *dbg, RDebugPluginSession *ds) { R_RETURN_VAL_IF_FAIL (dbg && ds, false); - ds->plugin_data = R_NEW0 (PluginData); return !!ds->plugin_data; } @@ -333,7 +332,7 @@ RDebugPlugin r_debug_plugin_winkd = { .license = "LGPL-3.0-only", }, .arch = "x86", - .bits = R_SYS_BITS_32 | R_SYS_BITS_64, + .bits = R_SYS_BITS_PACK2 (32, 64), .init_plugin = init_plugin, .fini_plugin = fini_plugin, .init_debugger = &r_debug_winkd_init, diff --git a/libr/debug/p/native/bt.c b/libr/debug/p/native/bt.c index 2847311808fb0..10b297e08945c 100644 --- a/libr/debug/p/native/bt.c +++ b/libr/debug/p/native/bt.c @@ -47,7 +47,7 @@ static RList *r_debug_native_frames(RDebug *dbg, ut64 at) { if (!strcmp (dbg->btalgo, "fuzzy")) { cb = backtrace_fuzzy; } else if (!strcmp (dbg->btalgo, "anal")) { - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { cb = backtrace_x86_64_anal; } else { cb = backtrace_x86_32_anal; @@ -55,7 +55,7 @@ static RList *r_debug_native_frames(RDebug *dbg, ut64 at) { } } if (!cb) { - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { cb = backtrace_x86_64; } else { cb = backtrace_x86_32; diff --git a/libr/debug/p/native/linux/linux_debug.c b/libr/debug/p/native/linux/linux_debug.c index deec292d3f0bc..a1a14830b58c8 100644 --- a/libr/debug/p/native/linux/linux_debug.c +++ b/libr/debug/p/native/linux/linux_debug.c @@ -29,13 +29,15 @@ char *linux_reg_profile (RDebug *dbg) { #elif __riscv # include "reg/linux-riscv64.h" #elif __arm64__ || __aarch64__ - if (dbg->bits & R_SYS_BITS_32) { -# include "reg/linux-arm.h" - } else { + const bool is64 = R_SYS_BITS_CHECK (dbg->bits, 64); + if (is64) { # include "reg/linux-arm64.h" + } else { +# include "reg/linux-arm.h" } #elif __mips__ - if ((dbg->bits & R_SYS_BITS_32) && (dbg->bp->endian == 1)) { + const bool is32 = R_SYS_BITS_CHECK (dbg->bits, 32); + if (is32 && (dbg->bp->endian == 1)) { # include "reg/linux-mips.h" } else { # include "reg/linux-mips64.h" @@ -43,7 +45,8 @@ char *linux_reg_profile (RDebug *dbg) { #elif __loongarch__ # include "reg/linux-loongarch64.h" #elif (__i386__ || __x86_64__) - if (dbg->bits & R_SYS_BITS_32) { + const bool is32 = R_SYS_BITS_CHECK (dbg->bits, 32); + if (is32) { #if __x86_64__ # include "reg/linux-x64-32.h" #else @@ -53,16 +56,18 @@ char *linux_reg_profile (RDebug *dbg) { # include "reg/linux-x64.h" } #elif __powerpc__ - if (dbg->bits & R_SYS_BITS_32) { -# include "reg/linux-ppc.h" - } else { + const bool is64 = R_SYS_BITS_CHECK (dbg->bits, 64); + if (is64) { # include "reg/linux-ppc64.h" + } else { +# include "reg/linux-ppc.h" } #elif __s390x__ - if (dbg->bits & R_SYS_BITS_32) { -# include "reg/linux-s390x.h" - } else { + const bool is64 = R_SYS_BITS_CHECK (dbg->bits, 64); + if (is64) { # include "reg/linux-zarch.h" + } else { +# include "reg/linux-s390x.h" } #else # error "Unsupported Linux CPU" @@ -1267,6 +1272,7 @@ bool linux_reg_read(RDebug *dbg, int type, ut8 *buf, int size) { } size = R_MIN (sizeof (regs), size); memcpy (buf, ®s, size); + // r_print_hexdump (NULL, 0, buf, size, 16, 16, 0); return size; } break; diff --git a/libr/debug/p/native/reg.c b/libr/debug/p/native/reg.c index 38f1d597fa3bf..d5bf69d4da733 100644 --- a/libr/debug/p/native/reg.c +++ b/libr/debug/p/native/reg.c @@ -10,7 +10,7 @@ static char *r_debug_native_reg_profile(RDebug *dbg) { | | | |___|___| */ - if (dbg->bits & R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { #include "reg/windows-x64.h" } else { #include "reg/windows-x86.h" diff --git a/libr/debug/p/native/windows/windows_debug.c b/libr/debug/p/native/windows/windows_debug.c index deab43c24e066..323f8e9e14ad4 100644 --- a/libr/debug/p/native/windows/windows_debug.c +++ b/libr/debug/p/native/windows/windows_debug.c @@ -56,11 +56,10 @@ static PTHREAD_ITEM __r_debug_thread_add(RDebug *dbg, DWORD pid, DWORD tid, HAND } static int __suspend_thread(HANDLE th, int bits) { - int ret; - //if (bits == R_SYS_BITS_32) { - if ((ret = SuspendThread (th)) == -1) { - r_sys_perror ("__suspend_thread/SuspendThread"); - } + int ret = SuspendThread (th); + if (ret == -1) { + r_sys_perror ("__suspend_thread/SuspendThread"); + } /*} else { if ((ret = Wow64SuspendThread (th)) == -1) { r_sys_perror ("__suspend_thread/Wow64SuspendThread"); @@ -70,16 +69,10 @@ static int __suspend_thread(HANDLE th, int bits) { } static int __resume_thread(HANDLE th, int bits) { - int ret; - //if (bits == R_SYS_BITS_32) { - if ((ret = ResumeThread (th)) == -1) { - r_sys_perror ("__resume_thread/ResumeThread"); - } - /*} else { - if ((ret = ResumeThread (th)) == -1) { - r_sys_perror ("__resume_thread/Wow64ResumeThread"); - } - }*/ + int ret = ResumeThread (th); + if (ret == -1) { + r_sys_perror ("__resume_thread/ResumeThread"); + } return ret; } @@ -298,8 +291,8 @@ int w32_reg_read(RDebug *dbg, int type, ut8 *buf, int size) { HANDLE th = wrap->pi.dwThreadId == dbg->tid ? wrap->pi.hThread : NULL; if (!th || th == INVALID_HANDLE_VALUE) { DWORD flags = THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT; - if (dbg->bits == R_SYS_BITS_64) { - flags |= THREAD_QUERY_INFORMATION; + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { + flags |= THREAD_QUERY_INFORMATION; } th = OpenThread (flags, FALSE, dbg->tid); if (!th && alive) { @@ -342,7 +335,7 @@ static void __transfer_drx(RDebug *dbg, const ut8 *buf) { int w32_reg_write(RDebug *dbg, int type, const ut8 *buf, int size) { DWORD flags = THREAD_SUSPEND_RESUME | THREAD_SET_CONTEXT; - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { flags |= THREAD_QUERY_INFORMATION; } HANDLE th = OpenThread (flags, FALSE, dbg->tid); diff --git a/libr/debug/p/native/windows/windows_message.c b/libr/debug/p/native/windows/windows_message.c index 0e182c4018525..19ed982c775a4 100644 --- a/libr/debug/p/native/windows/windows_message.c +++ b/libr/debug/p/native/windows/windows_message.c @@ -495,7 +495,7 @@ R_API bool r_w32_add_winmsg_breakpoint(RDebug *dbg, const char *input) { cond = r_str_newf ("?= `ae %lu,edx,-`", type); } else { char *reg; - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { reg = "rcx"; } else { reg = "ecx"; diff --git a/libr/debug/p/native/xnu/trap_arm.c b/libr/debug/p/native/xnu/trap_arm.c index 2678f6fbe8c82..1a812bd909fd9 100644 --- a/libr/debug/p/native/xnu/trap_arm.c +++ b/libr/debug/p/native/xnu/trap_arm.c @@ -87,7 +87,7 @@ static bool ios_hwstep_enable32(RDebug *dbg, bool enable) { } bool xnu_native_hwstep_enable(RDebug *dbg, bool enable) { - if (dbg->bits == R_SYS_BITS_64 || dbg->bits == 64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { return ios_hwstep_enable64 (dbg, enable); } return ios_hwstep_enable32 (dbg, enable); diff --git a/libr/debug/p/native/xnu/trap_x86.c b/libr/debug/p/native/xnu/trap_x86.c index 25499bfc2d590..46c7ffae9a848 100644 --- a/libr/debug/p/native/xnu/trap_x86.c +++ b/libr/debug/p/native/xnu/trap_x86.c @@ -50,7 +50,7 @@ static bool xnu_x86_hwstep_enable32(RDebug *dbg, bool enable) { } bool xnu_native_hwstep_enable(RDebug *dbg, bool enable) { - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { return xnu_x86_hwstep_enable64 (dbg, enable); } return xnu_x86_hwstep_enable32 (dbg, enable); diff --git a/libr/debug/p/native/xnu/xnu_debug.c b/libr/debug/p/native/xnu/xnu_debug.c index 0ffbeeee06329..a88c3eac1320a 100644 --- a/libr/debug/p/native/xnu/xnu_debug.c +++ b/libr/debug/p/native/xnu/xnu_debug.c @@ -418,9 +418,9 @@ bool xnu_continue(RDebug *dbg, int pid, int tid, int sig) { char *xnu_reg_profile(RDebug *dbg) { #if __i386__ || __x86_64__ - if (dbg->bits & R_SYS_BITS_32) { + if (R_SYS_BITS_CHECK (dbg->bits, 32)) { # include "reg/darwin-x86.h" - } else if (dbg->bits == R_SYS_BITS_64) { + } else if (R_SYS_BITS_CHECK (dbg->bits, 64)) { # include "reg/darwin-x64.h" } else { R_LOG_ERROR ("invalid bit size"); @@ -429,7 +429,7 @@ char *xnu_reg_profile(RDebug *dbg) { #elif __POWERPC__ # include "reg/darwin-ppc.h" #elif __APPLE__ && (__aarch64__ || __arm64__ || __arm__ || __arm64e__) - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { # include "reg/darwin-arm64.h" } else { # include "reg/darwin-arm.h" @@ -455,7 +455,7 @@ bool xnu_reg_write(RDebug *dbg, int type, const ut8 *buf, int size) { #elif __i386__ memcpy (&th->drx.uds.ds64, buf, R_MIN (size, sizeof (th->drx))); #elif __arm64 || __aarch64 || __arm64e - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { memcpy (&th->debug.drx64, buf, R_MIN (size, sizeof (th->debug.drx64))); } else { memcpy (&th->debug.drx32, buf, R_MIN (size, sizeof (th->debug.drx32))); @@ -625,17 +625,15 @@ static void xnu_free_threads_ports(RDebugPid *p) { RList *xnu_thread_list(RDebug *dbg, int pid, RList *list) { #if __arm64__ || __aarch_64__ || __arm64e__ - //#define CPU_PC (dbg->bits == R_SYS_BITS_64) ? \ - // state.arm64.__pc : state.arm32.__pc - #define CPU_PC (dbg->bits == R_SYS_BITS_64) ? \ + #define CPU_PC R_SYS_BITS_CHECK (dbg->bits, 64)? \ __darwin_arm_thread_state64_get_pc (state.ts_64) : state.ts_32.__pc #elif __arm__ || __arm - #define CPU_PC (dbg->bits == R_SYS_BITS_64) ? \ + #define CPU_PC R_SYS_BITS_CHECK (dbg->bits, 64)? \ state.ts_64.__pc : state.ts_32.__pc #elif __POWERPC__ #define CPU_PC state.srr0 #elif __x86_64__ || __i386__ - #define CPU_PC (dbg->bits == R_SYS_BITS_64) ? \ + #define CPU_PC R_SYS_BITS_CHECK (dbg->bits, 64)? \ state.uts.ts64.__rip : state.uts.ts32.__eip #endif RListIter *iter; diff --git a/libr/debug/p/native/xnu/xnu_debug.h b/libr/debug/p/native/xnu/xnu_debug.h index e6423c70f592c..ac107a64ad32d 100644 --- a/libr/debug/p/native/xnu/xnu_debug.h +++ b/libr/debug/p/native/xnu/xnu_debug.h @@ -115,12 +115,8 @@ int ptrace(int _request, pid_t _pid, caddr_t _addr, int _data); #define IMAGE_OFFSET 0x201000 #define KERNEL_LOWER 0x80000000 #endif -//#define R_DEBUG_STATE_T XXX -//(dbg->bits==64)?x86_THREAD_STATE:_STRUCT_X86_THREAD_STATE32 -//#define R_DEBUG_REG_T _STRUCT_X86_THREAD_STATE64 -//#define R_DEBUG_STATE_SZ ((dbg->bits == R_SYS_BITS_64) ? 168 : 64) -#define REG_PC ((dbg->bits == R_SYS_BITS_64) ? 16 : 10) -#define REG_FL ((dbg->bits == R_SYS_BITS_64) ? 17 : 9) +#define REG_PC (R_SYS_BITS_CHECK (dbg->bits, 64) ? 16 : 10) +#define REG_FL (R_SYS_BITS_CHECK (dbg->bits, 64) ? 17 : 9) #define REG_SP (7) #endif diff --git a/libr/debug/p/native/xnu/xnu_threads.c b/libr/debug/p/native/xnu/xnu_threads.c index e087f97ce56cd..d47a157e4d5f7 100644 --- a/libr/debug/p/native/xnu/xnu_threads.c +++ b/libr/debug/p/native/xnu/xnu_threads.c @@ -58,14 +58,14 @@ static bool xnu_thread_get_drx(RDebug *dbg, xnu_thread_t *thread) { #if __x86_64__ || __i386__ thread->flavor = x86_DEBUG_STATE; thread->count = x86_DEBUG_STATE_COUNT; - thread->state_size = (dbg->bits == R_SYS_BITS_64) + thread->state_size = R_SYS_BITS_CHECK (dbg->bits, 64) ? sizeof (x86_debug_state64_t) : sizeof (x86_debug_state32_t); thread->state = &thread->drx.uds; rc = thread_get_state (thread->port, thread->flavor, (thread_state_t)&thread->drx, &thread->count); #elif __arm64__ || __arm64 || __aarch64 || __aarch64__ || __arm64e__ - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { thread->count = ARM_DEBUG_STATE64_COUNT; thread->flavor = ARM_DEBUG_STATE64; rc = thread_get_state (thread->port, thread->flavor, @@ -106,7 +106,7 @@ static bool xnu_thread_set_drx(RDebug *dbg, xnu_thread_t *thread) { } thread->flavor = x86_DEBUG_STATE; thread->count = x86_DEBUG_STATE_COUNT; - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { regs->dsh.flavor = x86_DEBUG_STATE64; regs->dsh.count = x86_DEBUG_STATE64_COUNT; } else { @@ -116,7 +116,7 @@ static bool xnu_thread_set_drx(RDebug *dbg, xnu_thread_t *thread) { rc = thread_set_state (thread->port, thread->flavor, (thread_state_t)regs, thread->count); #elif __arm64__ || __arm64 || __aarch64 || __aarch64__ || __arm64e__ - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { thread->count = ARM_DEBUG_STATE64_COUNT; thread->flavor = ARM_DEBUG_STATE64; rc = thread_set_state (thread->port, thread->flavor, @@ -169,7 +169,7 @@ static bool xnu_thread_set_gpr(RDebug *dbg, xnu_thread_t *thread) { thread->state = ®s->uts; thread->flavor = x86_THREAD_STATE; thread->count = x86_THREAD_STATE_COUNT; - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { regs->tsh.flavor = x86_THREAD_STATE64; regs->tsh.count = x86_THREAD_STATE64_COUNT; } else { @@ -177,7 +177,7 @@ static bool xnu_thread_set_gpr(RDebug *dbg, xnu_thread_t *thread) { regs->tsh.count = x86_THREAD_STATE32_COUNT; } #elif __arm64 || __aarch64 || __arm64__ || __aarch64__ || __arm64e__ - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { thread_state_t remote_state = alloca (thread->state_size); thread->count = ARM_UNIFIED_THREAD_STATE_COUNT; @@ -240,7 +240,7 @@ static bool xnu_thread_get_gpr(RDebug *dbg, xnu_thread_t *thread) { #if __POWERPC__ thread->state = regs; #elif __arm64 || __aarch64 || __arch64__ || __arm64__ || __arm64e__ - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { thread->flavor = ARM_UNIFIED_THREAD_STATE; thread->count = ARM_UNIFIED_THREAD_STATE_COUNT; thread->state_size = sizeof (arm_thread_state64_t); @@ -260,9 +260,9 @@ static bool xnu_thread_get_gpr(RDebug *dbg, xnu_thread_t *thread) { thread->state = ®s->uts; thread->flavor = x86_THREAD_STATE; thread->count = x86_THREAD_STATE_COUNT; - thread->state_size = (dbg->bits == R_SYS_BITS_64) ? - sizeof (x86_thread_state64_t) : - sizeof (x86_thread_state32_t); + thread->state_size = R_SYS_BITS_CHECK (dbg->bits, 64) + ? sizeof (x86_thread_state64_t) + : sizeof (x86_thread_state32_t); #endif #if defined(THREAD_CONVERT_THREAD_STATE_TO_SELF) #if !__POWERPC__ @@ -284,7 +284,7 @@ static bool xnu_thread_get_gpr(RDebug *dbg, xnu_thread_t *thread) { #endif #if __arm64e__ else { - if (dbg->bits == R_SYS_BITS_64) { + if (R_SYS_BITS_CHECK (dbg->bits, 64)) { arm_thread_state64_ptrauth_strip (regs->ts_64); } } diff --git a/libr/include/r_core.h b/libr/include/r_core.h index 94307ea36826f..0cf3a468b96b7 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -875,7 +875,7 @@ typedef struct r_core_bin_filter_t { } RCoreBinFilter; R_API bool r_core_bin_info(RCore *core, int action, PJ *pj, int mode, int va, RCoreBinFilter *filter, const char *chksum); -R_API bool r_core_bin_set_arch_bits(RCore *r, const char *name, const char *arch, ut16 bits); +R_API bool r_core_bin_set_arch_bits(RCore *r, const char *name, const char *arch, RSysBits bits); R_API bool r_core_bin_update_arch_bits(RCore *r); R_API char *r_core_bin_attr_tostring(RCore *core, ut64 flags, int mode); R_API bool r_core_pdb_info(RCore *core, const char *file, PJ *pj, int mode); diff --git a/libr/include/r_debug.h b/libr/include/r_debug.h index fe937ba1cad67..3dc3875919270 100644 --- a/libr/include/r_debug.h +++ b/libr/include/r_debug.h @@ -294,7 +294,7 @@ typedef struct r_debug_plugin_session_t RDebugPluginSession; typedef int (*RDebugCmdCb)(RDebug *dbg, const char *cmd); typedef struct r_debug_plugin_t { RPluginMeta meta; - ut32 bits; + RSysBits bits; const char *arch; int canstep; int keepio; @@ -348,9 +348,9 @@ typedef struct r_debug_plugin_session_t { R_VEC_FORWARD_DECLARE (RVecDebugPluginSession); typedef struct r_debug_t { - // R2_590 use RArchConfig instead + // R2_600 use RArchConfig instead? char *arch; - int bits; /// XXX: MUST SET /// + int bits; // only 16, 32, 64, .. not packed int hitinfo; int main_pid; diff --git a/libr/include/r_reg.h b/libr/include/r_reg.h index d6271e7a0445b..5308960217f6a 100644 --- a/libr/include/r_reg.h +++ b/libr/include/r_reg.h @@ -1,9 +1,10 @@ #ifndef R2_REG_H #define R2_REG_H +#include #include #include -#include +#include #include #include @@ -135,7 +136,7 @@ typedef struct r_reg_t { RRegSet regset[R_REG_TYPE_LAST]; RList *allregs; char *roregs; - ut64 hasbits; + RSysBits hasbits; int iters; int size; int bits_default; diff --git a/libr/include/r_types.h b/libr/include/r_types.h index 77bfb9057f5e7..62626db3b9ff9 100644 --- a/libr/include/r_types.h +++ b/libr/include/r_types.h @@ -289,11 +289,8 @@ #if __MINGW32__ #include #include -// #define PFMT64x "I64x" -#define PFMT64x PRIx64 #endif - #ifdef __cplusplus extern "C" { #endif @@ -362,7 +359,6 @@ typedef int (*PrintfCallback)(const char *str, ...) R_PRINTF_CHECK(1, 2); #endif - #define R_HIDDEN __attribute__((visibility("hidden"))) #define R_LIB_VERSION_HEADER(x) \ @@ -441,7 +437,6 @@ static inline void *r_new_copy(int size, void *data) { #endif #endif - #define R_FREE(x) { free((void *)x); x = NULL; } #if R2__WINDOWS__ @@ -451,7 +446,11 @@ static inline void *r_new_copy(int size, void *data) { #endif #if R2__WINDOWS__ +#if __MINGW32__ +#define PFMT64x PRIx64 +#else #define PFMT64x "I64x" +#endif #define PFMT64d "I64d" #define PFMT64u "I64u" #define PFMT64o "I64o" @@ -554,23 +553,23 @@ static inline void *r_new_copy(int size, void *data) { /* arch */ #if __i386__ -#define R_SYS_ARCH "x86" -#define R_SYS_BITS R_SYS_BITS_32 -#define R_SYS_ENDIAN 0 +# define R_SYS_ARCH "x86" +# define R_SYS_BITS R_SYS_BITS_PACK (32) +# define R_SYS_ENDIAN 0 #elif __EMSCRIPTEN__ || __wasi__ -#define R_SYS_ARCH "wasm" -#define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64) -#define R_SYS_ENDIAN 0 +# define R_SYS_ARCH "wasm" +# define R_SYS_BITS R_SYS_BITS_PACK2 (32, 64) +# define R_SYS_ENDIAN 0 #elif __x86_64__ -#define R_SYS_ARCH "x86" -#define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64) -#define R_SYS_ENDIAN 0 +# define R_SYS_ARCH "x86" +# define R_SYS_BITS R_SYS_BITS_PACK2 (32, 64) +# define R_SYS_ENDIAN 0 #elif __POWERPC__ # define R_SYS_ARCH "ppc" # ifdef __powerpc64__ -# define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64) +# define R_SYS_BITS R_SYS_BITS_PACK2 (32, 64) # else -# define R_SYS_BITS R_SYS_BITS_32 +# define R_SYS_BITS R_SYS_BITS_PACK (32) # endif # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ # define R_SYS_ENDIAN 0 @@ -578,72 +577,75 @@ static inline void *r_new_copy(int size, void *data) { # define R_SYS_ENDIAN 1 # endif #elif __arm__ -#define R_SYS_ARCH "arm" -#define R_SYS_BITS R_SYS_BITS_32 -#define R_SYS_ENDIAN 0 +# define R_SYS_ARCH "arm" +# define R_SYS_BITS R_SYS_BITS_PACK (32) +# define R_SYS_ENDIAN 0 #elif __arm64__ || __aarch64__ || __arm64e__ -#define R_SYS_ARCH "arm" -#define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64) -#define R_SYS_ENDIAN 0 +# define R_SYS_ARCH "arm" +# define R_SYS_BITS R_SYS_BITS_PACK2 (32, 64) +# define R_SYS_ENDIAN 0 #elif __arc__ -#define R_SYS_ARCH "arc" -#define R_SYS_BITS R_SYS_BITS_32 -#define R_SYS_ENDIAN 0 +# define R_SYS_ARCH "arc" +# define R_SYS_BITS R_SYS_BITS_PACK (32) +# define R_SYS_ENDIAN 0 #elif __s390x__ -#define R_SYS_ARCH "s390" -#define R_SYS_BITS R_SYS_BITS_64 -#define R_SYS_ENDIAN 1 +# define R_SYS_ARCH "s390" +# define R_SYS_BITS R_SYS_BITS_PACK (64) +# define R_SYS_ENDIAN 1 #elif __sparc__ #define R_SYS_ARCH "sparc" -#define R_SYS_BITS R_SYS_BITS_32 +# define R_SYS_BITS R_SYS_BITS_PACK (32) #define R_SYS_ENDIAN 1 #elif __mips__ -#define R_SYS_ARCH "mips" -#define R_SYS_BITS R_SYS_BITS_32 -#define R_SYS_ENDIAN 1 +# define R_SYS_ARCH "mips" +# define R_SYS_BITS R_SYS_BITS_PACK (32) +# define R_SYS_ENDIAN 1 #elif __loongarch__ -#define R_SYS_ARCH "loongarch" -#define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64) -#define R_SYS_ENDIAN 1 +# define R_SYS_ARCH "loongarch" +# define R_SYS_BITS R_SYS_BITS_PACK2 (32, 64) +# define R_SYS_ENDIAN 1 #elif __EMSCRIPTEN__ /* we should default to wasm when ready */ -#define R_SYS_ARCH "x86" -#define R_SYS_BITS R_SYS_BITS_32 +# define R_SYS_ARCH "x86" +# define R_SYS_BITS R_SYS_BITS_PACK (32) #elif __riscv__ || __riscv # define R_SYS_ARCH "riscv" # define R_SYS_ENDIAN 0 # if __riscv_xlen == 32 -# define R_SYS_BITS R_SYS_BITS_32 +# define R_SYS_BITS R_SYS_BITS_PACK (32) # else -# define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64) +# define R_SYS_BITS R_SYS_BITS_PACK2 (32, 64) # endif #else #ifdef _MSC_VER #if defined(_M_ARM64) -#define R_SYS_ARCH "arm" -#define R_SYS_BITS R_SYS_BITS_64 -#define R_SYS_ENDIAN 0 +# define R_SYS_ARCH "arm" +# define R_SYS_BITS R_SYS_BITS_PACK (64) +# define R_SYS_ENDIAN 0 #elif defined(_WIN64) -#define R_SYS_ARCH "x86" -#define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64) -#define R_SYS_ENDIAN 0 -#define __x86_64__ 1 +# define R_SYS_ARCH "x86" +# define R_SYS_BITS R_SYS_BITS_PACK2 (32, 64) +# define R_SYS_ENDIAN 0 +# define __x86_64__ 1 #else -#define R_SYS_ARCH "x86" -#define R_SYS_BITS (R_SYS_BITS_32) -#define __i386__ 1 -#define R_SYS_ENDIAN 0 +# define R_SYS_ARCH "x86" +# define R_SYS_BITS R_SYS_BITS_PACK (32) +# define __i386__ 1 +# define R_SYS_ENDIAN 0 #endif #else -#define R_SYS_ARCH "unknown" -#define R_SYS_BITS R_SYS_BITS_32 -#define R_SYS_ENDIAN 0 +# define R_SYS_ARCH "unknown" +# define R_SYS_BITS R_SYS_BITS_PACK (32) +# define R_SYS_ENDIAN 0 #endif #endif +// TODO: use 1234, 4321, 1324, ... #define R_SYS_ENDIAN_NONE 0 #define R_SYS_ENDIAN_LITTLE 1 +// #define R_SYS_ENDIAN_4321 1 #define R_SYS_ENDIAN_BIG 2 +// #define R_SYS_ENDIAN_1234 2 #define R_SYS_ENDIAN_BI 3 #define R_SYS_ENDIAN_MIDDLE 4 diff --git a/libr/include/r_types_base.h b/libr/include/r_types_base.h index fae9110bab1c9..30be74f112955 100644 --- a/libr/include/r_types_base.h +++ b/libr/include/r_types_base.h @@ -258,6 +258,38 @@ typedef struct _utX { #define DEBUGGER 0 #endif +typedef ut64 RSysBits; +typedef ut8 RSysBitValue; + +#define R_SYS_BITS_SIZE 8 +#define R_SYS_BITS_MASK 0xff +#define R_SYS_BITS_PACK(x) (x) +#define R_SYS_BITS_PACK1(x) (x) +#define R_SYS_BITS_PACK2(x,y) ((x) | ((y)<> R_SYS_BITS_SIZE) & R_SYS_BITS_MASK) == (y)) || \ + ((((x) >> (R_SYS_BITS_SIZE*2)) & R_SYS_BITS_MASK) == (y)) || \ + ((((x) >> (R_SYS_BITS_SIZE*3)) & R_SYS_BITS_MASK) == (y)) \ +) +#define R_SYS_BITS_CHECK3(x, a, b, c) (bool)( \ + (((x) & R_SYS_BITS_MASK) == (a)) || \ + (((x) & R_SYS_BITS_MASK) == (b)) || \ + (((x) & R_SYS_BITS_MASK) == (c)) || \ + ((((x) >> R_SYS_BITS_SIZE) & R_SYS_BITS_MASK) == (a)) || \ + ((((x) >> R_SYS_BITS_SIZE) & R_SYS_BITS_MASK) == (b)) || \ + ((((x) >> R_SYS_BITS_SIZE) & R_SYS_BITS_MASK) == (c)) || \ + ((((x) >> (R_SYS_BITS_SIZE*2)) & R_SYS_BITS_MASK) == (a)) || \ + ((((x) >> (R_SYS_BITS_SIZE*2)) & R_SYS_BITS_MASK) == (b)) || \ + ((((x) >> (R_SYS_BITS_SIZE*2)) & R_SYS_BITS_MASK) == (c)) || \ + ((((x) >> (R_SYS_BITS_SIZE*3)) & R_SYS_BITS_MASK) == (a)) || \ + ((((x) >> (R_SYS_BITS_SIZE*3)) & R_SYS_BITS_MASK) == (b)) || \ + ((((x) >> (R_SYS_BITS_SIZE*3)) & R_SYS_BITS_MASK) == (c)) \ +) + + #define HEAPTYPE(x) \ static x* x##_new(x n) {\ x *m = malloc(sizeof (x));\ diff --git a/libr/include/r_util/r_bitmap.h b/libr/include/r_util/r_bitmap.h index eed5a032ffb4e..8843802bd6376 100644 --- a/libr/include/r_util/r_bitmap.h +++ b/libr/include/r_util/r_bitmap.h @@ -10,7 +10,7 @@ #else -#if R_SYS_BITS == 4 +#if R_SYS_BITS == 32 #define BITWORD_BITS_SHIFT 5 #define RBitword ut32 #else diff --git a/libr/include/r_util/r_str.h b/libr/include/r_util/r_str.h index 0b784fe7a6bdf..bcb7ab3f40e95 100644 --- a/libr/include/r_util/r_str.h +++ b/libr/include/r_util/r_str.h @@ -155,7 +155,7 @@ R_API char *r_str_newf(const char *fmt, ...) R_PRINTF_CHECK(1, 2); R_API char *r_str_newvf(const char *fmt, va_list ap); R_API int r_str_distance(const char *a, const char *b); R_API char *r_str_newlen(const char *str, int len); -R_API const char *r_str_sysbits(const int v); +R_API const char *r_str_sysbits(const RSysBits v); R_API char *r_str_trunc_ellipsis(const char *str, int len); R_API const char *r_str_bool(bool b); R_API bool r_str_is_true(const char *s); diff --git a/libr/include/r_util/r_sys.h b/libr/include/r_util/r_sys.h index ef377864f84f8..116f6c4fd0b34 100644 --- a/libr/include/r_util/r_sys.h +++ b/libr/include/r_util/r_sys.h @@ -15,33 +15,6 @@ extern "C" { #endif -// no need for an enum or type here, its just 1:1 -// XXX deprecate because it's confusing, we can use the new RSYS_BITS_PACK macros -#define R_SYS_BITS_8 1 -#define R_SYS_BITS_16 2 -#define R_SYS_BITS_32 4 -#define R_SYS_BITS_64 8 -#define R_SYS_BITS_24 24 -#define R_SYS_BITS_27 16 -#define R_SYS_BITS_4 32 -#define R_SYS_BITS_12 64 - -typedef ut64 RSysBits; - -#define R_SYS_BITS_SIZE 8 -#define R_SYS_BITS_MASK 0xff -#define R_SYS_BITS_PACK(x) (RSysBits)(x) -#define R_SYS_BITS_PACK1(x) (RSysBits)(x) -#define R_SYS_BITS_PACK2(x,y) (RSysBits)((x) | ((y)<> R_SYS_BITS_SIZE) & R_SYS_BITS_MASK) == (y)) || \ - ((((x) >> (R_SYS_BITS_SIZE*2)) & R_SYS_BITS_MASK) == (y)) || \ - ((((x) >> (R_SYS_BITS_SIZE*3)) & R_SYS_BITS_MASK) == (y)) \ -) - typedef struct { char *sysname; char *nodename; diff --git a/libr/io/p/io_ptrace.c b/libr/io/p/io_ptrace.c index 78af3c2c1d5c1..9ea0f0289006c 100644 --- a/libr/io/p/io_ptrace.c +++ b/libr/io/p/io_ptrace.c @@ -333,10 +333,11 @@ static char *__system(RIO *io, RIODesc *fd, const char *cmd) { // TODO: do not set pid if attach fails? iop->pid = iop->tid = pid; } + return NULL; } else { - io->cb_printf ("%d\n", iop->pid); + return r_str_newf ("%d", iop->pid); + // io->cb_printf ("%d\n", iop->pid); } - return r_str_newf ("%d", iop->pid); } } else { show_help (); diff --git a/libr/main/rafind2.c b/libr/main/rafind2.c index 277b9d79713ba..fb02b26784d85 100644 --- a/libr/main/rafind2.c +++ b/libr/main/rafind2.c @@ -500,7 +500,7 @@ R_API int r_main_rafind2(int argc, const char **argv) { char *colon = strchr (arg, ':'); char *comma = NULL; ut8 buf[8] = {0}; - int size = (R_SYS_BITS & R_SYS_BITS_64)? 8: 4; + int size = R_SYS_BITS_CHECK (R_SYS_BITS, 64)? 8: 4; ut64 value, min_value = 0, max_value = 0; if (colon) { diff --git a/libr/main/ragg2.c b/libr/main/ragg2.c index 43b5c310cdf5a..c808bbd40c820 100644 --- a/libr/main/ragg2.c +++ b/libr/main/ragg2.c @@ -185,7 +185,7 @@ static int openfile(const char *f, int x) { #endif return fd; } -#define ISEXEC (fmt!='r') +#define ISEXEC (fmt != 'r') R_API int r_main_ragg2(int argc, const char **argv) { const char *file = NULL; @@ -209,7 +209,7 @@ R_API int r_main_ragg2(int argc, const char **argv) { const char *encoder = NULL; const char *eggprg = NULL; char *sequence = NULL; - int bits = (R_SYS_BITS & R_SYS_BITS_64)? 64: 32; + int bits = R_SYS_BITS_CHECK (R_SYS_BITS, 64)? 64: 32; int fmt = 0; const char *ofile = NULL; int ofileauto = 0; diff --git a/libr/main/rasm2.c b/libr/main/rasm2.c index a665ecbdbff48..5abb489c7c6c3 100644 --- a/libr/main/rasm2.c +++ b/libr/main/rasm2.c @@ -21,7 +21,7 @@ static void __as_set_archbits(RAsmState *as) { const char *arch = as->a->config->arch; // R_SYS_ARCH; r_asm_use (as->a, arch); r_anal_use (as->anal, arch); - int sysbits = (R_SYS_BITS & R_SYS_BITS_64)? 64: 32; + const int sysbits = R_SYS_BITS_CHECK (R_SYS_BITS, 64)? 64: 32; r_asm_set_bits (as->a, sysbits); r_anal_set_bits (as->anal, sysbits); } @@ -724,7 +724,7 @@ R_API int r_main_rasm2(int argc, const char *argv[]) { bool hexwords = false; ut64 offset = 0; int fd = -1, dis = 0, bin = 0, ret = 0, c, whatsop = 0; - int bits = (R_SYS_BITS & R_SYS_BITS_64)? 64: 32; + int bits = R_SYS_BITS_CHECK (R_SYS_BITS, 64)? 64: 32; int help = 0; ut64 len = 0, idx = 0, skip = 0; bool analinfo = false; diff --git a/libr/util/str.c b/libr/util/str.c index 89905305d8dd9..445aecf3dfbed 100644 --- a/libr/util/str.c +++ b/libr/util/str.c @@ -136,18 +136,17 @@ R_API int r_str_bits(char *strout, const ut8 *buf, int len, const char *bitz) { return j; } -R_API const char *r_str_sysbits(const int v) { +R_API const char *r_str_sysbits(const RSysBits v) { switch (v) { - case R_SYS_BITS_4: return "4"; - case R_SYS_BITS_8: return "8"; - case R_SYS_BITS_4 | R_SYS_BITS_8: return "4,8"; - case R_SYS_BITS_16: return "16"; - case R_SYS_BITS_27: return "27"; - case R_SYS_BITS_32: return "32"; - case R_SYS_BITS_64: return "64"; - case R_SYS_BITS_16 | R_SYS_BITS_32: return "16,32"; - case R_SYS_BITS_16 | R_SYS_BITS_32 | R_SYS_BITS_64: return "16,32,64"; - case R_SYS_BITS_32 | R_SYS_BITS_64: return "32,64"; + case R_SYS_BITS_PACK (4): return "4"; + case R_SYS_BITS_PACK (8): return "8"; + case R_SYS_BITS_PACK (16): return "16"; + case R_SYS_BITS_PACK (32): return "32"; + case R_SYS_BITS_PACK (64): return "64"; + case R_SYS_BITS_PACK2 (4, 8): return "4,8"; + case R_SYS_BITS_PACK2 (16, 64): return "16,32"; + case R_SYS_BITS_PACK2 (32, 64): return "32,64"; + case R_SYS_BITS_PACK3 (16, 32, 64): return "16,32,64"; } return "?"; } diff --git a/libr/util/sys.c b/libr/util/sys.c index 8beef5cdadfc0..50ad337a3ffb9 100644 --- a/libr/util/sys.c +++ b/libr/util/sys.c @@ -1079,6 +1079,7 @@ R_API int r_sys_run(const ut8 *buf, int len) { return ret; } +// TODO. maybe this should be moved into socket/run? R_API int r_sys_run_rop(const ut8 *buf, int len) { #if USE_FORK int st; diff --git a/shlr/winkd/winkd.c b/shlr/winkd/winkd.c index e2624f4eb8323..b45aa5e14d19b 100644 --- a/shlr/winkd/winkd.c +++ b/shlr/winkd/winkd.c @@ -106,7 +106,7 @@ bool winkd_lock_leave(WindCtx *ctx) { } int winkd_get_bits(WindCtx *ctx) { - return ctx->is_x64 ? R_SYS_BITS_64 : R_SYS_BITS_32; + return ctx->is_x64 ? R_SYS_BITS_PACK (64): R_SYS_BITS_PACK (32); } int winkd_get_cpus(WindCtx *ctx) { diff --git a/test/unit/test_egg.c b/test/unit/test_egg.c index 491754722cdad..968c269626279 100644 --- a/test/unit/test_egg.c +++ b/test/unit/test_egg.c @@ -7,7 +7,7 @@ #if __linux__ && __x86_64__ const char *arch = R_SYS_ARCH; const char *os = R_EGG_OS_NAME; -const int bits = (R_SYS_BITS & R_SYS_BITS_64)? 64: 32; +const int bits = R_SYS_BITS_CHECK (R_SYS_BITS, 64)? 64: 32; const char program[] = " \ read@syscall(0); \ write@syscall(1); \