modlib:support modlib can load PIC elf

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao
2024-07-03 15:33:48 +08:00
committed by GUIDINGLI
parent cfc90ad1f3
commit 112b6fd9a5
8 changed files with 174 additions and 30 deletions

View File

@@ -28,7 +28,7 @@ config BINFMT_LOADABLE
Automatically selected if a loadable binary format is selected.
config PIC
bool
bool "Executable elf position-independent support"
default n
---help---
Automatically selected if the binary format requires position

View File

@@ -33,6 +33,7 @@
#include <nuttx/arch.h>
#include <nuttx/binfmt/binfmt.h>
#include <nuttx/kmalloc.h>
#ifdef CONFIG_ELF
@@ -119,7 +120,7 @@ static int elf_loadbinary(FAR struct binary_s *binp,
/* Bind the program to the exported symbol table */
if (loadinfo.ehdr.e_type == ET_REL)
if (loadinfo.ehdr.e_type == ET_REL || loadinfo.gotindex >= 0)
{
ret = modlib_bind(&binp->mod, &loadinfo, exports, nexports);
if (ret != 0)
@@ -207,6 +208,23 @@ static int elf_loadbinary(FAR struct binary_s *binp,
#endif
modlib_dumpentrypt(&loadinfo);
#ifdef CONFIG_PIC
if (loadinfo.gotindex >= 0)
{
FAR struct dspace_s *dspaces = kmm_zalloc(sizeof(struct dspace_s));
if (dspaces == NULL)
{
ret = -ENOMEM;
goto errout_with_load;
}
dspaces->region = (FAR void *)loadinfo.shdr[loadinfo.gotindex].sh_addr;
dspaces->crefs = 1;
binp->picbase = (FAR void *)dspaces;
}
#endif
modlib_uninitialize(&loadinfo);
return OK;