From 9aa82975b1da7e66f488a26461633f2984d581de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 25 Oct 2022 22:23:39 +0200 Subject: [PATCH] Bugifx: Fix CRCs not being written to section --- src/elfpatch.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/elfpatch.c b/src/elfpatch.c index dd1b72a..32c1903 100644 --- a/src/elfpatch.c +++ b/src/elfpatch.c @@ -633,17 +633,10 @@ int elf_patch_write_crcs_to_section(elfpatch_handle_t *ep, const char *section, } } - /* Update ELF file */ - if (ep->readonly) { - print_debug("DRY RUN: File will not be updated\n"); - ret = 0; - } else { - if (elf_update(ep->elf, ELF_C_WRITE) < 0) { - print_err("Error writing ELF file: %s\n", elf_errmsg(-1)); - } else { - ret = 0; - } - } + /* Flag section data as invalid to trigger rewrite. + * This is needed to to the forced memory layout + */ + elf_flagdata(output_sec_data, ELF_C_SET, ELF_F_DIRTY); ret_err: return ret; @@ -653,6 +646,17 @@ void elf_patch_close_and_free(elfpatch_handle_t *ep) { ret_if_ep_err(ep); + if (ep->elf) { + /* Update ELF file */ + if (ep->readonly) { + print_debug("DRY RUN: File will not be updated\n"); + } else { + if (elf_update(ep->elf, ELF_C_WRITE) < 0) { + print_err("Error writing ELF file: %s\n", elf_errmsg(-1)); + } + } + } + if (ep->elf) elf_end(ep->elf);