From 13888a7933b18b6b5360a162338f075ea2ae3ba0 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 26 Aug 2014 10:42:42 -0600 Subject: Can't dump loaded code unless we first establish the mappings --- nuttx/binfmt/elf.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/nuttx/binfmt/elf.c b/nuttx/binfmt/elf.c index cff3463a7..98924a6a8 100644 --- a/nuttx/binfmt/elf.c +++ b/nuttx/binfmt/elf.c @@ -47,9 +47,13 @@ #include #include + +#include #include #include +#include "libelf/libelf.h" + #ifdef CONFIG_ELF /**************************************************************************** @@ -170,6 +174,47 @@ static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo) # define elf_dumploadinfo(i) #endif +/**************************************************************************** + * Name: elf_dumpentrypt + ****************************************************************************/ + +#ifdef CONFIG_ELF_DUMPBUFFER +static void elf_dumpentrypt(FAR struct binary_s *binp, + FAR struct elf_loadinfo_s *loadinfo) +{ +#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. elf_addrenv_select() will + * temporarily instantiate that address space. + */ + + ret = elf_addrenv_select(loadinfo); + if (ret < 0) + { + bdbg("ERROR: elf_addrenv_select() failed: %d\n", ret); + return; + } +#endif + + elf_dumpbuffer("Entry code", (FAR const uint8_t*)binp->entrypt, + MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512)); + +#ifdef CONFIG_ARCH_ADDRENV + /* Restore the original address environment */ + + ret = elf_addrenv_restore(loadinfo); + if (ret < 0) + { + bdbg("ERROR: elf_addrenv_restore() failed: %d\n", ret); + } +#endif +} +#else +# define elf_dumpentrypt(b,l) +#endif + /**************************************************************************** * Name: elf_loadbinary * @@ -179,7 +224,7 @@ static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo) * ****************************************************************************/ -static int elf_loadbinary(struct binary_s *binp) +static int elf_loadbinary(FAR struct binary_s *binp) { struct elf_loadinfo_s loadinfo; /* Contains globals for libelf */ int ret; @@ -256,9 +301,7 @@ static int elf_loadbinary(struct binary_s *binp) binp->addrenv = loadinfo.addrenv; #endif - elf_dumpbuffer("Entry code", (FAR const uint8_t*)binp->entrypt, - MIN(loadinfo.textsize - loadinfo.ehdr.e_entry, 512)); - + elf_dumpentrypt(binp, &loadinfo); elf_uninit(&loadinfo); return OK; -- cgit v1.2.3