Skip to content

Commit

Permalink
binfmt/elf: Fix the minor style issue
Browse files Browse the repository at this point in the history
and remove the unused macros and unnecessary cast

Signed-off-by: Xiang Xiao <[email protected]>
  • Loading branch information
xiaoxiang781216 authored and acassis committed Aug 27, 2023
1 parent 98bb669 commit cb8df39
Show file tree
Hide file tree
Showing 48 changed files with 254 additions and 309 deletions.
4 changes: 2 additions & 2 deletions binfmt/binfmt_execmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static void exec_ctors(FAR void *arg)

for (i = 0; i < binp->nctors; i++)
{
binfo("Calling ctor %d at %p\n", i, (FAR void *)ctor);
binfo("Calling ctor %d at %p\n", i, ctor);

(*ctor)();
ctor++;
Expand Down Expand Up @@ -272,7 +272,7 @@ int exec_module(FAR struct binary_s *binp,

if (binp->nctors > 0)
{
nxtask_starthook(tcb, exec_ctors, (FAR void *)binp);
nxtask_starthook(tcb, exec_ctors, binp);
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion binfmt/binfmt_globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <nuttx/config.h>

#include <nuttx/binfmt/binfmt.h>
#include "binfmt.h"

#ifndef CONFIG_BINFMT_DISABLE

Expand Down
10 changes: 5 additions & 5 deletions binfmt/binfmt_unloadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static inline int exec_dtors(FAR struct binary_s *binp)
/* Instantiate the address environment containing the destructors */

#ifdef CONFIG_ARCH_ADDRENV
ret = addrenv_select(&binp->addrenv);
ret = addrenv_select(binp->addrenv, &binp->oldenv);
if (ret < 0)
{
berr("ERROR: addrenv_select() failed: %d\n", ret);
Expand All @@ -81,7 +81,7 @@ static inline int exec_dtors(FAR struct binary_s *binp)

for (i = 0; i < binp->ndtors; i++)
{
binfo("Calling dtor %d at %p\n", i, (FAR void *)dtor);
binfo("Calling dtor %d at %p\n", i, dtor);

(*dtor)();
dtor++;
Expand All @@ -90,7 +90,7 @@ static inline int exec_dtors(FAR struct binary_s *binp)
/* Restore the address environment */

#ifdef CONFIG_ARCH_ADDRENV
return addrenv_restore();
return addrenv_restore(binp->oldenv);
#else
return OK;
#endif
Expand Down Expand Up @@ -169,12 +169,12 @@ int unload_module(FAR struct binary_s *binp)
#if defined(CONFIG_ARCH_USE_TEXT_HEAP)
if (i == 0)
{
up_textheap_free((FAR void *)binp->alloc[i]);
up_textheap_free(binp->alloc[i]);
}
else
#endif
{
kumm_free((FAR void *)binp->alloc[i]);
kumm_free(binp->alloc[i]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion binfmt/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static int builtin_loadbinary(FAR struct binary_s *binp,
* the priority. That is a bug and needs to be fixed.
*/

builtin = builtin_for_index(index);
builtin = builtin_for_index(index);
if (builtin == NULL)
{
berr("ERROR: %s is not a builtin application\n", filename);
Expand Down
22 changes: 11 additions & 11 deletions binfmt/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* have to be defined or CONFIG_ELF_DUMPBUFFER does nothing.
*/

#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
#if !defined(CONFIG_DEBUG_INFO) || !defined(CONFIG_DEBUG_BINFMT)
# undef CONFIG_ELF_DUMPBUFFER
#endif

Expand Down Expand Up @@ -300,22 +300,22 @@ static int elf_loadbinary(FAR struct binary_s *binp,
binp->addrenv = loadinfo.addrenv;

#else
binp->alloc[0] = (FAR void *)loadinfo.textalloc;
binp->alloc[1] = (FAR void *)loadinfo.dataalloc;
#ifdef CONFIG_BINFMT_CONSTRUCTORS
binp->alloc[2] = loadinfo.ctoralloc;
binp->alloc[3] = loadinfo.dtoralloc;
#endif
binp->alloc[0] = (FAR void *)loadinfo.textalloc;
binp->alloc[1] = (FAR void *)loadinfo.dataalloc;
# ifdef CONFIG_BINFMT_CONSTRUCTORS
binp->alloc[2] = loadinfo.ctoralloc;
binp->alloc[3] = loadinfo.dtoralloc;
# endif
#endif

#ifdef CONFIG_BINFMT_CONSTRUCTORS
/* Save information about constructors and destructors. */

binp->ctors = loadinfo.ctors;
binp->nctors = loadinfo.nctors;
binp->ctors = loadinfo.ctors;
binp->nctors = loadinfo.nctors;

binp->dtors = loadinfo.dtors;
binp->ndtors = loadinfo.ndtors;
binp->dtors = loadinfo.dtors;
binp->ndtors = loadinfo.ndtors;
#endif

#ifdef CONFIG_SCHED_USER_IDENTITY
Expand Down
7 changes: 1 addition & 6 deletions binfmt/libelf/libelf.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>

#include <nuttx/arch.h>
#include <nuttx/binfmt/elf.h>

/****************************************************************************
Expand Down Expand Up @@ -253,7 +248,7 @@ int elf_loaddtors(FAR struct elf_loadinfo_s *loadinfo);
* Allocate memory for the ELF image (textalloc and dataalloc).
* If CONFIG_ARCH_ADDRENV=n, textalloc will be allocated using kmm_zalloc()
* and dataalloc will be a offset from textalloc.
* If CONFIG_ARCH_ADDRENV-y, then textalloc and dataalloc will be allocated
* If CONFIG_ARCH_ADDRENV=y, then textalloc and dataalloc will be allocated
* using up_addrenv_create().
* In either case, there will be a unique instance of textalloc and
* dataalloc (and stack) for each instance of a process.
Expand Down
30 changes: 14 additions & 16 deletions binfmt/libelf/libelf_addrenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
****************************************************************************/

#define ELF_TEXT_WRE (PROT_READ | PROT_WRITE | PROT_EXEC)
#define ELF_TEXT_WRD (PROT_READ | PROT_EXEC)
#define ELF_TEXT_RE (PROT_READ | PROT_EXEC)

/****************************************************************************
* Private Constant Data
Expand Down Expand Up @@ -144,14 +144,13 @@ int elf_addrenv_alloc(FAR struct elf_loadinfo_s *loadinfo, size_t textsize,

/* Allocate memory to hold the ELF image */

#if defined(CONFIG_ARCH_USE_TEXT_HEAP)
# if defined(CONFIG_ARCH_USE_TEXT_HEAP)
loadinfo->textalloc = (uintptr_t)
up_textheap_memalign(loadinfo->textalign,
textsize);
#else
loadinfo->textalloc = (uintptr_t)kumm_memalign(loadinfo->textalign,
textsize);
#endif
up_textheap_memalign(loadinfo->textalign, textsize);
# else
loadinfo->textalloc = (uintptr_t)
kumm_memalign(loadinfo->textalign, textsize);
# endif

if (!loadinfo->textalloc)
{
Expand All @@ -160,8 +159,8 @@ int elf_addrenv_alloc(FAR struct elf_loadinfo_s *loadinfo, size_t textsize,

if (loadinfo->datasize > 0)
{
loadinfo->dataalloc = (uintptr_t)kumm_memalign(loadinfo->dataalign,
datasize);
loadinfo->dataalloc = (uintptr_t)
kumm_memalign(loadinfo->dataalign, datasize);
if (!loadinfo->dataalloc)
{
return -ENOMEM;
Expand Down Expand Up @@ -236,7 +235,7 @@ int elf_addrenv_restore(FAR struct elf_loadinfo_s *loadinfo)
/* Remove write access to .text */

ret = up_addrenv_mprot(&loadinfo->addrenv->addrenv, loadinfo->textalloc,
loadinfo->textsize, ELF_TEXT_WRD);
loadinfo->textsize, ELF_TEXT_RE);
if (ret < 0)
{
berr("ERROR: up_addrenv_text_disable_write failed: %d\n", ret);
Expand All @@ -261,7 +260,7 @@ int elf_addrenv_restore(FAR struct elf_loadinfo_s *loadinfo)
*
* Description:
* Release the address environment previously created by
* elf_addrenv_alloc(). This function is called only under certain error
* elf_addrenv_alloc(). This function is called only under certain error
* conditions after the module has been loaded but not yet started.
* After the module has been started, the address environment will
* automatically be freed when the module exits.
Expand All @@ -285,18 +284,17 @@ void elf_addrenv_free(FAR struct elf_loadinfo_s *loadinfo)

if (loadinfo->textalloc != 0)
{
#if defined(CONFIG_ARCH_USE_TEXT_HEAP)
# if defined(CONFIG_ARCH_USE_TEXT_HEAP)
up_textheap_free((FAR void *)loadinfo->textalloc);
#else
# else
kumm_free((FAR void *)loadinfo->textalloc);
#endif
# endif
}

if (loadinfo->dataalloc != 0)
{
kumm_free((FAR void *)loadinfo->dataalloc);
}

#endif

/* Clear out all indications of the allocated address environment */
Expand Down
7 changes: 3 additions & 4 deletions binfmt/libelf/libelf_ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int elf_loadctors(FAR struct elf_loadinfo_s *loadinfo)
ctorsize = shdr->sh_size;
loadinfo->nctors = ctorsize / sizeof(binfmt_ctor_t);

binfo("ctoridx=%d ctorsize=%d sizeof(binfmt_ctor_t)=%d nctors=%d\n",
binfo("ctoridx=%d ctorsize=%zd sizeof(binfmt_ctor_t)=%zd nctors=%d\n",
ctoridx, ctorsize, sizeof(binfmt_ctor_t), loadinfo->nctors);

/* Check if there are any constructors. It is not an error if there
Expand Down Expand Up @@ -168,9 +168,8 @@ int elf_loadctors(FAR struct elf_loadinfo_s *loadinfo)
((FAR void *)(&loadinfo->ctors)[i]);

binfo("ctor %d: "
"%08" PRIxPTR " + %08" PRIxPTR " = %08" PRIxPTR "\n",
i, *ptr, (uintptr_t)loadinfo->textalloc,
(uintptr_t)(*ptr + loadinfo->textalloc));
"%08" PRIxPTR " + %08" PRIxPTR " = %08" PRIxPTR "\n", i,
*ptr, loadinfo->textalloc, (*ptr + loadinfo->textalloc));

*ptr += loadinfo->textalloc;
}
Expand Down
7 changes: 3 additions & 4 deletions binfmt/libelf/libelf_dtors.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int elf_loaddtors(FAR struct elf_loadinfo_s *loadinfo)
loadinfo->ndtors = dtorsize / sizeof(binfmt_dtor_t);

binfo("dtoridx=%d dtorsize=%d sizeof(binfmt_dtor_t)=%d ndtors=%d\n",
dtoridx, dtorsize, sizeof(binfmt_dtor_t), loadinfo->ndtors);
dtoridx, dtorsize, sizeof(binfmt_dtor_t), loadinfo->ndtors);

/* Check if there are any destructors. It is not an error if there
* are none.
Expand Down Expand Up @@ -169,9 +169,8 @@ int elf_loaddtors(FAR struct elf_loadinfo_s *loadinfo)
((FAR void *)(&loadinfo->dtors)[i]);

binfo("dtor %d: "
"%08" PRIxPTR " + %08" PRIxPTR " = %08" PRIxPTR "\n",
i, *ptr, (uintptr_t)loadinfo->textalloc,
(uintptr_t)(*ptr + loadinfo->textalloc));
"%08" PRIxPTR " + %08" PRIxPTR " = %08" PRIxPTR "\n", i,
*ptr, loadinfo->textalloc, (*ptr + loadinfo->textalloc));

*ptr += loadinfo->textalloc;
}
Expand Down
2 changes: 1 addition & 1 deletion binfmt/libelf/libelf_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* be defined or CONFIG_ELF_DUMPBUFFER does nothing.
*/

#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
#if !defined(CONFIG_DEBUG_INFO) || !defined(CONFIG_DEBUG_BINFMT)
# undef CONFIG_ELF_DUMPBUFFER
#endif

Expand Down
4 changes: 2 additions & 2 deletions binfmt/libelf/libelf_iobuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

int elf_allocbuffer(FAR struct elf_loadinfo_s *loadinfo)
{
/* Has a buffer been allocated> */
/* Has a buffer been allocated? */

if (!loadinfo->iobuffer)
{
Expand Down Expand Up @@ -105,7 +105,7 @@ int elf_reallocbuffer(FAR struct elf_loadinfo_s *loadinfo, size_t increment)

/* And perform the reallocation */

buffer = kmm_realloc((FAR void *)loadinfo->iobuffer, newsize);
buffer = kmm_realloc(loadinfo->iobuffer, newsize);
if (!buffer)
{
berr("Failed to reallocate the I/O buffer\n");
Expand Down
47 changes: 20 additions & 27 deletions binfmt/libelf/libelf_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

/* _ALIGN_UP: 'a' is assumed to be a power of two */

#define _ALIGN_UP(v, a) (((v) + ((a) - 1)) & ~((a) - 1))
#define _ALIGN_UP(v, a) (((v) + ((a) - 1)) & ~((a) - 1))

/****************************************************************************
* Private Constant Data
Expand All @@ -74,17 +74,14 @@
*
****************************************************************************/

static void elf_elfsize(struct elf_loadinfo_s *loadinfo)
static void elf_elfsize(FAR struct elf_loadinfo_s *loadinfo)
{
size_t textsize;
size_t datasize;
size_t textsize = 0;
size_t datasize = 0;
int i;

/* Accumulate the size each section into memory that is marked SHF_ALLOC */

textsize = 0;
datasize = 0;

for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
{
FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
Expand Down Expand Up @@ -141,17 +138,15 @@ static void elf_elfsize(struct elf_loadinfo_s *loadinfo)

static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo)
{
FAR uint8_t *text;
FAR uint8_t *data;
FAR uint8_t *text = (FAR uint8_t *)loadinfo->textalloc;
FAR uint8_t *data = (FAR uint8_t *)loadinfo->dataalloc;
FAR uint8_t **pptr;
int ret;
int i;

/* Read each section into memory that is marked SHF_ALLOC + SHT_NOBITS */

binfo("Loaded sections:\n");
text = (FAR uint8_t *)loadinfo->textalloc;
data = (FAR uint8_t *)loadinfo->dataalloc;

for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
{
Expand Down Expand Up @@ -267,7 +262,20 @@ static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo)

int elf_load(FAR struct elf_loadinfo_s *loadinfo)
{
size_t heapsize;
/* Determine the heapsize to allocate. heapsize is ignored if there is
* no address environment because the heap is a shared resource in that
* case. If there is no dynamic stack then heapsize must at least as big
* as the fixed stack size since the stack will be allocated from the heap
* in that case.
*/

#if !defined(CONFIG_ARCH_ADDRENV)
size_t heapsize = 0;
#elif defined(CONFIG_ARCH_STACK_DYNAMIC)
size_t heapsize = ARCH_HEAP_SIZE;
#else
size_t heapsize = MAX(ARCH_HEAP_SIZE, CONFIG_ELF_STACKSIZE);
#endif
#ifdef CONFIG_ELF_EXIDX_SECTNAME
int exidx;
#endif
Expand All @@ -289,21 +297,6 @@ int elf_load(FAR struct elf_loadinfo_s *loadinfo)

elf_elfsize(loadinfo);

/* Determine the heapsize to allocate. heapsize is ignored if there is
* no address environment because the heap is a shared resource in that
* case. If there is no dynamic stack then heapsize must at least as big
* as the fixed stack size since the stack will be allocated from the heap
* in that case.
*/

#if !defined(CONFIG_ARCH_ADDRENV)
heapsize = 0;
#elif defined(CONFIG_ARCH_STACK_DYNAMIC)
heapsize = ARCH_HEAP_SIZE;
#else
heapsize = MAX(ARCH_HEAP_SIZE, CONFIG_ELF_STACKSIZE);
#endif

/* Allocate (and zero) memory for the ELF file. */

ret = elf_addrenv_alloc(loadinfo, loadinfo->textsize, loadinfo->datasize,
Expand Down
Loading

0 comments on commit cb8df39

Please sign in to comment.