From f55ccb9e1020f7a7ab14e4cfbfb3968862a8a893 Mon Sep 17 00:00:00 2001 From: Tiago Medicci Serrano Date: Fri, 1 Nov 2024 14:18:31 -0300 Subject: [PATCH] libs/modlib: Load data using `up_textheap_data_address` Some chips have different memory addressing spaces for the same region. This is true, for instance, for ESP32-S3: the same memory region can be accessed using the data bus or the data bus using different address ranges. The instruction bus, however, requires word-aligned access. That being said, it is recommended to use the data bus while copying sections to the text heap to avoid any illegal access using the instruction bus address which will be later used to run the program. --- libs/libc/modlib/modlib_load.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/libs/libc/modlib/modlib_load.c b/libs/libc/modlib/modlib_load.c index 98e7690c82073..0048f793a99a6 100644 --- a/libs/libc/modlib/modlib_load.c +++ b/libs/libc/modlib/modlib_load.c @@ -57,6 +57,13 @@ #define _ALIGN_UP(v, a) (((v) + ((a) - 1)) & ~((a) - 1)) +#ifdef CONFIG_ARCH_USE_TEXT_HEAP +# define buffer_data_address(p) \ + (FAR uint8_t *)up_textheap_data_address((FAR void *)p) +#else +# define buffer_data_address(p) ((FAR uint8_t *)p) +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -343,7 +350,8 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) { if (phdr->p_flags & PF_X) { - ret = modlib_read(loadinfo, text, phdr->p_filesz, + ret = modlib_read(loadinfo, buffer_data_address(text), + phdr->p_filesz, phdr->p_offset); } else @@ -441,8 +449,8 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) /* Read the section data from sh_offset to the memory region */ - ret = modlib_read(loadinfo, *pptr, shdr->sh_size, - shdr->sh_offset); + ret = modlib_read(loadinfo, buffer_data_address(*pptr), + shdr->sh_size, shdr->sh_offset); if (ret < 0) { berr("ERROR: Failed to read section %d: %d\n", i, ret);