From 1ed33709412fe1b4a8f1a7cfe2f345f37eaccc8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20J=2E=20Saraiva?= Date: Wed, 27 Mar 2024 16:59:41 +0000 Subject: [PATCH] Clear uninitialized sections in mips64_load_elf_image. Elf sections of type SHT_NOBITS do not contain data in the file. It was either reading junk data or failing to read. --- stable/mips64.c | 15 ++++++++++----- unstable/mips64.c | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/stable/mips64.c b/stable/mips64.c index b84e874fd..6278ae329 100644 --- a/stable/mips64.c +++ b/stable/mips64.c @@ -1013,11 +1013,16 @@ int mips64_load_elf_image(cpu_mips_t *cpu,char *filename,int skip_load, clen = m_min(clen,remain); - if (fread((u_char *)haddr,clen,1,bfd) != 1) { - perror("load_elf_image: fread"); - elf_end(img_elf); - fclose(bfd); - return(-1); + if (shdr->sh_type == SHT_NOBITS) { + // section with uninitialized data, zero it + memset((u_char *)haddr, 0, clen); + } else { + if (fread((u_char *)haddr,clen,1,bfd) != 1) { + perror("load_elf_image: fread"); + elf_end(img_elf); + fclose(bfd); + return(-1); + } } vaddr += clen; diff --git a/unstable/mips64.c b/unstable/mips64.c index 66a1174be..445c7fcfa 100644 --- a/unstable/mips64.c +++ b/unstable/mips64.c @@ -1096,11 +1096,16 @@ int mips64_load_elf_image(cpu_mips_t *cpu,char *filename,int skip_load, clen = m_min(clen,remain); - if (fread((u_char *)haddr,clen,1,bfd) != 1) { - perror("load_elf_image: fread"); - elf_end(img_elf); - fclose(bfd); - return(-1); + if (shdr->sh_type == SHT_NOBITS) { + // section with uninitialized data, zero it + memset((u_char *)haddr, 0, clen); + } else { + if (fread((u_char *)haddr,clen,1,bfd) != 1) { + perror("load_elf_image: fread"); + elf_end(img_elf); + fclose(bfd); + return(-1); + } } vaddr += clen;