From c5ee7f9860d8fcf52dbd4d72e1cd9668a8f76f61 Mon Sep 17 00:00:00 2001 From: MooZ Date: Wed, 25 Dec 2024 23:03:42 +0100 Subject: [PATCH] Adding a new section keeps the array sorted and merge sections if necessary --- cli/etripator.c | 2 -- section.c | 61 ++++++++++++++++++++++---------- section/load.c | 2 -- test/data/bank0_0.json | 2 +- test/section.c | 79 ++++++++++++++++++++++-------------------- 5 files changed, 84 insertions(+), 62 deletions(-) diff --git a/cli/etripator.c b/cli/etripator.c index 06f460d..d04df25 100644 --- a/cli/etripator.c +++ b/cli/etripator.c @@ -208,8 +208,6 @@ int main(int argc, const char **argv) { /* Data will be loaded during section disassembly */ } -// [todo] section_sort(section, section_count); - CommentRepository *comments_repository = comment_repository_create(); /* Load comments */ if(NULL != options.comments_in) { diff --git a/section.c b/section.c index 23fba99..2e87698 100644 --- a/section.c +++ b/section.c @@ -156,11 +156,14 @@ static int section_overlap(const Section *a, const Section *b) { b = a; a = tmp; } - if(b->logical <= (a->logical + a->size)) { - if(a->type == b->type) { - ret = 1; + + if(a->type != b->type) { + ret = (b->logical < (a->logical + a->size)) ? -1 : 0; + } else if(b->logical <= (a->logical + a->size)) { + if(a->type == SECTION_TYPE_DATA) { + ret = (a->data.type == b->data.type) ? 1 : -1; } else { - ret = -1; + ret = 1; } } } @@ -193,22 +196,38 @@ int section_array_add(SectionArray *arr, const Section* in) { int ret = -1; size_t i; - // Search for overlapping section. - for(i=0; icount; i++) { - int overlap = section_overlap(&arr->data[i], in); - if(overlap == 1) { - section_merge(&arr->data[i], in); - INFO_MSG("Section %s has been merged with %s!", arr->data[i].name, in->name); - break; - } else if(overlap == -1) { - WARNING_MSG("Section %s and %s overlaps! %x %x.%x", arr->data[i].name, in->name); - break; + bool insert = false; + + // Find the slot where the section will be inserted or merged + for(i=0; (!insert) && (icount); ) { + if(in->page > arr->data[i].page) { + // ... + } else if(in->page < arr->data[i].page) { + insert = true; + } else { + int overlap = section_overlap(&arr->data[i], in); + if(overlap == 1) { + section_merge(&arr->data[i], in); + INFO_MSG("Section %s has been merged with %s!", arr->data[i].name, in->name); + ret = 1; + break; + } else if (overlap == -1) { + WARNING_MSG("Section %s and %s overlaps!", arr->data[i].name, in->name); + } else if(overlap == 0) { + // ... + } + if(in->offset < arr->data[i].offset) { + insert = true; + } + } + if(!insert) { + i++; } } - if(i >= arr->count) { + if((i >= arr->count) || insert) { // Check if we need to expand section array buffer - if(i >= arr->capacity) { + if(arr->count >= arr->capacity) { size_t n = arr->capacity + 4U; Section *ptr = realloc(arr->data, n*sizeof(Section)); if(ptr == NULL) { @@ -222,9 +241,13 @@ int section_array_add(SectionArray *arr, const Section* in) { ret = 1; } - // Append new section. - if(ret) { - arr->data[arr->count++] = *in; + size_t ii = i; + if(ret > 0) { + for(size_t j=arr->count; j>i; j--) { + arr->data[j] = arr->data[j-1]; + } + arr->count++; + arr->data[i] = *in; } } return ret; diff --git a/section/load.c b/section/load.c index 1f93f81..00f25b8 100644 --- a/section/load.c +++ b/section/load.c @@ -342,8 +342,6 @@ bool section_load(SectionArray *arr, const char *filename) { Section *ptr; const char* key; - section_array_reset(arr); - json_t* root = json_load_file(filename, 0, &err); if(root == NULL) { ERROR_MSG("Failed to parse %s:%d:%d: %s", filename, err.line, err.column, err.text); diff --git a/test/data/bank0_0.json b/test/data/bank0_0.json index 06b09ab..af12643 100644 --- a/test/data/bank0_0.json +++ b/test/data/bank0_0.json @@ -4,7 +4,7 @@ "type": "code", "page": "0", "logical" : "e000", - "size": "505", + "size": "504", "mpr": ["ff", "f8", 0, 0, 0, 0, 0, 0 ] }, diff --git a/test/section.c b/test/section.c index d7318b7..45eccab 100644 --- a/test/section.c +++ b/test/section.c @@ -104,7 +104,7 @@ MunitResult section_load_test(const MunitParameter params[], void* fixture) { (void)fixture; static const Section bank0_0[4] = { - { "cdbios_functions", SECTION_TYPE_CODE, 0, 0xe000, 0x0000, 0x505, { 0xff, 0xf8, 0, 0, 0, 0, 0, 0 }, "syscard.asm", { DATA_TYPE_UNKNOWN, 8, 16 } }, + { "cdbios_functions", SECTION_TYPE_CODE, 0, 0xe000, 0x0000, 0x504, { 0xff, 0xf8, 0, 0, 0, 0, 0, 0 }, "syscard.asm", { DATA_TYPE_UNKNOWN, 8, 16 } }, { "unknown.0", SECTION_TYPE_DATA, 0, 0xe504, 0x0504, 0x05, { 0xff, 0xf8, 0, 0, 0, 0, 0, 0 }, "syscard.asm", { DATA_TYPE_UNKNOWN, 8, 16 } }, { "ex_colorcmd.impl", SECTION_TYPE_CODE, 0, 0xe509, 0x0509, 0xce, { 0xff, 0xf8, 0, 0, 0, 0, 0, 0 }, "syscard.asm", { DATA_TYPE_UNKNOWN, 8, 16 } }, { "unknown.1", SECTION_TYPE_DATA, 0, 0xe5d7, 0x05d7, 0x03, { 0xff, 0xf8, 0, 0, 0, 0, 0, 0 }, "syscard.asm", { DATA_TYPE_UNKNOWN, 8, 16 } } @@ -121,10 +121,9 @@ MunitResult section_load_test(const MunitParameter params[], void* fixture) { { "unknown.3", SECTION_TYPE_DATA, 0, 0xfec2, 0x1ec2, 0x134, { 0xff, 0xf8, 0, 0, 0, 0, 0, 0 }, "syscard.asm", { DATA_TYPE_UNKNOWN, 8, 16 } }, { "irq_vectors", SECTION_TYPE_DATA, 0, 0xfff6, 0x1ff6, 0x0a, { 0xff, 0xf8, 0, 0, 0, 0, 0, 0 }, "syscard.asm", { DATA_TYPE_HEX, 2, 1 } } }; - - static const Section* bank0[2] = { bank0_0, bank0_1 }; - + SectionArray arr = {0}; + section_array_reset(&arr); int i, j, k; int ret; @@ -132,44 +131,48 @@ MunitResult section_load_test(const MunitParameter params[], void* fixture) { munit_assert_int(ret, !=, 0); munit_assert_int(arr.count, ==, 4); -/* - section_sort(section, count[0]); - - for(i=0; i