From 2931d1e85fcc74d8b7ed4b11a0956a546db1a2d5 Mon Sep 17 00:00:00 2001 From: anjiahao Date: Tue, 16 Jul 2024 11:31:07 +0800 Subject: [PATCH] modlib:support modlib can load exec Signed-off-by: anjiahao --- libs/libc/modlib/modlib_load.c | 13 +++++++++---- libs/libc/modlib/modlib_verify.c | 3 ++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/libs/libc/modlib/modlib_load.c b/libs/libc/modlib/modlib_load.c index 152f477100e..8c9a9621149 100644 --- a/libs/libc/modlib/modlib_load.c +++ b/libs/libc/modlib/modlib_load.c @@ -328,6 +328,11 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) * execution */ + if (shdr->sh_size == 0) + { + continue; + } + if ((shdr->sh_flags & SHF_ALLOC) == 0) { continue; @@ -360,7 +365,7 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) } *pptr = (FAR uint8_t *)_ALIGN_UP((uintptr_t)*pptr, - shdr->sh_addralign); + shdr->sh_addralign); } /* SHT_NOBITS indicates that there is no data in the file for the @@ -370,7 +375,7 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) if (shdr->sh_type != SHT_NOBITS) { #ifdef CONFIG_MODLIB_LOADTO_LMA - ret = modlib_vma2lma(loadinfo, shdr, *pptr); + ret = modlib_vma2lma(loadinfo, shdr, (FAR Elf_Addr *)pptr); if (ret < 0) { berr("ERROR: Failed to convert addr %d: %d\n", i, ret); @@ -393,7 +398,7 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) * section must be cleared. */ - else + else if (*pptr != NULL) { memset(*pptr, 0, shdr->sh_size); } @@ -509,7 +514,7 @@ int modlib_load(FAR struct mod_loadinfo_s *loadinfo) } #endif } - else + else if (loadinfo->ehdr.e_type == ET_DYN) { loadinfo->textalloc = (uintptr_t)lib_memalign(loadinfo->textalign, loadinfo->textsize + diff --git a/libs/libc/modlib/modlib_verify.c b/libs/libc/modlib/modlib_verify.c index 4f59c438a8f..efd947bf3e9 100644 --- a/libs/libc/modlib/modlib_verify.c +++ b/libs/libc/modlib/modlib_verify.c @@ -81,7 +81,8 @@ int modlib_verifyheader(FAR const Elf_Ehdr *ehdr) /* Verify that this is a relocatable file */ - if (ehdr->e_type != ET_REL && ehdr->e_type != ET_DYN) + if (ehdr->e_type != ET_REL && ehdr->e_type != ET_DYN && + ehdr->e_type != ET_EXEC) { berr("ERROR: Not a relocatable file: e_type=%d\n", ehdr->e_type); return -EINVAL;