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.
This commit is contained in:
Ville Juven 2024-10-16 15:02:14 +03:00 committed by Xiang Xiao
parent ef350afd28
commit e384a6a625
1 changed files with 38 additions and 7 deletions

View File

@ -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;
}