2012-10-29 21:43:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libelf/libelf_dtors.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 <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/binfmt/elf.h>
|
|
|
|
|
|
|
|
#include "libelf.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 16:29:03 +02:00
|
|
|
* Pre-processor Definitions
|
2012-10-29 21:43:35 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_loaddtors
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Load pointers to static destructors 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_loaddtors(FAR struct elf_loadinfo_s *loadinfo)
|
|
|
|
{
|
|
|
|
FAR Elf32_Shdr *shdr;
|
|
|
|
size_t dtorsize;
|
|
|
|
int dtoridx;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
DEBUGASSERT(loadinfo->dtors == NULL);
|
|
|
|
|
|
|
|
/* Allocate an I/O buffer if necessary. This buffer is used by
|
|
|
|
* elf_sectname() to accumulate the variable length symbol name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = elf_allocbuffer(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("elf_allocbuffer failed: %d\n", ret);
|
2012-10-29 21:43:35 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the index to the section named ".dtors." NOTE: On old ABI system,
|
|
|
|
* .dtors is the name of the section containing the list of destructors;
|
2014-04-14 00:22:22 +02:00
|
|
|
* On newer systems, the similar section is called .fini_array. It is
|
2012-10-29 21:43:35 +01:00
|
|
|
* expected that the linker script will force the section name to be ".dtors"
|
|
|
|
* in either case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dtoridx = elf_findsection(loadinfo, ".dtors");
|
|
|
|
if (dtoridx < 0)
|
|
|
|
{
|
|
|
|
/* This may not be a failure. -ENOENT indicates that the file has no
|
|
|
|
* static destructor section.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("elf_findsection .dtors section failed: %d\n", dtoridx);
|
2012-10-29 21:43:35 +01:00
|
|
|
return ret == -ENOENT ? OK : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we can get a pointer to the .dtor section in the section header
|
|
|
|
* table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
shdr = &loadinfo->shdr[dtoridx];
|
|
|
|
|
|
|
|
/* Get the size of the .dtor section and the number of destructors that
|
|
|
|
* will need to be called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dtorsize = shdr->sh_size;
|
|
|
|
loadinfo->ndtors = dtorsize / sizeof(binfmt_dtor_t);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("dtoridx=%d dtorsize=%d sizeof(binfmt_dtor_t)=%d ndtors=%d\n",
|
2012-10-29 21:43:35 +01:00
|
|
|
dtoridx, dtorsize, sizeof(binfmt_dtor_t), loadinfo->ndtors);
|
|
|
|
|
|
|
|
/* Check if there are any destructors. It is not an error if there
|
|
|
|
* are none.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (loadinfo->ndtors > 0)
|
|
|
|
{
|
|
|
|
/* Check an assumption that we made above */
|
|
|
|
|
|
|
|
DEBUGASSERT(shdr->sh_size == loadinfo->ndtors * sizeof(binfmt_dtor_t));
|
|
|
|
|
|
|
|
/* In the old ABI, the .dtors section is not allocated. In that case,
|
|
|
|
* we need to allocate memory to hold the .dtors and then copy the
|
|
|
|
* from the file into the allocated memory.
|
|
|
|
*
|
|
|
|
* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) == 0)
|
|
|
|
{
|
|
|
|
/* Allocate memory to hold a copy of the .dtor section */
|
|
|
|
|
2014-09-11 02:44:09 +02:00
|
|
|
loadinfo->dtoralloc = (binfmt_dtor_t *)kumm_malloc(dtorsize);
|
|
|
|
if (!loadinfo->dtoralloc)
|
2012-10-29 21:43:35 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to allocate memory for .dtors\n");
|
2012-10-29 21:43:35 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2014-09-11 02:44:09 +02:00
|
|
|
loadinfo->dtors = (binfmt_dtor_t *)loadinfo->dtoralloc;
|
2012-10-29 21:43:35 +01:00
|
|
|
|
|
|
|
/* Read the section header table into memory */
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
ret = elf_read(loadinfo, (FAR uint8_t *)loadinfo->dtors, dtorsize,
|
2012-10-29 21:43:35 +01:00
|
|
|
shdr->sh_offset);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to allocate .dtors: %d\n", ret);
|
2012-10-29 21:43:35 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fix up all of the .dtor addresses. Since the addresses
|
|
|
|
* do not lie in allocated memory, there will be no relocation
|
|
|
|
* section for them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < loadinfo->ndtors; i++)
|
|
|
|
{
|
|
|
|
FAR uintptr_t *ptr = (uintptr_t *)((FAR void *)(&loadinfo->dtors)[i]);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("dtor %d: %08lx + %08lx = %08lx\n",
|
2014-08-24 19:54:14 +02:00
|
|
|
i, *ptr, (unsigned long)loadinfo->textalloc,
|
|
|
|
(unsigned long)(*ptr + loadinfo->textalloc));
|
2012-10-29 21:43:35 +01:00
|
|
|
|
2014-08-24 19:54:14 +02:00
|
|
|
*ptr += loadinfo->textalloc;
|
2012-10-29 21:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save the address of the .dtors (actually, .init_array) where it was
|
|
|
|
* loaded into memory. Since the .dtors lie in allocated memory, they
|
|
|
|
* will be relocated via the normal mechanism.
|
|
|
|
*/
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
loadinfo->dtors = (binfmt_dtor_t *)shdr->sh_addr;
|
2012-10-29 21:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_BINFMT_CONSTRUCTORS */
|