This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push: new e384a6a625 libs/modlib.c: Set VMA for empty and unallocated sections e384a6a625 is described below commit e384a6a6255f051d8661752f0602792157d94fdf Author: Ville Juven <ville.ju...@unikie.com> AuthorDate: Wed Oct 16 15:02:14 2024 +0300 libs/modlib.c: Set VMA for empty and unallocated sections This fixes issue where empty and unallocated sections are left without a VMA. Some relocations depend on the section VMA being set even if there is no data there, as the binary can refer to the symbols. Linker defined symbols do not contain data -> they can produce empty sections. This issue is seen when building a loadable file which declares _sctors / _sdtors linker defined symbols for ctor/dtor sections which are empty. crt0 references these symbols, so they need to be relocated, but the section VMA is not set -> they go outside of the addressable range of the user binary causing a potential crash. --- libs/libc/modlib/modlib_load.c | 45 +++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/libs/libc/modlib/modlib_load.c b/libs/libc/modlib/modlib_load.c index 25308cd3e0..cb3faa7b1e 100644 --- a/libs/libc/modlib/modlib_load.c +++ b/libs/libc/modlib/modlib_load.c @@ -128,7 +128,7 @@ static int modlib_section_alloc(FAR struct mod_loadinfo_s *loadinfo, } } - return 0; + return OK; } #endif @@ -266,7 +266,7 @@ static int modlib_vma2lma(FAR struct mod_loadinfo_s *loadinfo, shdr->sh_offset <= phdr->p_offset + phdr->p_filesz) { *lma = phdr->p_paddr + shdr->sh_addr - phdr->p_vaddr; - return 0; + return OK; } } @@ -274,6 +274,39 @@ static int modlib_vma2lma(FAR struct mod_loadinfo_s *loadinfo, } #endif +/**************************************************************************** + * Name: modlib_set_emptysect_vma + * + * Description: + * Set VMA for empty and unallocated sections, some relocations might + * depend on this. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static void modlib_set_emptysect_vma(FAR struct mod_loadinfo_s *loadinfo, + int section) +{ + FAR Elf_Shdr *shdr = &loadinfo->shdr[section]; + + /* Set the section as data or text, depending on SHF_WRITE */ + + if ((shdr->sh_flags & SHF_WRITE) != 0 +#ifdef CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ + || (shdr->sh_flags & SHF_EXECINSTR) == 0 +#endif + ) + { + shdr->sh_addr = loadinfo->datastart; + } + else + { + shdr->sh_addr = loadinfo->textalloc; + } +} + /**************************************************************************** * Name: modlib_loadfile * @@ -339,13 +372,11 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) * execution */ - if (shdr->sh_size == 0) + if ((shdr->sh_flags & SHF_ALLOC) == 0 || shdr->sh_size == 0) { - continue; - } + /* Set the VMA regardless */ - if ((shdr->sh_flags & SHF_ALLOC) == 0) - { + modlib_set_emptysect_vma(loadinfo, i); continue; }