From 84d39a8d9a77c26e432e2f34b2a53db2ff0e4f7f Mon Sep 17 00:00:00 2001 From: Tiago Medicci Serrano Date: Fri, 5 Jul 2024 16:15:53 -0300 Subject: [PATCH] binfmt/libelf: Enable ELF loader if text heap read is word-aligned The ELF loader needs to load the app into the memory before executing it from the same location. As expected, this memory space should be able to execute code. For architectures containing data and instruction buses, the instruction bus may not be able to be accessed in a non-aligned way, which is usually required when copying data to that location. Eventually, this same memory space can be accessed through the data bus, using different address ranges. This commit enables accessing the memory through the data bus to copy the app's data before executing it when `CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ` is enabled. --- binfmt/libelf/libelf_load.c | 12 ++++++++++-- libs/libc/machine/xtensa/arch_elf.c | 3 ++- libs/libc/modlib/modlib_load.c | 12 ++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/binfmt/libelf/libelf_load.c b/binfmt/libelf/libelf_load.c index 2a6be23bc6b..44986b8f5ee 100644 --- a/binfmt/libelf/libelf_load.c +++ b/binfmt/libelf/libelf_load.c @@ -96,7 +96,11 @@ static void elf_elfsize(FAR struct elf_loadinfo_s *loadinfo) * able */ - if ((shdr->sh_flags & SHF_WRITE) != 0) + if ((shdr->sh_flags & SHF_WRITE) != 0 +#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ + || (shdr->sh_flags & SHF_EXECINSTR) == 0 +#endif + ) { datasize = _ALIGN_UP(datasize, shdr->sh_addralign); datasize += ELF_ALIGNUP(shdr->sh_size); @@ -200,7 +204,11 @@ static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo) * able */ - if ((shdr->sh_flags & SHF_WRITE) != 0) + if ((shdr->sh_flags & SHF_WRITE) != 0 +#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ + || (shdr->sh_flags & SHF_EXECINSTR) == 0 +#endif + ) { pptr = &data; } diff --git a/libs/libc/machine/xtensa/arch_elf.c b/libs/libc/machine/xtensa/arch_elf.c index b1dcf6abe15..3e7558b2de6 100644 --- a/libs/libc/machine/xtensa/arch_elf.c +++ b/libs/libc/machine/xtensa/arch_elf.c @@ -29,6 +29,7 @@ #include #include +#include #include /**************************************************************************** @@ -200,7 +201,7 @@ int up_relocateadd(const Elf32_Rela *rel, const Elf32_Sym *sym, break; case R_XTENSA_SLOT0_OP: - p = (unsigned char *)addr; + p = (unsigned char *)up_textheap_data_address((void *)addr); if (is_l32r(p)) { /* Xtensa ISA: diff --git a/libs/libc/modlib/modlib_load.c b/libs/libc/modlib/modlib_load.c index d6772d5cca5..8e1200863f7 100644 --- a/libs/libc/modlib/modlib_load.c +++ b/libs/libc/modlib/modlib_load.c @@ -112,7 +112,11 @@ static void modlib_elfsize(FAR struct mod_loadinfo_s *loadinfo) * able */ - if ((shdr->sh_flags & SHF_WRITE) != 0) + if ((shdr->sh_flags & SHF_WRITE) != 0 +#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ + || (shdr->sh_flags & SHF_EXECINSTR) == 0 +#endif + ) { datasize = _ALIGN_UP(datasize, shdr->sh_addralign); datasize += ELF_ALIGNUP(shdr->sh_size); @@ -214,7 +218,11 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) * able */ - if ((shdr->sh_flags & SHF_WRITE) != 0) + if ((shdr->sh_flags & SHF_WRITE) != 0 +#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ + || (shdr->sh_flags & SHF_EXECINSTR) == 0 +#endif + ) { pptr = &data; }