From fa41033c78479975189c885ee731cff42f3f7bdc Mon Sep 17 00:00:00 2001 From: Adrian Warecki Date: Tue, 14 Nov 2023 13:54:57 +0100 Subject: [PATCH 1/2] rimage: module: Fix calculation of segment size in file If a given output segment type did not contain any input section (was empty), its size was incorrectly calculated as 1 and then rounded to the page size (4096). This resulted in an unnecessary segment in a output file. Signed-off-by: Adrian Warecki --- tools/rimage/src/module.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tools/rimage/src/module.c b/tools/rimage/src/module.c index b150e246ca8c..ddd22aea1dee 100644 --- a/tools/rimage/src/module.c +++ b/tools/rimage/src/module.c @@ -111,15 +111,12 @@ void module_print_zones(const struct module *module) { fprintf(stdout, "\n\tTotals\tStart\t\tEnd\t\tSize"); - fprintf(stdout, "\n\tTEXT\t0x%8.8x\t0x%8.8x\t0x%x\n", - module->text.start, module->text.end, - module->text.end - module->text.start); - fprintf(stdout, "\tDATA\t0x%8.8x\t0x%8.8x\t0x%x\n", - module->data.start, module->data.end, - module->data.end - module->data.start); - fprintf(stdout, "\tBSS\t0x%8.8x\t0x%8.8x\t0x%x\n\n", - module->bss.start, module->bss.end, - module->bss.end - module->bss.start); + fprintf(stdout, "\n\tTEXT\t0x%8.8x\t0x%8.8x\t0x%zx\n", + module->text.start, module->text.end, module->text.file_size); + fprintf(stdout, "\tDATA\t0x%8.8x\t0x%8.8x\t0x%zx\n", + module->data.start, module->data.end, module->data.file_size); + fprintf(stdout, "\tBSS\t0x%8.8x\t0x%8.8x\t0x%zx\n\n", + module->bss.start, module->bss.end, module->bss.file_size); } /** @@ -252,7 +249,7 @@ static void sections_info_add(struct module_sections_info *info, struct module_s */ static void sections_info_finalize(struct module_sections_info *info) { - info->file_size = info->end - info->start; + info->file_size = (info->end > info->start) ? info->end - info->start : 0; /* file sizes round up to nearest page */ info->file_size = (info->file_size + MAN_PAGE_SIZE - 1) & ~(MAN_PAGE_SIZE - 1); From 194a810e8f062c7fc2d424f4e1a165bba4581ccd Mon Sep 17 00:00:00 2001 From: Adrian Warecki Date: Tue, 14 Nov 2023 14:19:26 +0100 Subject: [PATCH 2/2] rimage: manifest: Improved handling of empty RODATA segment If the RODATA segment does not contain any data, its type is marked as empty. Cleared the readonly flag because this segment also contains .data sections. Signed-off-by: Adrian Warecki --- tools/rimage/src/manifest.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/tools/rimage/src/manifest.c b/tools/rimage/src/manifest.c index 84176418df22..f8b7e38f60f3 100644 --- a/tools/rimage/src/manifest.c +++ b/tools/rimage/src/manifest.c @@ -240,20 +240,21 @@ static int man_get_module_manifest(struct image *image, struct manifest_module * segment->flags.r.load = 1; segment->flags.r.readonly = 1; segment->flags.r.code = 1; + segment->flags.r.type = SOF_MAN_SEGMENT_TEXT; /* data segment */ segment = &man_module->segment[SOF_MAN_SEGMENT_RODATA]; segment->flags.r.contents = 1; segment->flags.r.alloc = 1; segment->flags.r.load = 1; - segment->flags.r.readonly = 1; + segment->flags.r.readonly = 0; /* rodata segment contains also writtable data */ segment->flags.r.data = 1; - segment->flags.r.type = 1; + segment->flags.r.type = SOF_MAN_SEGMENT_RODATA; /* bss segment */ segment = &man_module->segment[SOF_MAN_SEGMENT_BSS]; segment->flags.r.alloc = 1; - segment->flags.r.type = 2; + segment->flags.r.type = SOF_MAN_SEGMENT_BSS; fprintf(stdout, " Entry point 0x%8.8x\n", man_module->entry_point); @@ -366,22 +367,28 @@ static int man_module_create(struct image *image, struct manifest_module *module if (err) return err; - /* data section */ - man_module->segment[SOF_MAN_SEGMENT_RODATA].v_base_addr = module->file.data.start; - man_module->segment[SOF_MAN_SEGMENT_RODATA].file_offset = module->foffset + - module->text_fixup_size; /* file_size is already aligned to MAN_PAGE_SIZE */ pages = module->file.data.file_size / MAN_PAGE_SIZE; man_module->segment[SOF_MAN_SEGMENT_RODATA].flags.r.length = pages; - - /* Copy data sections content */ - err = man_copy_elf_sections(image, module, &man_module->segment[SOF_MAN_SEGMENT_RODATA], - module->file.data.first_section); - if (err) - return err; + if (pages) { + man_module->segment[SOF_MAN_SEGMENT_RODATA].v_base_addr = module->file.data.start; + man_module->segment[SOF_MAN_SEGMENT_RODATA].file_offset = module->foffset + + module->text_fixup_size; + /* Copy data sections content */ + err = man_copy_elf_sections(image, module, + &man_module->segment[SOF_MAN_SEGMENT_RODATA], + module->file.data.first_section); + if (err) + return err; + } else { + man_module->segment[SOF_MAN_SEGMENT_RODATA].v_base_addr = 0; + man_module->segment[SOF_MAN_SEGMENT_RODATA].file_offset = 0; + man_module->segment[SOF_MAN_SEGMENT_RODATA].flags.ul = 0; + man_module->segment[SOF_MAN_SEGMENT_RODATA].flags.r.type = SOF_MAN_SEGMENT_EMPTY; + } /* bss is last */