From 275b17bfeee3fd20b41d0ec4c1907e00565aa47c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sat, 2 Apr 2022 09:49:06 +0200 Subject: [PATCH] Fix host endian dependency in rz-gg rz-gg -dDnN now always write numbers in little endian instead of depending on the host's endian, to fix tests on ppc. Making it target-dependent will require further refactoring (see comments). --- librz/egg/egg.c | 7 +++++++ librz/include/rz_egg.h | 1 + librz/main/rz-gg.c | 34 +++++++++++----------------------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/librz/egg/egg.c b/librz/egg/egg.c index b5509e3128..8f97174f0e 100644 --- a/librz/egg/egg.c +++ b/librz/egg/egg.c @@ -588,6 +588,13 @@ RZ_API int rz_egg_patch(RzEgg *egg, int off, const ut8 *buf, int len) { return true; } +RZ_API bool rz_egg_patch_num(RzEgg *egg, int off, ut64 num, ut32 bits) { + rz_return_val_if_fail(egg && bits <= 64, false); + ut8 buf[8] = { 0 }; + rz_write_ble(buf, num, egg->endian, bits); + return rz_egg_patch(egg, off, buf, bits / 8); +} + RZ_API void rz_egg_finalize(RzEgg *egg) { struct egg_patch_t *ep; RzListIter *iter; diff --git a/librz/include/rz_egg.h b/librz/include/rz_egg.h index f434d902c1..e31e07f1a4 100644 --- a/librz/include/rz_egg.h +++ b/librz/include/rz_egg.h @@ -218,6 +218,7 @@ RZ_API void rz_egg_append(RzEgg *egg, const char *src); RZ_API int rz_egg_run(RzEgg *egg); RZ_API int rz_egg_run_rop(RzEgg *egg); RZ_API int rz_egg_patch(RzEgg *egg, int off, const ut8 *b, int l); +RZ_API bool rz_egg_patch_num(RzEgg *egg, int off, ut64 val, ut32 bits); RZ_API void rz_egg_finalize(RzEgg *egg); /* rz_egg_Cfile.c */ diff --git a/librz/main/rz-gg.c b/librz/main/rz-gg.c index a94cefd308..bf93950316 100644 --- a/librz/main/rz-gg.c +++ b/librz/main/rz-gg.c @@ -192,39 +192,27 @@ RZ_API int rz_main_rz_gg(int argc, const char **argv) { } free(arg); } break; - case 'n': { - ut32 n = rz_num_math(NULL, opt.arg); - append = 1; - rz_egg_patch(egg, -1, (const ut8 *)&n, 4); - } break; + case 'n': case 'N': { ut64 n = rz_num_math(NULL, opt.arg); - rz_egg_patch(egg, -1, (const ut8 *)&n, 8); + // TODO: support big endian too + // (this is always little because rz_egg_setup is further below) + rz_egg_patch_num(egg, -1, n, c == 'N' ? 64 : 32); append = 1; } break; - case 'd': { - ut32 off, n; - char *p = strchr(opt.arg, ':'); - if (p) { - *p = 0; - off = rz_num_math(NULL, opt.arg); - n = rz_num_math(NULL, p + 1); - *p = ':'; - // TODO: honor endianness here - rz_egg_patch(egg, off, (const ut8 *)&n, 4); - } else { - eprintf("Missing colon in -d\n"); - } - } break; + case 'd': case 'D': { char *p = strchr(opt.arg, ':'); if (p) { + *p = '\0'; ut64 n, off = rz_num_math(NULL, opt.arg); + *p = ':'; n = rz_num_math(NULL, p + 1); - // TODO: honor endianness here - rz_egg_patch(egg, off, (const ut8 *)&n, 8); + // TODO: support big endian too + // (this is always little because rz_egg_setup is further below) + rz_egg_patch_num(egg, off, n, c == 'D' ? 64 : 32); } else { - eprintf("Missing colon in -d\n"); + eprintf("Missing colon in -%c\n", c); } } break; case 'S':