Skip to content

Commit

Permalink
Adding a new section keeps the array sorted and merge sections if nec…
Browse files Browse the repository at this point in the history
…essary
  • Loading branch information
BlockoS committed Dec 26, 2024
1 parent b167e74 commit c5ee7f9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 62 deletions.
2 changes: 0 additions & 2 deletions cli/etripator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
61 changes: 42 additions & 19 deletions section.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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; i<arr->count; 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) && (i<arr->count); ) {
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) {
Expand All @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions section/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/data/bank0_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "code",
"page": "0",
"logical" : "e000",
"size": "505",
"size": "504",
"mpr": ["ff", "f8", 0, 0, 0, 0, 0, 0 ]
},

Expand Down
79 changes: 41 additions & 38 deletions test/section.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }
Expand All @@ -121,55 +121,58 @@ 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;
ret = section_load(&arr, "./data/bank0_0.json");

munit_assert_int(ret, !=, 0);
munit_assert_int(arr.count, ==, 4);
/*
section_sort(section, count[0]);
for(i=0; i<count[0]; i++) {
munit_assert_string_equal(bank0[0][i].name, section[i].name);
munit_assert_int(bank0[0][i].type, ==, section[i].type);
munit_assert_uint8(bank0[0][i].page, ==, section[i].page);
munit_assert_uint16(bank0[0][i].logical, ==, section[i].logical);
munit_assert_uint32(bank0[0][i].offset, ==, section[i].offset);
munit_assert_int32(bank0[0][i].size, ==, section[i].size);
munit_assert_memory_equal(8, bank0[0][i].mpr, section[i].mpr);
munit_assert_int32(bank0[0][i].data.element_size, ==, section[i].data.element_size);
munit_assert_int32(bank0[0][i].data.elements_per_line, ==, section[i].data.elements_per_line);
munit_assert_string_equal(bank0[0][i].output, section[i].output);

for(i=0; i<arr.count; i++) {
munit_assert_string_equal(bank0_0[i].name, arr.data[i].name);
munit_assert_int(bank0_0[i].type, ==, arr.data[i].type);
munit_assert_uint8(bank0_0[i].page, ==, arr.data[i].page);
munit_assert_uint16(bank0_0[i].logical, ==, arr.data[i].logical);
munit_assert_uint32(bank0_0[i].offset, ==, arr.data[i].offset);
munit_assert_int32(bank0_0[i].size, ==, arr.data[i].size);
munit_assert_memory_equal(8, bank0_0[i].mpr, arr.data[i].mpr);
munit_assert_int32(bank0_0[i].data.element_size, ==, arr.data[i].data.element_size);
munit_assert_int32(bank0_0[i].data.elements_per_line, ==, arr.data[i].data.elements_per_line);
munit_assert_string_equal(bank0_0[i].output, arr.data[i].output);
}
*/

ret = section_load(&arr, "./data/bank0_1.json");
munit_assert_int(ret, !=, 0);
munit_assert_int(arr.count, ==, 13);
/*
section_sort(section, count[1]);
for(i=0, j=0; j<2; j++) {
for(k=0; i<count[j]; i++, k++) {
munit_assert_string_equal(bank0[j][k].name, section[i].name);
munit_assert_int(bank0[j][k].type, ==, section[i].type);
munit_assert_uint8(bank0[j][k].page, ==, section[i].page);
munit_assert_uint16(bank0[j][k].logical, ==, section[i].logical);
munit_assert_uint32(bank0[j][k].offset, ==, section[i].offset);
munit_assert_int32(bank0[j][k].size, ==, section[i].size);
munit_assert_memory_equal(8, bank0[j][k].mpr, section[i].mpr);
munit_assert_int32(bank0[j][k].data.element_size, ==, section[i].data.element_size);
munit_assert_int32(bank0[j][k].data.elements_per_line, ==, section[i].data.elements_per_line);
munit_assert_string_equal(bank0[j][k].output, section[i].output);
}
}
section_delete(section, count[1]);
*/
for(i=0, k=0; k<4; i++, k++) {
munit_assert_string_equal(bank0_0[k].name, arr.data[i].name);
munit_assert_int(bank0_0[k].type, ==, arr.data[i].type);
munit_assert_uint8(bank0_0[k].page, ==, arr.data[i].page);
munit_assert_uint16(bank0_0[k].logical, ==, arr.data[i].logical);
munit_assert_uint32(bank0_0[k].offset, ==, arr.data[i].offset);
munit_assert_int32(bank0_0[k].size, ==, arr.data[i].size);
munit_assert_memory_equal(8, bank0_0[k].mpr, arr.data[i].mpr);
munit_assert_int32(bank0_0[k].data.element_size, ==, arr.data[i].data.element_size);
munit_assert_int32(bank0_0[k].data.elements_per_line, ==, arr.data[i].data.elements_per_line);
munit_assert_string_equal(bank0_0[k].output, arr.data[i].output);
}
for(k=0; k<9; i++, k++) {
munit_assert_string_equal(bank0_1[k].name, arr.data[i].name);
munit_assert_int(bank0_1[k].type, ==, arr.data[i].type);
munit_assert_uint8(bank0_1[k].page, ==, arr.data[i].page);
munit_assert_uint16(bank0_1[k].logical, ==, arr.data[i].logical);
munit_assert_uint32(bank0_1[k].offset, ==, arr.data[i].offset);
munit_assert_int32(bank0_1[k].size, ==, arr.data[i].size);
munit_assert_memory_equal(8, bank0_1[k].mpr, arr.data[i].mpr);
munit_assert_int32(bank0_1[k].data.element_size, ==, arr.data[i].data.element_size);
munit_assert_int32(bank0_1[k].data.elements_per_line, ==, arr.data[i].data.elements_per_line);
munit_assert_string_equal(bank0_1[k].output, arr.data[i].output);
}

section_array_delete(&arr);
return MUNIT_OK;
}
Expand Down

0 comments on commit c5ee7f9

Please sign in to comment.