libs/libc/symtab: fix bogus symbol size under SYMTAB_ORDEREDBYNAME

allsyms_lookup() derived a symbol's size from the physically next table
entry, assuming address order. Under CONFIG_SYMTAB_ORDEREDBYNAME the
table is sorted by name instead, producing a huge bogus size in
%pS/backtrace output.

Scan for the closest larger address instead of relying on table order.

Signed-off-by: liang.huang <liang.huang@houmo.ai>
Assisted-by: Claude Code:claude-sonnet-5
This commit is contained in:
liang.huang 2026-07-25 13:30:13 +08:00
parent 1de02e49d4
commit c92684faec

View file

@ -75,7 +75,38 @@ allsyms_lookup(FAR const char *name, FAR void *value,
}
}
if (symbol && symbol != &g_allsyms[g_nallsyms - 1])
if (symbol == NULL)
{
*size = 0;
return NULL;
}
#ifdef CONFIG_SYMTAB_ORDEREDBYNAME
/* g_allsyms is sorted by name here, not by value, so the physically
* next table entry is not necessarily the next symbol by address.
* Scan the real symbols (index 1..g_nallsyms - 2; 0 and g_nallsyms - 1
* are the boundary sentinels) for the smallest address greater than
* this symbol's, falling back to the high sentinel's now-meaningful
* value if none is closer.
*/
{
FAR const struct symtab_s *next = &g_allsyms[g_nallsyms - 1];
int i;
for (i = 1; i < g_nallsyms - 1; i++)
{
if (g_allsyms[i].sym_value > symbol->sym_value &&
g_allsyms[i].sym_value < next->sym_value)
{
next = &g_allsyms[i];
}
}
*size = next->sym_value - symbol->sym_value;
}
#else
if (symbol != &g_allsyms[g_nallsyms - 1])
{
*size = (symbol + 1)->sym_value - symbol->sym_value;
}
@ -83,6 +114,7 @@ allsyms_lookup(FAR const char *name, FAR void *value,
{
*size = 0;
}
#endif
return symbol;
}