Skip to content

Commit 693dff1

Browse files
authored
Move arch config into function (#5902)
1 parent 1587651 commit 693dff1

File tree

30 files changed

+973
-733
lines changed

30 files changed

+973
-733
lines changed

librz/arch/analysis.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name) {
252252
}
253253
plugin_fini(analysis);
254254
analysis->cur = h;
255+
256+
// always set the cpu as the name of the arch.
257+
rz_analysis_set_cpu(analysis, name);
255258
if (h->init && !h->init(&analysis->plugin_data)) {
256259
RZ_LOG_ERROR("analysis plugin '%s' failed to initialize.\n", h->name);
257260
rz_iterator_free(it);
@@ -312,12 +315,17 @@ RZ_API bool rz_analysis_set_reg_profile(RzAnalysis *analysis) {
312315

313316
static bool analysis_set_os(RzAnalysis *analysis, const char *os) {
314317
rz_return_val_if_fail(analysis, false);
315-
if (!os || !*os) {
318+
if (RZ_STR_ISEMPTY(os)) {
316319
os = RZ_SYS_OS;
317320
}
318-
free(analysis->os);
321+
322+
if (analysis->os && RZ_STR_EQ(analysis->os, os)) {
323+
return true;
324+
}
325+
326+
RZ_FREE(analysis->os);
319327
analysis->os = rz_str_dup(os);
320-
rz_type_db_set_os(analysis->typedb, os);
328+
rz_type_db_set_os(analysis->typedb, analysis->os);
321329
rz_type_db_reload(analysis->typedb, analysis->sdb_types_path);
322330
return true;
323331
}

librz/arch/asm.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ RZ_API RzAsm *rz_asm_new(void) {
281281
return NULL;
282282
}
283283
a->dataalign = 1;
284-
a->bits = RZ_SYS_BITS;
284+
a->bits = RZ_SYS_BITS << 3;
285285
a->bitshift = 0;
286286
a->syntax = RZ_ASM_SYNTAX_INTEL;
287287
a->sdb_opcodes_path = rz_path_new();
@@ -449,6 +449,24 @@ static void remove_plugin_config(RZ_BORROW RzCore *core, const char *plugin_name
449449
ht_sp_delete(core->plugin_configs, plugin_name);
450450
}
451451

452+
static ut32 asm_get_first_default_bits(RzAsmPlugin *h) {
453+
if (!h) {
454+
return RZ_SYS_BITS << 3;
455+
}
456+
457+
if (h->bits & 32) {
458+
return 32;
459+
} else if (h->bits & 64) {
460+
return 64;
461+
} else if (h->bits & 16) {
462+
return 16;
463+
} else if (h->bits & 8) {
464+
return 8;
465+
}
466+
467+
return RZ_SYS_BITS << 3;
468+
}
469+
452470
// TODO: this can be optimized using rz_str_hash()
453471
/**
454472
* \brief Puts an Asm plugin in use and disables the previous one.
@@ -488,6 +506,8 @@ RZ_API bool rz_asm_use(RzAsm *a, RZ_NULLABLE const char *name) {
488506
}
489507
free(opcodes_dir);
490508
}
509+
510+
rz_asm_set_cpu(a, NULL);
491511
if (h->init && !h->init(&a->plugin_data)) {
492512
RZ_LOG_ERROR("asm plugin '%s' failed to initialize.\n", h->name);
493513
rz_iterator_free(iter);
@@ -502,6 +522,9 @@ RZ_API bool rz_asm_use(RzAsm *a, RZ_NULLABLE const char *name) {
502522
}
503523
a->cur = h;
504524
rz_iterator_free(iter);
525+
RZ_FREE(a->features);
526+
RZ_FREE(a->platforms);
527+
a->bits = asm_get_first_default_bits(h);
505528
return true;
506529
}
507530
}

librz/arch/platform_profile.c

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -170,43 +170,6 @@ RZ_API bool rz_platform_load_profile_sdb(RZ_NONNULL RzPlatformTarget *t, RZ_NONN
170170
return sdb_load_arch_profile_by_path(t, path);
171171
}
172172

173-
static bool is_cpu_valid(const char *cpu_dir, const char *cpu) {
174-
RzList *files = rz_sys_dir(cpu_dir);
175-
if (!files) {
176-
return false;
177-
}
178-
RzListIter *it;
179-
char *filename = NULL;
180-
char *arch_cpu = NULL;
181-
182-
rz_list_foreach (files, it, filename) {
183-
char *cpu_name = NULL;
184-
if (!strcmp(filename, "..") || !strcmp(filename, "..")) {
185-
continue;
186-
}
187-
arch_cpu = rz_str_ndup(filename, strlen(filename) - 4);
188-
if (!arch_cpu) {
189-
continue;
190-
}
191-
cpu_name = strchr(arch_cpu, '-');
192-
if (!cpu_name) {
193-
free(arch_cpu);
194-
continue;
195-
}
196-
cpu_name[0] = '\0';
197-
if (!strcmp(cpu_name + 1, cpu)) {
198-
rz_list_free(files);
199-
free(arch_cpu);
200-
return true;
201-
}
202-
203-
free(arch_cpu);
204-
}
205-
206-
rz_list_free(files);
207-
return false;
208-
}
209-
210173
/**
211174
* \brief Initializes RzPlatformProfile by loading the path to the SDB file
212175
* of the CPU profile
@@ -223,17 +186,23 @@ RZ_API bool rz_platform_profiles_init(RZ_NULLABLE RzPlatformTarget *t, RZ_NULLAB
223186
if (!cpu_reload_needed(t, cpu, arch)) {
224187
return false;
225188
}
189+
190+
if (cpu && RZ_STR_EQ(cpu, "avr")) {
191+
cpu = "ATmega8";
192+
}
193+
194+
if (t->arch && RZ_STR_EQ(t->arch, arch) &&
195+
t->cpu && RZ_STR_EQ(t->cpu, cpu)) {
196+
// already loaded.
197+
return true;
198+
}
199+
226200
char buf[50];
227201
char *path = rz_file_path_join(cpus_dir, rz_strf(buf, "%s-%s.sdb", arch, cpu));
228202
if (!path) {
229203
return false;
230204
}
231-
if (!is_cpu_valid(cpus_dir, cpu)) {
232-
if (!strcmp(arch, "avr")) {
233-
free(path);
234-
path = rz_file_path_join(cpus_dir, "avr-ATmega8.sdb");
235-
}
236-
}
205+
237206
free(t->cpu);
238207
free(t->arch);
239208
t->cpu = rz_str_dup(cpu);

librz/arch/platform_target_index.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ RZ_API bool rz_platform_target_index_load_sdb(RZ_NONNULL RzPlatformTargetIndex *
128128
* \param platform reference to the selected platform (value of `asm.platform`)
129129
* \param platforms_dir reference to the directory containing platform files
130130
*/
131-
RZ_API bool rz_platform_target_index_init(RzPlatformTargetIndex *t, RZ_NONNULL const char *arch, RZ_NONNULL const char *cpu,
132-
const char *platform, RZ_NONNULL const char *platforms_dir) {
131+
RZ_API bool rz_platform_target_index_init(RzPlatformTargetIndex *t, RZ_NONNULL const char *arch, RZ_NONNULL const char *cpu, const char *platform, RZ_NONNULL const char *platforms_dir) {
133132
if (RZ_STR_ISEMPTY(platform)) {
134133
return true;
135134
}

librz/bin/format/elf/elf_info.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -884,42 +884,46 @@ static int get_bits_mips(ELFOBJ *bin) {
884884
return get_bits_mips_common(mips_type);
885885
}
886886

887+
static inline bool arch_is(ELFOBJ *bin, ut64 machine_id) {
888+
return bin->ehdr.e_machine == machine_id;
889+
}
890+
887891
static inline bool arch_is_nanomips(ELFOBJ *bin) {
888-
return bin->ehdr.e_machine == EM_IMG1;
892+
return arch_is(bin, EM_IMG1);
889893
}
890894

891-
static bool arch_is_mips(ELFOBJ *bin) {
895+
static inline bool arch_is_mips(ELFOBJ *bin) {
892896
return bin->ehdr.e_machine == EM_MIPS ||
893897
bin->ehdr.e_machine == EM_MIPS_RS3_LE ||
894898
bin->ehdr.e_machine == EM_MIPS_X ||
895899
arch_is_nanomips(bin);
896900
}
897901

898-
static bool arch_is_h8xx(ELFOBJ *bin) {
902+
static inline bool arch_is_h8xx(ELFOBJ *bin) {
899903
return bin->ehdr.e_machine == EM_H8_300 ||
900904
bin->ehdr.e_machine == EM_H8_300H ||
901905
bin->ehdr.e_machine == EM_H8S ||
902906
bin->ehdr.e_machine == EM_H8_500;
903907
}
904908

905-
static bool arch_is_sparc(ELFOBJ *bin) {
909+
static inline bool arch_is_sparc(ELFOBJ *bin) {
906910
return bin->ehdr.e_machine == EM_SPARC ||
907911
bin->ehdr.e_machine == EM_SPARC32PLUS ||
908912
bin->ehdr.e_machine == EM_SPARCV9;
909913
}
910914

911-
static bool arch_is_arm(ELFOBJ *bin) {
915+
static inline bool arch_is_arm(ELFOBJ *bin) {
912916
return bin->ehdr.e_machine == EM_ARM || bin->ehdr.e_machine == EM_AARCH64;
913917
}
914918

915-
static bool arch_is_arcompact(ELFOBJ *bin) {
919+
static inline bool arch_is_arcompact(ELFOBJ *bin) {
916920
return bin->ehdr.e_machine == EM_ARC_A5 ||
917921
bin->ehdr.e_machine == EM_ARC_COMPACT3_64 ||
918922
bin->ehdr.e_machine == EM_ARC_COMPACT3;
919923
}
920924

921-
static bool arch_is_parisc(ELFOBJ *bin) {
922-
return bin->ehdr.e_machine == EM_PARISC;
925+
static inline bool arch_is_parisc(ELFOBJ *bin) {
926+
return arch_is(bin, EM_PARISC);
923927
}
924928

925929
static bool arch_is_riscv(ELFOBJ *bin) {
@@ -2389,8 +2393,9 @@ bool Elf_(rz_bin_elf_is_static)(RZ_NONNULL ELFOBJ *bin) {
23892393
int Elf_(rz_bin_elf_get_bits)(RZ_NONNULL ELFOBJ *bin) {
23902394
rz_return_val_if_fail(bin, 0);
23912395

2392-
/* Hack for ARCompact */
2393-
if (arch_is_arcompact(bin)) {
2396+
if (arch_is_arcompact(bin) ||
2397+
arch_is_h8xx(bin) ||
2398+
arch_is(bin, EM_MSP430)) {
23942399
return 16;
23952400
}
23962401

@@ -2399,10 +2404,6 @@ int Elf_(rz_bin_elf_get_bits)(RZ_NONNULL ELFOBJ *bin) {
23992404
return get_bits_mips(bin);
24002405
}
24012406

2402-
if (arch_is_h8xx(bin)) {
2403-
return 16;
2404-
}
2405-
24062407
/* Hack for Thumb */
24072408
if (Elf_(rz_bin_elf_is_arm_binary_supporting_thumb)(bin)) {
24082409
if (!Elf_(rz_bin_elf_is_static)(bin) && has_thumb_symbol(bin)) {

librz/bin/p/bin_avr.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,12 +734,21 @@ static RzBinInfo *avr_info(RzBinFile *bf) {
734734
return NULL;
735735
}
736736

737+
const char *board = "ATmel (unknown)";
738+
const char *cpu = "ATmega8";
739+
740+
if (rom->board) {
741+
board = rom->board->name;
742+
cpu = rom->board->cpu;
743+
}
744+
737745
bi->file = rz_str_dup(bf->file);
738746
bi->type = rz_str_dup("ROM");
739-
bi->machine = rz_str_dup(rom->board ? rom->board->name : "ATmel (unknown)");
747+
bi->machine = rz_str_dup(board);
740748
bi->os = rz_str_dup("avr usermode");
741749
bi->has_va = false;
742750
bi->arch = rz_str_dup("avr");
751+
bi->cpu = rz_str_dup(cpu);
743752
bi->bits = 8;
744753
return bi;
745754
}

librz/bin/p/bin_mach0.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ static RzBinInfo *mach0_info(RzBinFile *bf) {
475475
ret->machine = MACH0_(get_cpusubtype)(bf->o->bin_obj);
476476
ret->type = MACH0_(get_filetype)(bf->o->bin_obj);
477477
ret->big_endian = MACH0_(is_big_endian)(bf->buf);
478-
ret->bits = 32;
478+
ret->bits = MACH0_(get_bits)(bf->o->bin_obj);
479479
if (bf && bf->o && bf->o->bin_obj) {
480480
ret->has_crypto = ((struct MACH0_(obj_t) *)
481481
bf->o->bin_obj)

librz/bin/p/bin_qnx.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ static RzBinInfo *qnx_info(RzBinFile *bf) {
240240
ret->os = rz_str_dup("any");
241241
ret->subsystem = rz_str_dup("any");
242242
ret->lang = "C/C++";
243+
ret->bits = 32;
243244
ret->signature = true;
244245
return ret;
245246
}

librz/config/config.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ RZ_API const char *rz_config_node_type(RzConfigNode *node) {
170170
RZ_API RZ_BORROW RzConfigNode *rz_config_set_cb(RZ_BORROW RzConfig *cfg, const char *name, const char *value, RzConfigCallback cb) {
171171
RzConfigNode *node = rz_config_set(cfg, name, value);
172172
if (node && (node->setter = cb)) {
173-
if (!cb(cfg->user, node)) {
174-
return NULL;
173+
if (!node->setter(cfg->user, node)) {
174+
return node;
175175
}
176176
}
177177
return node;
@@ -181,7 +181,7 @@ RZ_API RZ_BORROW RzConfigNode *rz_config_set_i_cb(RZ_BORROW RzConfig *cfg, const
181181
RzConfigNode *node = rz_config_set_i(cfg, name, ivalue);
182182
if (node && (node->setter = cb)) {
183183
if (!node->setter(cfg->user, node)) {
184-
return NULL;
184+
return node;
185185
}
186186
}
187187
return node;

0 commit comments

Comments
 (0)