elf:use elf symbol to parse attribute

Parse application attributes (stacksize, priority, and uid/gid/mode under
CONFIG_SCHED_USER_IDENTITY) from absolute symbols (nx_stacksize,
nx_priority, nx_uid, nx_gid, nx_mode) embedded in the ELF at link time,
falling back to defaults when the symbols are absent.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2026-07-15 16:10:08 +08:00 committed by Alan C. Assis
parent c8b71df614
commit 0178ceab19
2 changed files with 79 additions and 2 deletions

View file

@ -98,6 +98,7 @@ static int elf_loadbinary(FAR struct binary_s *binp,
int nexports)
{
struct mod_loadinfo_s loadinfo;
Elf_Sym sym;
int ret;
binfo("Loading file: %s\n", filename);
@ -173,7 +174,57 @@ static int elf_loadbinary(FAR struct binary_s *binp,
/* Return the load information */
binp->stacksize = CONFIG_ELF_STACKSIZE;
ret = libelf_findsymbol(&loadinfo, "nx_stacksize", &sym);
if (ret == 0)
{
binp->stacksize = sym.st_value;
}
else
{
binp->stacksize = CONFIG_ELF_STACKSIZE;
}
ret = libelf_findsymbol(&loadinfo, "nx_priority", &sym);
if (ret == 0)
{
binp->priority = sym.st_value;
}
else
{
binp->priority = SCHED_PRIORITY_DEFAULT;
}
#ifdef CONFIG_SCHED_USER_IDENTITY
ret = libelf_findsymbol(&loadinfo, "nx_uid", &sym);
if (ret == 0)
{
binp->uid = sym.st_value;
}
else
{
binp->uid = 0;
}
ret = libelf_findsymbol(&loadinfo, "nx_gid", &sym);
if (ret == 0)
{
binp->gid = sym.st_value;
}
else
{
binp->gid = 0;
}
ret = libelf_findsymbol(&loadinfo, "nx_mode", &sym);
if (ret == 0)
{
binp->mode = sym.st_value;
}
else
{
binp->mode = 0;
}
#endif
/* Add the ELF allocation to the alloc[] only if there is no address
* environment. If there is an address environment, it will automatically

View file

@ -147,6 +147,31 @@ function(nuttx_add_application)
if(NOT "${CMAKE_LD}" MATCHES "gcc$")
set(USE_LINKER True)
endif()
if(STACKSIZE)
set(SYMBOL_STACKSIZE $<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--defsym
nx_stacksize=${STACKSIZE})
endif()
if(PRIORITY)
if(PRIORITY STREQUAL "SCHED_PRIORITY_DEFAULT")
set(PRIORITY "0")
endif()
set(SYMBOL_PRIORITY $<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--defsym
nx_priority=${PRIORITY})
endif()
if(CONFIG_SCHED_USER_IDENTITY)
if(UID)
set(SYMBOL_UID $<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--defsym
nx_uid=${UID})
endif()
if(GID)
set(SYMBOL_GID $<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--defsym
nx_gid=${GID})
endif()
if(MODE)
set(SYMBOL_MODE $<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--defsym
nx_mode=${MODE})
endif()
endif()
add_custom_command(
TARGET ${TARGET}
POST_BUILD
@ -158,7 +183,8 @@ function(nuttx_add_application)
# add global ELF link option if m&kernel link
$<$<OR:$<BOOL:${KERNEL_ELF_MODE}>,$<BOOL:${LOADABLE_ELF_MODE}>>:$<TARGET_PROPERTY:nuttx_global,NUTTX_ELF_APP_LINK_OPTIONS>>
# add local link option last
${LINK_FLAGS}
${LINK_FLAGS} ${SYMBOL_STACKSIZE} ${SYMBOL_PRIORITY} ${SYMBOL_UID}
${SYMBOL_GID} ${SYMBOL_MODE}
# link startup obj if m&kernel link
$<$<AND:$<TARGET_EXISTS:STARTUP_OBJS>,$<NOT:$<BOOL:${DYNLIB_ELF_MODE}>>>:$<TARGET_OBJECTS:STARTUP_OBJS>>
$<$<NOT:$<BOOL:${USE_LINKER}>>:-Wl,>--start-group