From cb8df392074314a326996a8c4d1e2a18bd0f5bc3 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 3 Jul 2023 00:11:02 +0800 Subject: [PATCH] binfmt/elf: Fix the minor style issue and remove the unused macros and unnecessary cast Signed-off-by: Xiang Xiao --- binfmt/binfmt_execmodule.c | 4 +-- binfmt/binfmt_globals.c | 2 +- binfmt/binfmt_unloadmodule.c | 10 +++--- binfmt/builtin.c | 2 +- binfmt/elf.c | 22 +++++++------- binfmt/libelf/libelf.h | 7 +---- binfmt/libelf/libelf_addrenv.c | 30 +++++++++--------- binfmt/libelf/libelf_ctors.c | 7 ++--- binfmt/libelf/libelf_dtors.c | 7 ++--- binfmt/libelf/libelf_init.c | 2 +- binfmt/libelf/libelf_iobuffer.c | 4 +-- binfmt/libelf/libelf_load.c | 47 ++++++++++++---------------- binfmt/libelf/libelf_read.c | 30 +++++++++--------- binfmt/libelf/libelf_sections.c | 22 ++++++-------- binfmt/libelf/libelf_symbols.c | 13 +++----- binfmt/libelf/libelf_uninit.c | 12 ++++---- binfmt/libelf/libelf_unload.c | 6 ++-- binfmt/libelf/libelf_verify.c | 2 +- binfmt/libnxflat/libnxflat.h | 22 ++++++++++---- binfmt/libnxflat/libnxflat_init.c | 2 ++ binfmt/nxflat.c | 2 +- include/nuttx/binfmt/binfmt.h | 8 ++--- include/nuttx/binfmt/elf.h | 15 ++------- include/nuttx/binfmt/nxflat.h | 15 --------- include/nuttx/lib/modlib.h | 6 ++-- libs/libc/dlfcn/lib_dlclose.c | 8 +++-- libs/libc/dlfcn/lib_dlerror.c | 2 +- libs/libc/dlfcn/lib_dlopen.c | 29 ++++++++---------- libs/libc/dlfcn/lib_dlsymtab.c | 1 - libs/libc/modlib/Make.defs | 3 +- libs/libc/modlib/modlib.h | 5 --- libs/libc/modlib/modlib_bind.c | 49 +++++++++++++++--------------- libs/libc/modlib/modlib_depend.c | 4 +-- libs/libc/modlib/modlib_init.c | 3 +- libs/libc/modlib/modlib_iobuffer.c | 2 +- libs/libc/modlib/modlib_load.c | 33 ++++++++------------ libs/libc/modlib/modlib_loadhdrs.c | 12 ++++---- libs/libc/modlib/modlib_read.c | 30 +++++++++--------- libs/libc/modlib/modlib_registry.c | 1 - libs/libc/modlib/modlib_sections.c | 20 +++++------- libs/libc/modlib/modlib_symtab.c | 1 + libs/libc/modlib/modlib_uninit.c | 18 +++++------ libs/libc/modlib/modlib_unload.c | 2 +- libs/libc/modlib/modlib_verify.c | 12 +++----- sched/module/mod_insmod.c | 11 ++++--- sched/module/mod_modhandle.c | 2 +- sched/module/mod_procfs.c | 10 +++--- sched/module/mod_rmmod.c | 6 ++-- 48 files changed, 254 insertions(+), 309 deletions(-) diff --git a/binfmt/binfmt_execmodule.c b/binfmt/binfmt_execmodule.c index b660e3eef26bc..7862615c36d2c 100644 --- a/binfmt/binfmt_execmodule.c +++ b/binfmt/binfmt_execmodule.c @@ -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++; @@ -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 diff --git a/binfmt/binfmt_globals.c b/binfmt/binfmt_globals.c index 0d80b4c953919..5140ff9624e85 100644 --- a/binfmt/binfmt_globals.c +++ b/binfmt/binfmt_globals.c @@ -24,7 +24,7 @@ #include -#include +#include "binfmt.h" #ifndef CONFIG_BINFMT_DISABLE diff --git a/binfmt/binfmt_unloadmodule.c b/binfmt/binfmt_unloadmodule.c index 0c0be333edc3d..b3f89a99507c2 100644 --- a/binfmt/binfmt_unloadmodule.c +++ b/binfmt/binfmt_unloadmodule.c @@ -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); @@ -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++; @@ -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 @@ -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]); } } } diff --git a/binfmt/builtin.c b/binfmt/builtin.c index 772068acd31ac..19a8c3f4bb345 100644 --- a/binfmt/builtin.c +++ b/binfmt/builtin.c @@ -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); diff --git a/binfmt/elf.c b/binfmt/elf.c index 81c5fdf845871..d78abcff98739 100644 --- a/binfmt/elf.c +++ b/binfmt/elf.c @@ -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 @@ -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 diff --git a/binfmt/libelf/libelf.h b/binfmt/libelf/libelf.h index d2f7b6607e040..a13aa74d6e7c4 100644 --- a/binfmt/libelf/libelf.h +++ b/binfmt/libelf/libelf.h @@ -25,11 +25,6 @@ * Included Files ****************************************************************************/ -#include - -#include - -#include #include /**************************************************************************** @@ -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. diff --git a/binfmt/libelf/libelf_addrenv.c b/binfmt/libelf/libelf_addrenv.c index 8221c20488136..32c1709b30871 100644 --- a/binfmt/libelf/libelf_addrenv.c +++ b/binfmt/libelf/libelf_addrenv.c @@ -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 @@ -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) { @@ -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; @@ -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); @@ -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. @@ -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 */ diff --git a/binfmt/libelf/libelf_ctors.c b/binfmt/libelf/libelf_ctors.c index 8722df4893ea4..63c294e1e9b52 100644 --- a/binfmt/libelf/libelf_ctors.c +++ b/binfmt/libelf/libelf_ctors.c @@ -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 @@ -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; } diff --git a/binfmt/libelf/libelf_dtors.c b/binfmt/libelf/libelf_dtors.c index 2f502740996cb..ac389606241ad 100644 --- a/binfmt/libelf/libelf_dtors.c +++ b/binfmt/libelf/libelf_dtors.c @@ -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. @@ -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; } diff --git a/binfmt/libelf/libelf_init.c b/binfmt/libelf/libelf_init.c index 95a7a0c91e51c..adca65e37de40 100644 --- a/binfmt/libelf/libelf_init.c +++ b/binfmt/libelf/libelf_init.c @@ -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 diff --git a/binfmt/libelf/libelf_iobuffer.c b/binfmt/libelf/libelf_iobuffer.c index b8ed7ff32c3d6..b2f0e96aca2d1 100644 --- a/binfmt/libelf/libelf_iobuffer.c +++ b/binfmt/libelf/libelf_iobuffer.c @@ -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) { @@ -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"); diff --git a/binfmt/libelf/libelf_load.c b/binfmt/libelf/libelf_load.c index 9e22e07bff78e..e7f7ebd06b85b 100644 --- a/binfmt/libelf/libelf_load.c +++ b/binfmt/libelf/libelf_load.c @@ -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 @@ -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]; @@ -141,8 +138,8 @@ 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; @@ -150,8 +147,6 @@ static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo) /* 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++) { @@ -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 @@ -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, diff --git a/binfmt/libelf/libelf_read.c b/binfmt/libelf/libelf_read.c index b05cda281e697..ef6feea92d504 100644 --- a/binfmt/libelf/libelf_read.c +++ b/binfmt/libelf/libelf_read.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -52,19 +53,19 @@ * Name: elf_dumpreaddata ****************************************************************************/ -#if defined(ELF_DUMP_READDATA) -static inline void elf_dumpreaddata(FAR char *buffer, int buflen) +#ifdef ELF_DUMP_READDATA +static inline void elf_dumpreaddata(FAR char *buffer, size_t buflen) { FAR uint32_t *buf32 = (FAR uint32_t *)buffer; - int i; - int j; + size_t i; + size_t j; for (i = 0; i < buflen; i += 32) { - syslog(LOG_DEBUG, "%04x:", i); + syslog(LOG_DEBUG, "%04zx:", i); for (j = 0; j < 32; j += sizeof(uint32_t)) { - syslog(LOG_DEBUG, " %08x", *buf32++); + syslog(LOG_DEBUG, " %08" PRIx32, *buf32++); } syslog(LOG_DEBUG, "\n"); @@ -97,10 +98,11 @@ static inline void elf_dumpreaddata(FAR char *buffer, int buflen) int elf_read(FAR struct elf_loadinfo_s *loadinfo, FAR uint8_t *buffer, size_t readsize, off_t offset) { + size_t nsize = readsize; ssize_t nbytes; /* Number of bytes read */ off_t rpos; /* Position returned by lseek */ - binfo("Read %ld bytes from offset %ld\n", (long)readsize, (long)offset); + binfo("Read %zu bytes from offset %" PRIdOFF "\n", readsize, offset); /* Loop until all of the requested data has been read. */ @@ -111,22 +113,23 @@ int elf_read(FAR struct elf_loadinfo_s *loadinfo, FAR uint8_t *buffer, rpos = file_seek(&loadinfo->file, offset, SEEK_SET); if (rpos != offset) { - berr("Failed to seek to position %lu: %d\n", - (unsigned long)offset, (int)rpos); + berr("Failed to seek to position %" PRIdOFF ": %" PRIdOFF "\n", + offset, rpos); return rpos; } /* Read the file data at offset into the user buffer */ - nbytes = file_read(&loadinfo->file, buffer, readsize); + nbytes = file_read(&loadinfo->file, + buffer + nsize - readsize, readsize); if (nbytes < 0) { /* EINTR just means that we received a signal */ if (nbytes != -EINTR) { - berr("Read from offset %lu failed: %d\n", - (unsigned long)offset, (int)nbytes); + berr("Read from offset %" PRIdOFF " failed: %zd\n", + offset, nbytes); return nbytes; } } @@ -138,11 +141,10 @@ int elf_read(FAR struct elf_loadinfo_s *loadinfo, FAR uint8_t *buffer, else { readsize -= nbytes; - buffer += nbytes; offset += nbytes; } } - elf_dumpreaddata(buffer, readsize); + elf_dumpreaddata(buffer, nsize); return OK; } diff --git a/binfmt/libelf/libelf_sections.c b/binfmt/libelf/libelf_sections.c index 50bb79c0b2757..279cd02bae3a2 100644 --- a/binfmt/libelf/libelf_sections.c +++ b/binfmt/libelf/libelf_sections.c @@ -63,10 +63,8 @@ static inline int elf_sectname(FAR struct elf_loadinfo_s *loadinfo, FAR const Elf_Shdr *shdr) { FAR Elf_Shdr *shstr; - FAR uint8_t *buffer; off_t offset; - size_t readlen; - size_t bytesread; + size_t bytesread = 0; int shstrndx; int ret; @@ -110,13 +108,13 @@ static inline int elf_sectname(FAR struct elf_loadinfo_s *loadinfo, /* Loop until we get the entire section name into memory */ - bytesread = 0; - for (; ; ) { + FAR uint8_t *buffer = &loadinfo->iobuffer[bytesread]; + size_t readlen = loadinfo->buflen - bytesread; + /* Get the number of bytes to read */ - readlen = loadinfo->buflen - bytesread; if (offset + readlen > loadinfo->filelen) { if (loadinfo->filelen <= offset) @@ -130,7 +128,6 @@ static inline int elf_sectname(FAR struct elf_loadinfo_s *loadinfo, /* Read that number of bytes into the array */ - buffer = &loadinfo->iobuffer[bytesread]; ret = elf_read(loadinfo, buffer, readlen, offset + bytesread); if (ret < 0) { @@ -210,8 +207,8 @@ int elf_loadshdrs(FAR struct elf_loadinfo_s *loadinfo) loadinfo->shdr = (FAR FAR Elf_Shdr *)kmm_malloc(shdrsize); if (!loadinfo->shdr) { - berr("Failed to allocate the section header table. Size: %ld\n", - (long)shdrsize); + berr("Failed to allocate the section header table. Size: %zu\n", + shdrsize); return -ENOMEM; } @@ -246,8 +243,6 @@ int elf_loadshdrs(FAR struct elf_loadinfo_s *loadinfo) int elf_findsection(FAR struct elf_loadinfo_s *loadinfo, FAR const char *sectname) { - FAR const Elf_Shdr *shdr; - int ret; int i; /* Search through the shdr[] array in loadinfo for a section named @@ -256,10 +251,11 @@ int elf_findsection(FAR struct elf_loadinfo_s *loadinfo, for (i = 0; i < loadinfo->ehdr.e_shnum; i++) { + FAR const Elf_Shdr *shdr = &loadinfo->shdr[i]; + /* Get the name of this section */ - shdr = &loadinfo->shdr[i]; - ret = elf_sectname(loadinfo, shdr); + int ret = elf_sectname(loadinfo, shdr); if (ret < 0) { berr("elf_sectname failed: %d\n", ret); diff --git a/binfmt/libelf/libelf_symbols.c b/binfmt/libelf/libelf_symbols.c index 8c3557ab9d716..cfdd41d15e77a 100644 --- a/binfmt/libelf/libelf_symbols.c +++ b/binfmt/libelf/libelf_symbols.c @@ -66,10 +66,8 @@ static int elf_symname(FAR struct elf_loadinfo_s *loadinfo, FAR const Elf_Sym *sym) { - FAR uint8_t *buffer; off_t offset; - size_t readlen; - size_t bytesread; + size_t bytesread = 0; int ret; /* Get the file offset to the string that is the name of the symbol. The @@ -97,13 +95,13 @@ static int elf_symname(FAR struct elf_loadinfo_s *loadinfo, /* Loop until we get the entire symbol name into memory */ - bytesread = 0; - for (; ; ) { + FAR uint8_t *buffer = &loadinfo->iobuffer[bytesread]; + size_t readlen = loadinfo->buflen - bytesread; + /* Get the number of bytes to read */ - readlen = loadinfo->buflen - bytesread; if (offset + readlen > loadinfo->filelen) { if (loadinfo->filelen <= offset) @@ -117,7 +115,6 @@ static int elf_symname(FAR struct elf_loadinfo_s *loadinfo, /* Read that number of bytes into the array */ - buffer = &loadinfo->iobuffer[bytesread]; ret = elf_read(loadinfo, buffer, readlen, offset + bytesread); if (ret < 0) { @@ -320,7 +317,7 @@ int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf_Sym *sym, (uintptr_t)symbol->sym_value, (uintptr_t)(sym->st_value + (uintptr_t)symbol->sym_value)); - sym->st_value += ((uintptr_t)symbol->sym_value); + sym->st_value += (uintptr_t)symbol->sym_value; } break; diff --git a/binfmt/libelf/libelf_uninit.c b/binfmt/libelf/libelf_uninit.c index 54ebee6a344ce..5c2754696fe74 100644 --- a/binfmt/libelf/libelf_uninit.c +++ b/binfmt/libelf/libelf_uninit.c @@ -90,21 +90,21 @@ int elf_uninit(struct elf_loadinfo_s *loadinfo) * ****************************************************************************/ -int elf_freebuffers(struct elf_loadinfo_s *loadinfo) +int elf_freebuffers(FAR struct elf_loadinfo_s *loadinfo) { /* Release all working allocations */ if (loadinfo->shdr) { - kmm_free((FAR void *)loadinfo->shdr); - loadinfo->shdr = NULL; + kmm_free(loadinfo->shdr); + loadinfo->shdr = NULL; } if (loadinfo->iobuffer) { - kmm_free((FAR void *)loadinfo->iobuffer); - loadinfo->iobuffer = NULL; - loadinfo->buflen = 0; + kmm_free(loadinfo->iobuffer); + loadinfo->iobuffer = NULL; + loadinfo->buflen = 0; } return OK; diff --git a/binfmt/libelf/libelf_unload.c b/binfmt/libelf/libelf_unload.c index 85688424ad0d9..79476b2b4cf75 100644 --- a/binfmt/libelf/libelf_unload.c +++ b/binfmt/libelf/libelf_unload.c @@ -62,7 +62,7 @@ * ****************************************************************************/ -int elf_unload(struct elf_loadinfo_s *loadinfo) +int elf_unload(FAR struct elf_loadinfo_s *loadinfo) { /* Free all working buffers */ @@ -75,7 +75,7 @@ int elf_unload(struct elf_loadinfo_s *loadinfo) /* Release memory used to hold static constructors and destructors */ #ifdef CONFIG_BINFMT_CONSTRUCTORS -#ifndef CONFIG_ARCH_ADDRENV +# ifndef CONFIG_ARCH_ADDRENV if (loadinfo->ctoralloc != 0) { kumm_free(loadinfo->ctoralloc); @@ -85,7 +85,7 @@ int elf_unload(struct elf_loadinfo_s *loadinfo) { kumm_free(loadinfo->dtoralloc); } -#endif +# endif loadinfo->ctoralloc = NULL; loadinfo->ctors = NULL; diff --git a/binfmt/libelf/libelf_verify.c b/binfmt/libelf/libelf_verify.c index be32ddaefdf03..08eb74b21cd29 100644 --- a/binfmt/libelf/libelf_verify.c +++ b/binfmt/libelf/libelf_verify.c @@ -86,7 +86,7 @@ int elf_verifyheader(FAR const Elf_Ehdr *ehdr) /* Verify that this is a relocatable file */ - if ((ehdr->e_type != ET_REL) && (ehdr->e_type != ET_EXEC)) + if (ehdr->e_type != ET_REL && ehdr->e_type != ET_EXEC) { berr("Not a relocatable or executable file: e_type=%d\n", ehdr->e_type); diff --git a/binfmt/libnxflat/libnxflat.h b/binfmt/libnxflat/libnxflat.h index fe3a414683eea..4ff27e6262b3b 100644 --- a/binfmt/libnxflat/libnxflat.h +++ b/binfmt/libnxflat/libnxflat.h @@ -25,11 +25,6 @@ * Included Files ****************************************************************************/ -#include - -#include - -#include #include /**************************************************************************** @@ -37,9 +32,24 @@ ****************************************************************************/ /**************************************************************************** - * Public Types + * Public Function Definitions ****************************************************************************/ +/**************************************************************************** + * Name: nxflat_verifyheader + * + * Description: + * Given the header from a possible NXFLAT executable, verify that it is + * an NXFLAT executable. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int nxflat_verifyheader(FAR const struct nxflat_hdr_s *header); + /**************************************************************************** * Name: nxflat_addrenv_alloc * diff --git a/binfmt/libnxflat/libnxflat_init.c b/binfmt/libnxflat/libnxflat_init.c index 09816e1128c81..febb6c633b5ee 100644 --- a/binfmt/libnxflat/libnxflat_init.c +++ b/binfmt/libnxflat/libnxflat_init.c @@ -36,6 +36,8 @@ #include #include +#include "libnxflat.h" + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ diff --git a/binfmt/nxflat.c b/binfmt/nxflat.c index c3783f06b9d1d..b1ec250e63d9f 100644 --- a/binfmt/nxflat.c +++ b/binfmt/nxflat.c @@ -48,7 +48,7 @@ * have to be defined or CONFIG_NXFLAT_DUMPBUFFER does nothing. */ -#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT) +#if !defined(CONFIG_DEBUG_INFO) || !defined(CONFIG_DEBUG_BINFMT) # undef CONFIG_NXFLAT_DUMPBUFFER #endif diff --git a/include/nuttx/binfmt/binfmt.h b/include/nuttx/binfmt/binfmt.h index 1c90c60fe15d5..f15c88f8dd3d1 100644 --- a/include/nuttx/binfmt/binfmt.h +++ b/include/nuttx/binfmt/binfmt.h @@ -28,10 +28,8 @@ #include #include - #include -#include #include #include @@ -47,8 +45,8 @@ /* The type of one C++ constructor or destructor */ -typedef FAR void (*binfmt_ctor_t)(void); -typedef FAR void (*binfmt_dtor_t)(void); +typedef CODE void (*binfmt_ctor_t)(void); +typedef CODE void (*binfmt_dtor_t)(void); /* This describes the file to be loaded. * @@ -141,7 +139,7 @@ struct binfmt_s CODE int (*unload)(FAR struct binary_s *bin); - /* Unload module callback */ + /* Coredump callback */ CODE int (*coredump)(FAR struct memory_region_s *regions, FAR struct lib_outstream_s *stream, diff --git a/include/nuttx/binfmt/elf.h b/include/nuttx/binfmt/elf.h index 14d9e1233921b..80f7749756dbc 100644 --- a/include/nuttx/binfmt/elf.h +++ b/include/nuttx/binfmt/elf.h @@ -56,17 +56,6 @@ # define CONFIG_ELF_BUFFERINCR 32 #endif -/* Allocation array size and indices */ - -#define LIBELF_ELF_ALLOC 0 -#ifdef CONFIG_BINFMT_CONSTRUCTORS -# define LIBELF_CTORS_ALLOC 1 -# define LIBELF_CTPRS_ALLOC 2 -# define LIBELF_NALLOC 3 -#else -# define LIBELF_NALLOC 1 -#endif - /**************************************************************************** * Public Types ****************************************************************************/ @@ -82,7 +71,7 @@ struct elf_loadinfo_s * * If CONFIG_ARCH_ADDRENV=n, elfalloc will be allocated using kmm_malloc() * (or kmm_zalloc()). - * If CONFIG_ARCH_ADDRENV-y, then elfalloc will be allocated using + * If CONFIG_ARCH_ADDRENV=y, then elfalloc will be allocated using * up_addrenv_create(). In either case, there will be a unique instance * of elfalloc (and stack) for each instance of a process. * @@ -235,7 +224,7 @@ int elf_bind(FAR struct elf_loadinfo_s *loadinfo, * ****************************************************************************/ -int elf_unload(struct elf_loadinfo_s *loadinfo); +int elf_unload(FAR struct elf_loadinfo_s *loadinfo); /**************************************************************************** * Name: elf_coredump diff --git a/include/nuttx/binfmt/nxflat.h b/include/nuttx/binfmt/nxflat.h index e51a4f1ce144e..2a7a04fdac5d7 100644 --- a/include/nuttx/binfmt/nxflat.h +++ b/include/nuttx/binfmt/nxflat.h @@ -112,21 +112,6 @@ extern "C" #define EXTERN extern #endif -/**************************************************************************** - * Name: nxflat_verifyheader - * - * Description: - * Given the header from a possible NXFLAT executable, verify that it is - * an NXFLAT executable. - * - * Returned Value: - * 0 (OK) is returned on success and a negated errno is returned on - * failure. - * - ****************************************************************************/ - -int nxflat_verifyheader(const struct nxflat_hdr_s *header); - /**************************************************************************** * Name: nxflat_init * diff --git a/include/nuttx/lib/modlib.h b/include/nuttx/lib/modlib.h index 60384e40e87e7..48144a115f7b7 100644 --- a/include/nuttx/lib/modlib.h +++ b/include/nuttx/lib/modlib.h @@ -52,11 +52,11 @@ # define CONFIG_MODLIB_BUFFERINCR 32 #endif -/* CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT have to be defined or +/* CONFIG_DEBUG_INFO, and CONFIG_LIBC_MODLIB have to be defined or * CONFIG_MODLIB_DUMPBUFFER does nothing. */ -#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT) +#if !defined(CONFIG_DEBUG_INFO) || !defined(CONFIG_LIBC_MODLIB) # undef CONFIG_MODLIB_DUMPBUFFER #endif @@ -77,7 +77,7 @@ #if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) # define HAVE_MODLIB_NAMES -# define MODLIB_NAMEMAX 16 +# define MODLIB_NAMEMAX NAME_MAX #endif /**************************************************************************** diff --git a/libs/libc/dlfcn/lib_dlclose.c b/libs/libc/dlfcn/lib_dlclose.c index 28d139aa2c0f0..873f838d0ac32 100644 --- a/libs/libc/dlfcn/lib_dlclose.c +++ b/libs/libc/dlfcn/lib_dlclose.c @@ -129,7 +129,11 @@ static inline int dlremove(FAR void *handle) { /* Free the module memory */ - lib_free((FAR void *)modp->textalloc); +#if defined(CONFIG_ARCH_USE_TEXT_HEAP) + up_textheap_free(modp->textalloc); +#else + lib_free(modp->textalloc); +#endif /* Nullify so that the memory cannot be freed again */ @@ -143,7 +147,7 @@ static inline int dlremove(FAR void *handle) { /* Free the module memory */ - lib_free((FAR void *)modp->dataalloc); + lib_free(modp->dataalloc); /* Nullify so that the memory cannot be freed again */ diff --git a/libs/libc/dlfcn/lib_dlerror.c b/libs/libc/dlfcn/lib_dlerror.c index a57582525bd12..342d18fe982c5 100644 --- a/libs/libc/dlfcn/lib_dlerror.c +++ b/libs/libc/dlfcn/lib_dlerror.c @@ -55,5 +55,5 @@ FAR char *dlerror(void) { - return (FAR char *)strerror(get_errno()); + return strerror(get_errno()); } diff --git a/libs/libc/dlfcn/lib_dlopen.c b/libs/libc/dlfcn/lib_dlopen.c index d38c9f3054266..96bae367b4717 100644 --- a/libs/libc/dlfcn/lib_dlopen.c +++ b/libs/libc/dlfcn/lib_dlopen.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -49,7 +50,7 @@ ****************************************************************************/ #ifdef CONFIG_BUILD_PROTECTED -#if defined(CONFIG_DEBUG_INFO) && defined(CONFIG_DEBUG_BINFMT) +# if defined(CONFIG_DEBUG_INFO) && defined(CONFIG_DEBUG_BINFMT) static void dldump_loadinfo(FAR struct mod_loadinfo_s *loadinfo) { int i; @@ -118,18 +119,16 @@ static void dldump_loadinfo(FAR struct mod_loadinfo_s *loadinfo) } } } -#else -# define dldump_loadinfo(i) -#endif +# else +# define dldump_loadinfo(i) +# endif #endif /**************************************************************************** * Name: dldump_initializer ****************************************************************************/ -#ifdef CONFIG_BUILD_PROTECTED -#ifdef CONFIG_MODLIB_DUMPBUFFER -#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MODLIB_DUMPBUFFER) static void dldump_initializer(mod_initializer_t initializer, FAR struct mod_loadinfo_s *loadinfo) { @@ -139,7 +138,6 @@ static void dldump_initializer(mod_initializer_t initializer, #else # define dldump_initializer(b,l) #endif -#endif /**************************************************************************** * Public Functions @@ -204,14 +202,13 @@ static inline FAR void *dlinsert(FAR const char *filename) /* Allocate a module registry entry to hold the module data */ modp = (FAR struct module_s *)lib_zalloc(sizeof(struct module_s)); - if (ret != 0) + if (modp == NULL) { + ret = -ENOMEM; binfo("Failed to initialize for load of ELF program: %d\n", ret); goto errout_with_loadinfo; } - memset(modp, 0, sizeof(*modp)); - /* Load the program binary */ ret = modlib_load(&loadinfo); @@ -233,11 +230,11 @@ static inline FAR void *dlinsert(FAR const char *filename) /* Save the load information */ - modp->textalloc = (FAR void *)loadinfo.textalloc; - modp->dataalloc = (FAR void *)loadinfo.datastart; + modp->textalloc = (FAR void *)loadinfo.textalloc; + modp->dataalloc = (FAR void *)loadinfo.datastart; #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE) - modp->textsize = loadinfo.textsize; - modp->datasize = loadinfo.datasize; + modp->textsize = loadinfo.textsize; + modp->datasize = loadinfo.datasize; #endif /* Get the module initializer entry point */ @@ -290,7 +287,7 @@ static inline FAR void *dlinsert(FAR const char *filename) modlib_uninitialize(&loadinfo); modlib_registry_unlock(); - return (FAR void *)modp; + return modp; errout_with_load: modlib_unload(&loadinfo); diff --git a/libs/libc/dlfcn/lib_dlsymtab.c b/libs/libc/dlfcn/lib_dlsymtab.c index 6190bbcb23520..9b1a2df0642aa 100644 --- a/libs/libc/dlfcn/lib_dlsymtab.c +++ b/libs/libc/dlfcn/lib_dlsymtab.c @@ -27,7 +27,6 @@ #include #include -#include #include /**************************************************************************** diff --git a/libs/libc/modlib/Make.defs b/libs/libc/modlib/Make.defs index 24ff58209befd..f9cd0a3e335fa 100644 --- a/libs/libc/modlib/Make.defs +++ b/libs/libc/modlib/Make.defs @@ -23,10 +23,9 @@ ifeq ($(CONFIG_LIBC_MODLIB),y) # Add the nuttx/lib/modlib.h files to the build CSRCS += modlib_bind.c modlib_depend.c modlib_init.c modlib_iobuffer.c -CSRCS += modlib_load.c modlib_loadhdrs.c +CSRCS += modlib_load.c modlib_loadhdrs.c modlib_verify.c CSRCS += modlib_read.c modlib_registry.c modlib_sections.c CSRCS += modlib_symbols.c modlib_symtab.c modlib_uninit.c modlib_unload.c -CSRCS += modlib_verify.c # Add the modlib directory to the build diff --git a/libs/libc/modlib/modlib.h b/libs/libc/modlib/modlib.h index 92e8a1ecd6187..9b436744ef9d8 100644 --- a/libs/libc/modlib/modlib.h +++ b/libs/libc/modlib/modlib.h @@ -25,11 +25,6 @@ * Included Files ****************************************************************************/ -#include - -#include - -#include #include /**************************************************************************** diff --git a/libs/libc/modlib/modlib_bind.c b/libs/libc/modlib/modlib_bind.c index 540ce8714a6f0..cdda32a902af7 100644 --- a/libs/libc/modlib/modlib_bind.c +++ b/libs/libc/modlib/modlib_bind.c @@ -593,26 +593,26 @@ static int modlib_relocatedyn(FAR struct module_s *modp, switch (dyn[i].d_tag) { case DT_REL : - reldata.reloff[I_REL] = dyn[i].d_un.d_val; - break; + reldata.reloff[I_REL] = dyn[i].d_un.d_val; + break; case DT_RELSZ : - reldata.relsz[I_REL] = dyn[i].d_un.d_val; - break; + reldata.relsz[I_REL] = dyn[i].d_un.d_val; + break; case DT_RELENT : - reldata.relentsz = dyn[i].d_un.d_val; - break; + reldata.relentsz = dyn[i].d_un.d_val; + break; case DT_SYMTAB : - reldata.symoff = dyn[i].d_un.d_val; - break; + reldata.symoff = dyn[i].d_un.d_val; + break; case DT_STRTAB : - reldata.stroff = dyn[i].d_un.d_val; - break; + reldata.stroff = dyn[i].d_un.d_val; + break; case DT_JMPREL : - reldata.reloff[I_PLT] = dyn[i].d_un.d_val; - break; + reldata.reloff[I_PLT] = dyn[i].d_un.d_val; + break; case DT_PLTRELSZ : - reldata.relsz[I_PLT] = dyn[i].d_un.d_val; - break; + reldata.relsz[I_PLT] = dyn[i].d_un.d_val; + break; } } @@ -626,7 +626,7 @@ static int modlib_relocatedyn(FAR struct module_s *modp, return -ENOMEM; } - ret = modlib_read(loadinfo, (uint8_t *) sym, symhdr->sh_size, + ret = modlib_read(loadinfo, (FAR uint8_t *)sym, symhdr->sh_size, symhdr->sh_offset); if (ret < 0) { @@ -666,7 +666,7 @@ static int modlib_relocatedyn(FAR struct module_s *modp, relsize = reldata.relsz[idx_rel]; } - ret = modlib_read(loadinfo, (FAR uint8_t *) rels, + ret = modlib_read(loadinfo, (FAR uint8_t *)rels, relsize, reldata.reloff[idx_rel] + i * sizeof(Elf_Rel)); @@ -717,7 +717,7 @@ static int modlib_relocatedyn(FAR struct module_s *modp, } addr = rel->r_offset + loadinfo->textalloc; - *(uintptr_t *)addr = (uintptr_t)ep; + *(FAR uintptr_t *)addr = (uintptr_t)ep; } } else @@ -726,10 +726,11 @@ static int modlib_relocatedyn(FAR struct module_s *modp, addr = rel->r_offset - loadinfo->datasec + loadinfo->datastart; - if ((*(uint32_t *) addr) < loadinfo->datasec) - dynsym.st_value = *(uint32_t *) addr + loadinfo->textalloc; + if ((*(FAR uint32_t *)addr) < loadinfo->datasec) + dynsym.st_value = *(FAR uint32_t *)addr + + loadinfo->textalloc; else - dynsym.st_value = *(uint32_t *) addr - + dynsym.st_value = *(FAR uint32_t *)addr - loadinfo->datasec + loadinfo->datastart; ret = up_relocate(rel, &dynsym, addr); } @@ -872,11 +873,11 @@ int modlib_bind(FAR struct module_s *modp, switch (loadinfo->shdr[i].sh_type) { case SHT_REL : - ret = modlib_relocate(modp, loadinfo, i); - break; + ret = modlib_relocate(modp, loadinfo, i); + break; case SHT_RELA : - ret = modlib_relocateadd(modp, loadinfo, i); - break; + ret = modlib_relocateadd(modp, loadinfo, i); + break; } } diff --git a/libs/libc/modlib/modlib_depend.c b/libs/libc/modlib/modlib_depend.c index 4fb5e18b4f2d3..eaa453d007506 100644 --- a/libs/libc/modlib/modlib_depend.c +++ b/libs/libc/modlib/modlib_depend.c @@ -56,7 +56,7 @@ int modlib_depend(FAR struct module_s *importer, FAR struct module_s *exporter) { #if CONFIG_MODLIB_MAXDEPEND > 0 - int freendx; + int freendx = -1; int i; DEBUGASSERT(importer != NULL && exporter != NULL); @@ -71,7 +71,7 @@ int modlib_depend(FAR struct module_s *importer, * is small. Otherwise, a more dynamic data structure would be in order. */ - for (i = 0, freendx = -1; i < CONFIG_MODLIB_MAXDEPEND; i++) + for (i = 0; i < CONFIG_MODLIB_MAXDEPEND; i++) { FAR const struct module_s *modp; diff --git a/libs/libc/modlib/modlib_init.c b/libs/libc/modlib/modlib_init.c index e8cd643beaea9..dcbc3db813554 100644 --- a/libs/libc/modlib/modlib_init.c +++ b/libs/libc/modlib/modlib_init.c @@ -45,7 +45,7 @@ * have to be defined or CONFIG_MODLIB_DUMPBUFFER does nothing. */ -#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_MODLIB_DUMPBUFFER) +#if !defined(CONFIG_DEBUG_INFO) || !defined(CONFIG_MODLIB_DUMPBUFFER) # undef CONFIG_MODLIB_DUMPBUFFER #endif @@ -127,7 +127,6 @@ int modlib_initialize(FAR const char *filename, /* Clear the load info structure */ memset(loadinfo, 0, sizeof(struct mod_loadinfo_s)); - loadinfo->filfd = -1; /* Open the binary file for reading (only) */ diff --git a/libs/libc/modlib/modlib_iobuffer.c b/libs/libc/modlib/modlib_iobuffer.c index 947c3c9da5e7c..1c99e9c9ae6db 100644 --- a/libs/libc/modlib/modlib_iobuffer.c +++ b/libs/libc/modlib/modlib_iobuffer.c @@ -95,7 +95,7 @@ int modlib_reallocbuffer(FAR struct mod_loadinfo_s *loadinfo, /* And perform the reallocation */ - buffer = lib_realloc((FAR void *)loadinfo->iobuffer, newsize); + buffer = lib_realloc(loadinfo->iobuffer, newsize); if (!buffer) { berr("ERROR: Failed to reallocate the I/O buffer\n"); diff --git a/libs/libc/modlib/modlib_load.c b/libs/libc/modlib/modlib_load.c index 11ba615f0f9d3..a2e70d80eee65 100644 --- a/libs/libc/modlib/modlib_load.c +++ b/libs/libc/modlib/modlib_load.c @@ -50,7 +50,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 Functions @@ -68,17 +68,14 @@ * ****************************************************************************/ -static void modlib_elfsize(struct mod_loadinfo_s *loadinfo) +static void modlib_elfsize(FAR struct mod_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; - if (loadinfo->ehdr.e_phnum > 0) { for (i = 0; i < loadinfo->ehdr.e_phnum; i++) @@ -91,14 +88,14 @@ static void modlib_elfsize(struct mod_loadinfo_s *loadinfo) if (phdr->p_flags & PF_X) { textsize += phdr->p_memsz; - textaddr = (void *) phdr->p_vaddr; + textaddr = (FAR void *)phdr->p_vaddr; } else { datasize += phdr->p_memsz; loadinfo->datasec = phdr->p_vaddr; loadinfo->segpad = phdr->p_vaddr - - ((uintptr_t) textaddr + textsize); + ((uintptr_t)textaddr + textsize); } } } @@ -162,19 +159,16 @@ static void modlib_elfsize(struct mod_loadinfo_s *loadinfo) static inline int modlib_loadfile(FAR struct mod_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->datastart; FAR uint8_t **pptr; int ret; int i; /* Read each PT_LOAD area into memory */ - binfo("Loading sections - text: %p.%x data: %p.%x\n", - (void *)loadinfo->textalloc, (int) loadinfo->textsize, - (void *)loadinfo->datastart, (int) loadinfo->datasize); - text = (FAR uint8_t *)loadinfo->textalloc; - data = (FAR uint8_t *)loadinfo->datastart; + binfo("Loading sections - text: %p.%zx data: %p.%zx\n", + text, loadinfo->textsize, data, loadinfo->datasize); if (loadinfo->ehdr.e_phnum > 0) { @@ -191,11 +185,10 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) } else { - int bsssize = phdr->p_memsz - phdr->p_filesz; + size_t bsssize = phdr->p_memsz - phdr->p_filesz; ret = modlib_read(loadinfo, data, phdr->p_filesz, phdr->p_offset); - memset((FAR void *)((uintptr_t) data + phdr->p_filesz), 0, - bsssize); + memset(data + phdr->p_filesz, 0, bsssize); } if (ret < 0) @@ -235,7 +228,7 @@ static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo) } *pptr = (FAR uint8_t *)_ALIGN_UP((uintptr_t)*pptr, - shdr->sh_addralign); + shdr->sh_addralign); /* SHT_NOBITS indicates that there is no data in the file for the * section. diff --git a/libs/libc/modlib/modlib_loadhdrs.c b/libs/libc/modlib/modlib_loadhdrs.c index d1776f8446cd9..67ca77dd08eae 100644 --- a/libs/libc/modlib/modlib_loadhdrs.c +++ b/libs/libc/modlib/modlib_loadhdrs.c @@ -85,8 +85,8 @@ int modlib_loadhdrs(FAR struct mod_loadinfo_s *loadinfo) loadinfo->shdr = (FAR Elf_Shdr *)lib_malloc(shdrsize); if (!loadinfo->shdr) { - berr("ERROR: Failed to allocate the section header table. Size: %ld\n", - (long)shdrsize); + berr("ERROR: Failed to allocate the section header table. Size: %zu\n", + shdrsize); return -ENOMEM; } @@ -116,9 +116,9 @@ int modlib_loadhdrs(FAR struct mod_loadinfo_s *loadinfo) loadinfo->phdr = (FAR Elf_Phdr *)lib_malloc(phdrsize); if (!loadinfo->phdr) { - lib_free((FAR void *)loadinfo->shdr); + lib_free(loadinfo->shdr); berr("ERROR: Failed to allocate the program header table." - "Size: %ld\n", (long)phdrsize); + "Size: %zu\n", phdrsize); return -ENOMEM; } @@ -129,8 +129,8 @@ int modlib_loadhdrs(FAR struct mod_loadinfo_s *loadinfo) if (ret < 0) { berr("ERROR: Failed to read program header table: %d\n", ret); - lib_free((FAR void *)loadinfo->phdr); - lib_free((FAR void *)loadinfo->shdr); + lib_free(loadinfo->phdr); + lib_free(loadinfo->shdr); } } else diff --git a/libs/libc/modlib/modlib_read.c b/libs/libc/modlib/modlib_read.c index 0084c4866d181..87e7d087a2f33 100644 --- a/libs/libc/modlib/modlib_read.c +++ b/libs/libc/modlib/modlib_read.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -48,19 +49,19 @@ * Name: modlib_dumpreaddata ****************************************************************************/ -#if defined(ELF_DUMP_READDATA) -static inline void modlib_dumpreaddata(FAR char *buffer, int buflen) +#ifdef ELF_DUMP_READDATA +static inline void modlib_dumpreaddata(FAR char *buffer, size_t buflen) { FAR uint32_t *buf32 = (FAR uint32_t *)buffer; - int i; - int j; + size_t i; + size_t j; for (i = 0; i < buflen; i += 32) { - syslog(LOG_DEBUG, "%04x:", i); + syslog(LOG_DEBUG, "%04zx:", i); for (j = 0; j < 32; j += sizeof(uint32_t)) { - syslog(LOG_DEBUG, " %08x", *buf32++); + syslog(LOG_DEBUG, " %08" PRIx32, *buf32++); } syslog(LOG_DEBUG, "\n"); @@ -90,10 +91,11 @@ static inline void modlib_dumpreaddata(FAR char *buffer, int buflen) int modlib_read(FAR struct mod_loadinfo_s *loadinfo, FAR uint8_t *buffer, size_t readsize, off_t offset) { + size_t nsize = readsize; ssize_t nbytes; /* Number of bytes read */ off_t rpos; /* Position returned by lseek */ - binfo("Read %ld bytes from offset %ld\n", (long)readsize, (long)offset); + binfo("Read %zu bytes from offset %" PRIdOFF "\n", readsize, offset); /* Loop until all of the requested data has been read. */ @@ -105,14 +107,15 @@ int modlib_read(FAR struct mod_loadinfo_s *loadinfo, FAR uint8_t *buffer, if (rpos != offset) { int errval = _NX_GETERRNO(rpos); - berr("ERROR: Failed to seek to position %lu: %d\n", - (unsigned long)offset, errval); + berr("ERROR: Failed to seek to position %" PRIdOFF ": %d\n", + offset, errval); return -errval; } /* Read the file data at offset into the user buffer */ - nbytes = _NX_READ(loadinfo->filfd, buffer, readsize); + nbytes = _NX_READ(loadinfo->filfd, + buffer + nsize - readsize, readsize); if (nbytes < 0) { int errval = _NX_GETERRNO(nbytes); @@ -121,8 +124,8 @@ int modlib_read(FAR struct mod_loadinfo_s *loadinfo, FAR uint8_t *buffer, if (errval != EINTR) { - berr("ERROR: Read from offset %lu failed: %d\n", - (unsigned long)offset, errval); + berr("ERROR: Read from offset %" PRIdOFF " failed: %d\n", + offset, errval); return -errval; } } @@ -134,11 +137,10 @@ int modlib_read(FAR struct mod_loadinfo_s *loadinfo, FAR uint8_t *buffer, else { readsize -= nbytes; - buffer += nbytes; offset += nbytes; } } - modlib_dumpreaddata(buffer, readsize); + modlib_dumpreaddata(buffer, nsize); return OK; } diff --git a/libs/libc/modlib/modlib_registry.c b/libs/libc/modlib/modlib_registry.c index 6194de11c5a82..1453ec989905e 100644 --- a/libs/libc/modlib/modlib_registry.c +++ b/libs/libc/modlib/modlib_registry.c @@ -45,7 +45,6 @@ ****************************************************************************/ static rmutex_t g_modlock = NXRMUTEX_INITIALIZER; - static FAR struct module_s *g_mod_registry; /**************************************************************************** diff --git a/libs/libc/modlib/modlib_sections.c b/libs/libc/modlib/modlib_sections.c index 6b95d7c87cad9..917f6b4958b2e 100644 --- a/libs/libc/modlib/modlib_sections.c +++ b/libs/libc/modlib/modlib_sections.c @@ -29,8 +29,6 @@ #include #include -#include - #include "libc.h" #include "modlib/modlib.h" @@ -54,10 +52,8 @@ static inline int modlib_sectname(FAR struct mod_loadinfo_s *loadinfo, FAR const Elf_Shdr *shdr) { FAR Elf_Shdr *shstr; - FAR uint8_t *buffer; off_t offset; - size_t readlen; - size_t bytesread; + size_t bytesread = 0; int shstrndx; int ret; @@ -101,13 +97,13 @@ static inline int modlib_sectname(FAR struct mod_loadinfo_s *loadinfo, /* Loop until we get the entire section name into memory */ - bytesread = 0; - for (; ; ) { + FAR uint8_t *buffer = &loadinfo->iobuffer[bytesread]; + size_t readlen = loadinfo->buflen - bytesread; + /* Get the number of bytes to read */ - readlen = loadinfo->buflen - bytesread; if (offset + readlen > loadinfo->filelen) { if (loadinfo->filelen <= offset) @@ -121,7 +117,6 @@ static inline int modlib_sectname(FAR struct mod_loadinfo_s *loadinfo, /* Read that number of bytes into the array */ - buffer = &loadinfo->iobuffer[bytesread]; ret = modlib_read(loadinfo, buffer, readlen, offset); if (ret < 0) { @@ -178,8 +173,6 @@ static inline int modlib_sectname(FAR struct mod_loadinfo_s *loadinfo, int modlib_findsection(FAR struct mod_loadinfo_s *loadinfo, FAR const char *sectname) { - FAR const Elf_Shdr *shdr; - int ret; int i; /* Search through the shdr[] array in loadinfo for a section named @@ -188,10 +181,11 @@ int modlib_findsection(FAR struct mod_loadinfo_s *loadinfo, for (i = 0; i < loadinfo->ehdr.e_shnum; i++) { + FAR const Elf_Shdr *shdr = &loadinfo->shdr[i]; + /* Get the name of this section */ - shdr = &loadinfo->shdr[i]; - ret = modlib_sectname(loadinfo, shdr); + int ret = modlib_sectname(loadinfo, shdr); if (ret < 0) { berr("ERROR: modlib_sectname failed: %d\n", ret); diff --git a/libs/libc/modlib/modlib_symtab.c b/libs/libc/modlib/modlib_symtab.c index 203c4e5d9b8ef..fbf8bbcec4a4b 100644 --- a/libs/libc/modlib/modlib_symtab.c +++ b/libs/libc/modlib/modlib_symtab.c @@ -28,6 +28,7 @@ #include #include +#include /**************************************************************************** * Pre-processor Definitions diff --git a/libs/libc/modlib/modlib_uninit.c b/libs/libc/modlib/modlib_uninit.c index 2902696d6cdca..06d18f8b3e541 100644 --- a/libs/libc/modlib/modlib_uninit.c +++ b/libs/libc/modlib/modlib_uninit.c @@ -50,7 +50,7 @@ * ****************************************************************************/ -int modlib_uninitialize(struct mod_loadinfo_s *loadinfo) +int modlib_uninitialize(FAR struct mod_loadinfo_s *loadinfo) { /* Free all working buffers */ @@ -78,27 +78,27 @@ int modlib_uninitialize(struct mod_loadinfo_s *loadinfo) * ****************************************************************************/ -int modlib_freebuffers(struct mod_loadinfo_s *loadinfo) +int modlib_freebuffers(FAR struct mod_loadinfo_s *loadinfo) { /* Release all working allocations */ if (loadinfo->shdr != NULL) { - lib_free((FAR void *)loadinfo->shdr); - loadinfo->shdr = NULL; + lib_free(loadinfo->shdr); + loadinfo->shdr = NULL; } if (loadinfo->phdr != NULL) { - lib_free((FAR void *)loadinfo->phdr); - loadinfo->phdr = NULL; + lib_free(loadinfo->phdr); + loadinfo->phdr = NULL; } if (loadinfo->iobuffer != NULL) { - lib_free((FAR void *)loadinfo->iobuffer); - loadinfo->iobuffer = NULL; - loadinfo->buflen = 0; + lib_free(loadinfo->iobuffer); + loadinfo->iobuffer = NULL; + loadinfo->buflen = 0; } return OK; diff --git a/libs/libc/modlib/modlib_unload.c b/libs/libc/modlib/modlib_unload.c index 3ac6026dd0da7..03c59c65716e2 100644 --- a/libs/libc/modlib/modlib_unload.c +++ b/libs/libc/modlib/modlib_unload.c @@ -50,7 +50,7 @@ * ****************************************************************************/ -int modlib_unload(struct mod_loadinfo_s *loadinfo) +int modlib_unload(FAR struct mod_loadinfo_s *loadinfo) { /* Free all working buffers */ diff --git a/libs/libc/modlib/modlib_verify.c b/libs/libc/modlib/modlib_verify.c index 29e77a958b6b5..d1aea6b1fa39c 100644 --- a/libs/libc/modlib/modlib_verify.c +++ b/libs/libc/modlib/modlib_verify.c @@ -29,7 +29,8 @@ #include #include -#include + +#include "modlib/modlib.h" /**************************************************************************** * Private Constant Data @@ -78,13 +79,10 @@ int modlib_verifyheader(FAR const Elf_Ehdr *ehdr) /* Verify that this is a relocatable file */ - if (ehdr->e_type != ET_REL) + if (ehdr->e_type != ET_REL && ehdr->e_type != ET_DYN) { - if (ehdr->e_type != ET_DYN) - { - berr("ERROR: Not a relocatable file: e_type=%d\n", ehdr->e_type); - return -EINVAL; - } + berr("ERROR: Not a relocatable file: e_type=%d\n", ehdr->e_type); + return -EINVAL; } /* Verify that this file works with the currently configured architecture */ diff --git a/sched/module/mod_insmod.c b/sched/module/mod_insmod.c index 7faff98b96c8d..c32c6b6561c6e 100644 --- a/sched/module/mod_insmod.c +++ b/sched/module/mod_insmod.c @@ -188,6 +188,7 @@ FAR void *insmod(FAR const char *filename, FAR const char *modname) if (modp == NULL) { berr("Failed to allocate struct module_s\n"); + ret = -ENOMEM; goto errout_with_loadinfo; } @@ -218,11 +219,11 @@ FAR void *insmod(FAR const char *filename, FAR const char *modname) /* Save the load information */ - modp->textalloc = (FAR void *)loadinfo.textalloc; - modp->dataalloc = (FAR void *)loadinfo.datastart; + modp->textalloc = (FAR void *)loadinfo.textalloc; + modp->dataalloc = (FAR void *)loadinfo.datastart; #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE) - modp->textsize = loadinfo.textsize; - modp->datasize = loadinfo.datasize; + modp->textsize = loadinfo.textsize; + modp->datasize = loadinfo.datasize; #endif /* Get the module initializer entry point */ @@ -249,7 +250,7 @@ FAR void *insmod(FAR const char *filename, FAR const char *modname) modlib_uninitialize(&loadinfo); modlib_registry_unlock(); - return (FAR void *)modp; + return modp; errout_with_load: modlib_unload(&loadinfo); diff --git a/sched/module/mod_modhandle.c b/sched/module/mod_modhandle.c index 67426b30fc4cb..623bc208389e5 100644 --- a/sched/module/mod_modhandle.c +++ b/sched/module/mod_modhandle.c @@ -76,7 +76,7 @@ FAR void *modhandle(FAR const char *name) } modlib_registry_unlock(); - return (FAR void *)modp; + return modp; } #endif /* CONFIG_MODULE */ diff --git a/sched/module/mod_procfs.c b/sched/module/mod_procfs.c index 299af8e40d147..2a89e6302f989 100644 --- a/sched/module/mod_procfs.c +++ b/sched/module/mod_procfs.c @@ -148,7 +148,7 @@ static int modprocfs_callback(FAR struct module_s *modp, FAR void *arg) priv->buffer += copysize; priv->remaining -= copysize; - return (priv->totalsize >= priv->buflen) ? 1 : 0; + return priv->totalsize >= priv->buflen; } /**************************************************************************** @@ -188,7 +188,7 @@ static int modprocfs_open(FAR struct file *filep, FAR const char *relpath, * filep->f_priv. */ - filep->f_priv = (FAR void *)priv; + filep->f_priv = priv; return OK; } @@ -222,7 +222,7 @@ static ssize_t modprocfs_read(FAR struct file *filep, FAR char *buffer, FAR struct modprocfs_file_s *priv; int ret; - finfo("buffer=%p buflen=%lu\n", buffer, (unsigned long)buflen); + finfo("buffer=%p buflen=%zu\n", buffer, buflen); /* Recover our private data from the struct file instance */ @@ -283,7 +283,7 @@ static int modprocfs_dup(FAR const struct file *oldp, FAR struct file *newp) /* Save the new attributes in the new file structure */ - newp->f_priv = (FAR void *)newpriv; + newp->f_priv = newpriv; return OK; } @@ -297,7 +297,7 @@ static int modprocfs_dup(FAR const struct file *oldp, FAR struct file *newp) static int modprocfs_stat(FAR const char *relpath, FAR struct stat *buf) { memset(buf, 0, sizeof(struct stat)); - buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR; + buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR; return OK; } diff --git a/sched/module/mod_rmmod.c b/sched/module/mod_rmmod.c index 490beaf4e6cda..2dccf3840e564 100644 --- a/sched/module/mod_rmmod.c +++ b/sched/module/mod_rmmod.c @@ -121,11 +121,11 @@ int rmmod(FAR void *handle) */ #if defined(CONFIG_ARCH_USE_TEXT_HEAP) - up_textheap_free((FAR void *)modp->textalloc); + up_textheap_free(modp->textalloc); #else - kmm_free((FAR void *)modp->textalloc); + kmm_free(modp->textalloc); #endif - kmm_free((FAR void *)modp->dataalloc); + kmm_free(modp->dataalloc); modp->textalloc = NULL; modp->dataalloc = NULL; #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)