Skip to content

Commit

Permalink
modlib:add new dump api to modlib
Browse files Browse the repository at this point in the history
Signed-off-by: anjiahao <[email protected]>
  • Loading branch information
anjiahao1 authored and xiaoxiang781216 committed Oct 5, 2024
1 parent a9cb28c commit a613bcb
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 5 deletions.
34 changes: 34 additions & 0 deletions include/nuttx/lib/modlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,40 @@ int modlib_registry_foreach(mod_callback_t callback, FAR void *arg);

void modlib_freesymtab(FAR struct module_s *modp);

/****************************************************************************
* Name: modlib_dumploadinfo
*
* Description:
* Dump the load information to debug output.
*
****************************************************************************/

#ifdef CONFIG_DEBUG_BINFMT_INFO
void modlib_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo);
#else
# define modlib_dumploadinfo(i)
#endif

/****************************************************************************
* Name: modlib_dumpmodule
****************************************************************************/

#ifdef CONFIG_DEBUG_BINFMT_INFO
void modlib_dumpmodule(FAR struct module_s *modp);
#else
# define modlib_dumpmodule(m)
#endif

/****************************************************************************
* Name: elf_dumpentrypt
****************************************************************************/

#ifdef CONFIG_MODLIB_DUMPBUFFER
void modlib_dumpentrypt(FAR struct mod_loadinfo_s *loadinfo);
#else
# define modlib_dumpentrypt(l)
#endif

/****************************************************************************
* Name: modlib_insert
*
Expand Down
104 changes: 99 additions & 5 deletions libs/libc/modlib/modlib_insert.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@
#include <nuttx/lib/modlib.h>

/****************************************************************************
* Private Functions
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: modlib_dumploadinfo
*
* Description:
* Dump the load information to debug output.
*
****************************************************************************/

#ifdef CONFIG_DEBUG_BINFMT_INFO
static void modlib_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
void modlib_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
{
int i;

Expand Down Expand Up @@ -101,14 +105,104 @@ static void modlib_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
}
}
}
#else
# define modlib_dumploadinfo(i)

/****************************************************************************
* Name: modlib_dumpmodule
****************************************************************************/

void modlib_dumpmodule(FAR struct module_s *modp)
{
binfo("Module:\n");
binfo(" modname: %s\n", modp->modname);
binfo(" textalloc: %08lx\n", (long)modp->textalloc);
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
binfo(" dataalloc: %08lx\n", (long)modp->dataalloc);
binfo(" textsize: %ld\n", (long)modp->textsize);
#endif

#ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
binfo(" sectalloc: %p\n", modp->sectalloc);
binfo(" nsect: %ld\n", (long)modp->nsect);
for (int i = 0; i < modp->nsect; i++)
{
binfo(" sectalloc[%d]: %p\n", i, modp->sectalloc[i]);
}

#endif

#if CONFIG_MODLIB_MAXDEPEND > 0
binfo(" dependents: %d\n", modp->dependents);
for (int i = 0; i < modp->dependents; i++)
{
binfo("%d %s\n", i, modp->dependencies[i]->modname);
modlib_dumpmodule(modp->dependencies[i]);
}
#endif

binfo(" finiarr: %08lx\n", (long)modp->finiarr);
binfo(" nfini: %d\n", modp->nfini);
}

#endif
/****************************************************************************
* Public Functions
* Name: elf_dumpentrypt
****************************************************************************/

#ifdef CONFIG_MODLIB_DUMPBUFFER
void modlib_dumpentrypt(FAR struct mod_loadinfo_s *loadinfo)
{
FAR const uint8_t *entry;
#ifdef CONFIG_ARCH_ADDRENV
int ret;

/* If CONFIG_ARCH_ADDRENV=y, then the loaded ELF lies in a virtual address
* space that may not be in place now. modlib_addrenv_select() will
* temporarily instantiate that address space.
*/

if (loadinfo->addrenv != NULL)
{
ret = modlib_addrenv_select(loadinfo);
if (ret < 0)
{
berr("ERROR: modlib_addrenv_select() failed: %d\n", ret);
return;
}
}
#endif

if (loadinfo->ehdr.e_type == ET_REL)
{
entry = (FAR const uint8_t *)
((uintptr_t)loadinfo->textalloc + loadinfo->ehdr.e_entry);
}
else if (loadinfo->ehdr.e_type == ET_EXEC)
{
entry = (FAR const uint8_t *)loadinfo->ehdr.e_entry;
}
else
{
entry = (FAR const uint8_t *)loadinfo->textalloc;
}

modlib_dumpbuffer("Entry code", entry,
MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));

#ifdef CONFIG_ARCH_ADDRENV
/* Restore the original address environment */

if (loadinfo->addrenv != NULL)
{
ret = modlib_addrenv_restore(loadinfo);
if (ret < 0)
{
berr("ERROR: modlib_addrenv_restore() failed: %d\n", ret);
}
}
#endif
}
#endif

/****************************************************************************
* Name: modlib_insert
*
Expand Down

0 comments on commit a613bcb

Please sign in to comment.