diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-10-29 00:52:23 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-10-29 00:52:23 +0000 |
commit | ade5fb42679b7e890f29487ca850c65f9944fa1e (patch) | |
tree | 84f10662c214892091c5b7d5dce796b6f736c6b4 /nuttx/binfmt | |
parent | 09dcebb6991350498b809a20b55fbcfd982a0b2a (diff) | |
download | px4-nuttx-ade5fb42679b7e890f29487ca850c65f9944fa1e.tar.gz px4-nuttx-ade5fb42679b7e890f29487ca850c65f9944fa1e.tar.bz2 px4-nuttx-ade5fb42679b7e890f29487ca850c65f9944fa1e.zip |
Finish implemention of ELF loader static constructor support. Still some issues.
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5272 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/binfmt')
-rw-r--r-- | nuttx/binfmt/elf.c | 1 | ||||
-rw-r--r-- | nuttx/binfmt/libelf/Make.defs | 2 | ||||
-rw-r--r-- | nuttx/binfmt/libelf/libelf.h | 45 | ||||
-rw-r--r-- | nuttx/binfmt/libelf/libelf_bind.c | 15 | ||||
-rw-r--r-- | nuttx/binfmt/libelf/libelf_ctors.c | 274 | ||||
-rw-r--r-- | nuttx/binfmt/libelf/libelf_iobuffer.c | 136 | ||||
-rw-r--r-- | nuttx/binfmt/libelf/libelf_load.c | 29 | ||||
-rw-r--r-- | nuttx/binfmt/libelf/libelf_symbols.c | 17 | ||||
-rw-r--r-- | nuttx/binfmt/libelf/libelf_uninit.c | 50 | ||||
-rw-r--r-- | nuttx/binfmt/libelf/libelf_unload.c | 21 |
10 files changed, 513 insertions, 77 deletions
diff --git a/nuttx/binfmt/elf.c b/nuttx/binfmt/elf.c index 9a0ac1873..57123ed95 100644 --- a/nuttx/binfmt/elf.c +++ b/nuttx/binfmt/elf.c @@ -116,6 +116,7 @@ static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo) bdbg(" filelen: %ld\n", (long)loadinfo->filelen); #ifdef CONFIG_ELF_CONSTRUCTORS bdbg(" ctors: %08lx\n", (long)loadinfo->ctors); + bdbg(" nctors: %d\n", loadinfo->nctors); #endif bdbg(" filfd: %d\n", loadinfo->filfd); bdbg(" symtabidx: %d\n", loadinfo->symtabidx); diff --git a/nuttx/binfmt/libelf/Make.defs b/nuttx/binfmt/libelf/Make.defs index a70a127f8..cf2507a99 100644 --- a/nuttx/binfmt/libelf/Make.defs +++ b/nuttx/binfmt/libelf/Make.defs @@ -43,7 +43,7 @@ BINFMT_CSRCS += elf.c BINFMT_CSRCS += libelf_init.c libelf_uninit.c libelf_load.c \ libelf_unload.c libelf_verify.c libelf_read.c \ - libelf_bind.c libelf_symbols.c + libelf_bind.c libelf_symbols.c libelf_iobuffer.c ifeq ($(CONFIG_ELF_CONSTRUCTORS),y) BINFMT_CSRCS += libelf_ctors.c diff --git a/nuttx/binfmt/libelf/libelf.h b/nuttx/binfmt/libelf/libelf.h index 0fb1362dc..0d13515cc 100644 --- a/nuttx/binfmt/libelf/libelf.h +++ b/nuttx/binfmt/libelf/libelf.h @@ -142,6 +142,49 @@ int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf32_Sym *sym, FAR const struct symtab_s *exports, int nexports); /**************************************************************************** + * Name: elf_freebuffers + * + * Description: + * Release all working buffers. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_freebuffers(FAR struct elf_loadinfo_s *loadinfo); + +/**************************************************************************** + * Name: elf_allocbuffer + * + * Description: + * Perform the initial allocation of the I/O buffer, if it has not already + * been allocated. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_allocbuffer(FAR struct elf_loadinfo_s *loadinfo); + +/**************************************************************************** + * Name: elf_reallocbuffer + * + * Description: + * Increase the size of I/O buffer by the specified buffer increment. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_reallocbuffer(FAR struct elf_loadinfo_s *loadinfo, size_t increment); + +/**************************************************************************** * Name: elf_findctors * * Description: @@ -157,7 +200,7 @@ int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf32_Sym *sym, ****************************************************************************/ #ifdef CONFIG_ELF_CONSTRUCTORS -int elf_findctors(FAR struct elf_loadinfo_s *loadinfo); +int elf_loadctors(FAR struct elf_loadinfo_s *loadinfo); #endif /**************************************************************************** diff --git a/nuttx/binfmt/libelf/libelf_bind.c b/nuttx/binfmt/libelf/libelf_bind.c index 54ea8f1f0..ef1b4fc8f 100644 --- a/nuttx/binfmt/libelf/libelf_bind.c +++ b/nuttx/binfmt/libelf/libelf_bind.c @@ -46,7 +46,6 @@ #include <assert.h> #include <debug.h> -#include <nuttx/kmalloc.h> #include <nuttx/binfmt/elf.h> #include <nuttx/binfmt/symtab.h> @@ -247,17 +246,16 @@ int elf_bind(FAR struct elf_loadinfo_s *loadinfo, return ret; } - /* Allocate an I/O buffer. This buffer is used only by elf_symname() to + /* Allocate an I/O buffer. This buffer is used by elf_symname() to * accumulate the variable length symbol name. */ - loadinfo->iobuffer = (FAR uint8_t *)kmalloc(CONFIG_ELF_BUFFERSIZE); - if (!loadinfo->iobuffer) + ret = elf_allocbuffer(loadinfo); + if (ret < 0) { - bdbg("Failed to allocate an I/O buffer\n"); + bdbg("elf_allocbuffer failed: %d\n", ret); return -ENOMEM; } - loadinfo->buflen = CONFIG_ELF_BUFFERSIZE; /* Process relocations in every allocated section */ @@ -303,11 +301,6 @@ int elf_bind(FAR struct elf_loadinfo_s *loadinfo, arch_flushicache((FAR void*)loadinfo->alloc, loadinfo->allocsize); #endif - /* Free the I/O buffer */ - - kfree(loadinfo->iobuffer); - loadinfo->iobuffer = NULL; - loadinfo->buflen = 0; return ret; } diff --git a/nuttx/binfmt/libelf/libelf_ctors.c b/nuttx/binfmt/libelf/libelf_ctors.c index e8b095bb7..c53923d44 100644 --- a/nuttx/binfmt/libelf/libelf_ctors.c +++ b/nuttx/binfmt/libelf/libelf_ctors.c @@ -39,11 +39,15 @@ #include <nuttx/config.h> +#include <string.h> +#include <errno.h> +#include <assert.h> #include <debug.h> +#include <nuttx/kmalloc.h> #include <nuttx/binfmt/elf.h> -#include "libelf" +#include "libelf.h" #ifdef CONFIG_ELF_CONSTRUCTORS @@ -55,8 +59,6 @@ * Private Types ****************************************************************************/ -typedef FAR void (*ctor_t)(void); - /**************************************************************************** * Private Constant Data ****************************************************************************/ @@ -66,9 +68,111 @@ typedef FAR void (*ctor_t)(void); ****************************************************************************/ /**************************************************************************** - * Public Functions + * Name: elf_sectname + * + * Description: + * Get the symbol name in loadinfo->iobuffer[]. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * ****************************************************************************/ +static inline int elf_sectname(FAR struct elf_loadinfo_s *loadinfo, + FAR const Elf32_Shdr *shdr) +{ + FAR Elf32_Shdr *shstr; + FAR uint8_t *buffer; + off_t offset; + size_t readlen; + size_t bytesread; + int shstrndx; + int ret; + + /* Get the section header table index of the entry associated with the + * section name string table. If the file has no section name string table, + * this member holds the value SH_UNDEF. + */ + + shstrndx = loadinfo->ehdr.e_shstrndx; + if (shstrndx == SHN_UNDEF) + { + bdbg("No section header string table\n"); + return -EINVAL; + } + + /* Get the section name string table section header */ + + shstr = &loadinfo->shdr[shstrndx]; + + /* Get the file offset to the string that is the name of the section. This + * is the sum of: + * + * shstr->sh_offset: The file offset to the first byte of the section + * header string table data. + * shdr->sh_name: The offset to the name of the section in the section + * name table + */ + + offset = shstr->sh_offset + shdr->sh_name; + + /* Loop until we get the entire section name into memory */ + + buffer = loadinfo->iobuffer; + bytesread = 0; + + for (;;) + { + /* Get the number of bytes to read */ + + readlen = loadinfo->buflen - bytesread; + if (offset + readlen > loadinfo->filelen) + { + readlen = loadinfo->filelen - offset; + if (readlen <= 0) + { + bdbg("At end of file\n"); + return -EINVAL; + } + } + + /* Read that number of bytes into the array */ + + buffer = &loadinfo->iobuffer[bytesread]; + ret = elf_read(loadinfo, buffer, readlen, offset); + if (ret < 0) + { + bdbg("Failed to read section name\n"); + return ret; + } + + bytesread += readlen; + + /* Did we read the NUL terminator? */ + + if (memchr(buffer, '\0', readlen) != NULL) + { + /* Yes, the buffer contains a NUL terminator. */ + + return OK; + } + + /* No.. then we have to read more */ + + ret = elf_reallocbuffer(loadinfo, CONFIG_ELF_BUFFERINCR); + if (ret < 0) + { + bdbg("elf_reallocbuffer failed: %d\n", ret); + return ret; + } + } + + /* We will not get here */ + + return OK; +} + /**************************************************************************** * Name: elf_findctors * @@ -79,27 +183,157 @@ typedef FAR void (*ctor_t)(void); * loadinfo - Load state information * * Returned Value: + * On success, the index to the CTOR section is returned; A negated errno + * value is returned on failure. + * + ****************************************************************************/ + +static inline int elf_findctors(FAR struct elf_loadinfo_s *loadinfo) +{ + FAR const Elf32_Shdr *shdr; + int ret; + int i; + + /* Search through the shdr[] array in loadinfo for a section named .ctors */ + + for (i = 0; i < loadinfo->ehdr.e_shnum; i++) + { + /* Get the name of this section */ + + shdr = &loadinfo->shdr[i]; + ret = elf_sectname(loadinfo, shdr); + if (ret < 0) + { + bdbg("elf_sectname failed: %d\n", ret); + return ret; + } + + /* Check if the name of this section if ".ctors" */ + + bvdbg("%d. Comparing \"%s\" and .ctors\"\n", i, loadinfo->iobuffer); + + if (strcmp(".ctors", (FAR const char *)loadinfo->iobuffer) == 0) + { + /* We found it... return the index */ + + return i; + } + } + + /* We failed to find the .ctors sections. This may not be an error; maybe + * there are no static constructors. + */ + + return -ENOENT; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_loadctors + * + * Description: + * Load points to static constructors into an in-memory array. + * + * Input Parameters: + * loadinfo - Load state information + * + * Returned Value: * 0 (OK) is returned on success and a negated errno is returned on * failure. * ****************************************************************************/ -int elf_findctors(FAR struct elf_loadinfo_s *loadinfo) +int elf_loadctors(FAR struct elf_loadinfo_s *loadinfo) { - /* Search through the shdr[] array in loadinfo for a section named .ctors */ -#warning "Missing logic" + FAR Elf32_Shdr *shdr; + size_t ctorsize; + int ctoridx; + int ret; + int i; + + DEBUGASSERT(loadinfo->ctors == NULL); + + /* Allocate an I/O buffer. This buffer is used by elf_sectname() to + * accumulate the variable length symbol name. + */ + + ret = elf_allocbuffer(loadinfo); + if (ret < 0) + { + bdbg("elf_allocbuffer failed: %d\n", ret); + return -ENOMEM; + } + + /* Find the index to the section named ".ctors" */ + + ctoridx = elf_findctors(loadinfo); + if (ctoridx < 0) + { + /* This may not be a failure. -ENOENT indicates that the file has no + * static constructor section. + */ - /* Get the address of the beginning of the constructros from the sh_addr - * field of the section. Save that in the ctors field of the loadinfo - * structure. + bvdbg("elf_findctors failed: %d\n", ctoridx); + return ret == -ENOENT ? OK : ret; + } + + /* Now we can get a pointer to the .ctor section in the section header + * table. */ -#warning "Missing logic" - /* Get the number of constructors from the sh_size field of the section. - * Save that number in the nctors field of the loadinfo structure. - */ -#warning "Missing logic" - return -ENOSYS; + shdr = &loadinfo->shdr[ctoridx]; + + /* Allocate memory to hold a copy of the .ctor section */ + + ctorsize = shdr->sh_size; + loadinfo->nctors = ctorsize / sizeof(elf_ctor_t); + + bvdbg("ctoridx=%d ctorsize=%d sizeof(elf_ctor_t)=%d nctors=%d\n", + ctoridx, ctorsize, sizeof(elf_ctor_t), loadinfo->nctors); + + /* Check if there are any constructors. It is not an error if there + * are none. + */ + + if (loadinfo->nctors > 0) + { + /* Check an assumption that we made above */ + + DEBUGASSERT(shdr->sh_entsize == sizeof(elf_ctor_t)); + + loadinfo->ctors = (elf_ctor_t)kmalloc(ctorsize); + if (!loadinfo->ctors) + { + bdbg("Failed to allocate memory for .ctors\n"); + return -ENOMEM; + } + + /* Read the section header table into memory */ + + ret = elf_read(loadinfo, (FAR uint8_t*)loadinfo->ctors, ctorsize, + shdr->sh_offset); + if (ret < 0) + { + bdbg("Failed to allocate .ctors: %d\n", ret); + } + + /* Fix up all of the .ctor addresses */ + + for (i = 0; i < loadinfo->nctors; i++) + { + FAR uintptr_t *ptr = (uintptr_t *)((FAR void *)(&loadinfo->ctors)[i]); + + bvdbg("ctor %d: %08lx + %08lx = %08lx\n", i, + *ptr, loadinfo->alloc, *ptr + loadinfo->alloc); + + *ptr += loadinfo->alloc; + } + } + + return OK; } /**************************************************************************** @@ -119,16 +353,20 @@ int elf_findctors(FAR struct elf_loadinfo_s *loadinfo) int elf_doctors(FAR struct elf_loadinfo_s *loadinfo) { - ctor_t ctor = (ctor_t)loadinfo->ctors; + elf_ctor_t ctor = (elf_ctor_t)loadinfo->ctors; int i; /* Execute each constructor */ for (i = 0; i < loadinfo->nctors; i++) { + bvdbg("Calling ctor %d at %p\n", i, (FAR void *)ctor); + ctor(); ctor++; } + + return OK; } -#endif /* CONFIG_ELF_CONSTRUCTORS +#endif /* CONFIG_ELF_CONSTRUCTORS */ diff --git a/nuttx/binfmt/libelf/libelf_iobuffer.c b/nuttx/binfmt/libelf/libelf_iobuffer.c new file mode 100644 index 000000000..ead99ca09 --- /dev/null +++ b/nuttx/binfmt/libelf/libelf_iobuffer.c @@ -0,0 +1,136 @@ +/**************************************************************************** + * binfmt/libelf/elf_iobuffer.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <debug.h> +#include <errno.h> + +#include <nuttx/kmalloc.h> +#include <nuttx/binfmt/elf.h> + +#include "libelf.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: elf_allocbuffer + * + * Description: + * Perform the initial allocation of the I/O buffer, if it has not already + * been allocated. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_allocbuffer(FAR struct elf_loadinfo_s *loadinfo) +{ + /* Has a buffer been allocated> */ + + if (!loadinfo->iobuffer) + { + /* No.. allocate one now */ + + loadinfo->iobuffer = (FAR uint8_t *)kmalloc(CONFIG_ELF_BUFFERSIZE); + if (!loadinfo->iobuffer) + { + bdbg("Failed to allocate an I/O buffer\n"); + return -ENOMEM; + } + + loadinfo->buflen = CONFIG_ELF_BUFFERSIZE; + } + + return OK; +} + +/**************************************************************************** + * Name: elf_reallocbuffer + * + * Description: + * Increase the size of I/O buffer by the specified buffer increment. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_reallocbuffer(FAR struct elf_loadinfo_s *loadinfo, size_t increment) +{ + FAR void *buffer; + size_t newsize; + + /* Get the new size of the allocation */ + + newsize = loadinfo->buflen + increment; + + /* And perform the reallocation */ + + buffer = krealloc((FAR void *)loadinfo->iobuffer, newsize); + if (!buffer) + { + bdbg("Failed to reallocate the I/O buffer\n"); + return -ENOMEM; + } + + /* Save the new buffer info */ + + loadinfo->iobuffer = buffer; + loadinfo->buflen = newsize; + return OK; +} + diff --git a/nuttx/binfmt/libelf/libelf_load.c b/nuttx/binfmt/libelf/libelf_load.c index 6526004f7..505e2c813 100644 --- a/nuttx/binfmt/libelf/libelf_load.c +++ b/nuttx/binfmt/libelf/libelf_load.c @@ -126,14 +126,8 @@ static inline int elf_loadshdrs(FAR struct elf_loadinfo_s *loadinfo) if (ret < 0) { bdbg("Failed to read section header table: %d\n", ret); - goto errout_with_alloc; } - return OK; - -errout_with_alloc: - kfree(loadinfo->shdr); - loadinfo->shdr = 0; return ret; } @@ -233,7 +227,7 @@ static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo) if (ret < 0) { bdbg("Failed to read section %d: %d\n", i, ret); - goto errout_with_alloc; + return ret; } } @@ -248,11 +242,6 @@ static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo) } return OK; - -errout_with_alloc: - kfree((FAR void*)loadinfo->alloc); - loadinfo->alloc = 0; - return ret; } /**************************************************************************** @@ -285,7 +274,7 @@ int elf_load(FAR struct elf_loadinfo_s *loadinfo) if (ret < 0) { bdbg("elf_loadshdrs failed: %d\n", ret); - return ret; + goto errout_with_buffers; } /* Determine total size to allocate */ @@ -298,16 +287,17 @@ int elf_load(FAR struct elf_loadinfo_s *loadinfo) if (ret < 0) { bdbg("elf_loadfile failed: %d\n", ret); - goto errout_with_shdrs; + goto errout_with_buffers; } /* Find static constructors. */ #ifdef CONFIG_ELF_CONSTRUCTORS - ret = elf_findctors(loadinfo); + ret = elf_loadctors(loadinfo); + if (ret < 0) { - bdbg("elf_findctors failed: %d\n", ret); - goto errout_with_shdrs; + bdbg("elf_loadctors failed: %d\n", ret); + goto errout_with_buffers; } #endif @@ -315,9 +305,8 @@ int elf_load(FAR struct elf_loadinfo_s *loadinfo) /* Error exits */ -errout_with_shdrs: - kfree(loadinfo->shdr); - loadinfo->shdr = NULL; +errout_with_buffers: + elf_freebuffers(loadinfo); return ret; } diff --git a/nuttx/binfmt/libelf/libelf_symbols.c b/nuttx/binfmt/libelf/libelf_symbols.c index 123f9f77f..2d94b11af 100644 --- a/nuttx/binfmt/libelf/libelf_symbols.c +++ b/nuttx/binfmt/libelf/libelf_symbols.c @@ -101,7 +101,6 @@ static int elf_symname(FAR struct elf_loadinfo_s *loadinfo, /* Loop until we get the entire symbol name into memory */ - buffer = loadinfo->iobuffer; bytesread = 0; for (;;) @@ -125,7 +124,7 @@ static int elf_symname(FAR struct elf_loadinfo_s *loadinfo, ret = elf_read(loadinfo, buffer, readlen, offset); if (ret < 0) { - bdbg("Failed to read symbol name\n"); + bdbg("elf_read failed: %d\n", ret); return ret; } @@ -142,18 +141,12 @@ static int elf_symname(FAR struct elf_loadinfo_s *loadinfo, /* No.. then we have to read more */ - buffer = realloc((FAR void *)loadinfo->iobuffer, - loadinfo->buflen + CONFIG_ELF_BUFFERINCR); - if (!buffer) + ret = elf_reallocbuffer(loadinfo, CONFIG_ELF_BUFFERINCR); + if (ret < 0) { - bdbg("Failed to reallocate the I/O buffer\n"); - return -ENOMEM; + bdbg("elf_reallocbuffer failed: %d\n", ret); + return ret; } - - /* Save the new buffer info */ - - loadinfo->iobuffer = buffer; - loadinfo->buflen += CONFIG_ELF_BUFFERINCR; } /* We will not get here */ diff --git a/nuttx/binfmt/libelf/libelf_uninit.c b/nuttx/binfmt/libelf/libelf_uninit.c index e5fa2e6c3..06bb89681 100644 --- a/nuttx/binfmt/libelf/libelf_uninit.c +++ b/nuttx/binfmt/libelf/libelf_uninit.c @@ -42,8 +42,12 @@ #include <unistd.h> #include <debug.h> #include <errno.h> + +#include <nuttx/kmalloc.h> #include <nuttx/binfmt/elf.h> +#include "libelf.h" + /**************************************************************************** * Pre-Processor Definitions ****************************************************************************/ @@ -75,6 +79,12 @@ int elf_uninit(struct elf_loadinfo_s *loadinfo) { + /* Free all working buffers */ + + elf_freebuffers(loadinfo); + + /* Close the ELF file */ + if (loadinfo->filfd >= 0) { close(loadinfo->filfd); @@ -83,3 +93,43 @@ int elf_uninit(struct elf_loadinfo_s *loadinfo) return OK; } +/**************************************************************************** + * Name: elf_freebuffers + * + * Description: + * Release all working buffers. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ****************************************************************************/ + +int elf_freebuffers(struct elf_loadinfo_s *loadinfo) +{ + /* Release all working allocations */ + + if (loadinfo->shdr) + { + kfree((FAR void *)loadinfo->shdr); + loadinfo->shdr = NULL; + } + +#ifdef CONFIG_ELF_CONSTRUCTORS + if (loadinfo->ctors) + { + kfree((FAR void *)loadinfo->ctors); + loadinfo->ctors = NULL; + loadinfo->nctors = 0; + } +#endif + + if (loadinfo->iobuffer) + { + kfree((FAR void *)loadinfo->iobuffer); + loadinfo->iobuffer = NULL; + loadinfo->buflen = 0; + } + + return OK; +} diff --git a/nuttx/binfmt/libelf/libelf_unload.c b/nuttx/binfmt/libelf/libelf_unload.c index 335106407..d7a882711 100644 --- a/nuttx/binfmt/libelf/libelf_unload.c +++ b/nuttx/binfmt/libelf/libelf_unload.c @@ -45,6 +45,8 @@ #include <nuttx/kmalloc.h> #include <nuttx/binfmt/elf.h> +#include "libelf.h" + /**************************************************************************** * Pre-Processor Definitions ****************************************************************************/ @@ -76,7 +78,11 @@ int elf_unload(struct elf_loadinfo_s *loadinfo) { - /* Release the all allocated memory */ + /* Free all working buffers */ + + elf_freebuffers(loadinfo); + + /* Release memory holding the relocated ELF image */ if (loadinfo->alloc) { @@ -85,19 +91,6 @@ int elf_unload(struct elf_loadinfo_s *loadinfo) loadinfo->allocsize = 0; } - if (loadinfo->shdr) - { - kfree((FAR void *)loadinfo->shdr); - loadinfo->shdr = NULL; - } - - if (loadinfo->iobuffer) - { - kfree((FAR void *)loadinfo->iobuffer); - loadinfo->iobuffer = NULL; - loadinfo->buflen = 0; - } - return OK; } |