2012-10-24 13:46:12 -03:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libelf/libelf_load.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 <sys/types.h>
|
2012-10-24 20:40:31 -03:00
|
|
|
|
2012-10-24 13:46:12 -03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2012-10-24 20:40:31 -03:00
|
|
|
#include <unistd.h>
|
2012-10-25 23:42:39 -03:00
|
|
|
#include <elf32.h>
|
2012-10-25 13:18:20 -03:00
|
|
|
#include <assert.h>
|
2012-10-24 13:46:12 -03:00
|
|
|
#include <errno.h>
|
2012-10-25 13:18:20 -03:00
|
|
|
#include <debug.h>
|
2012-10-24 13:46:12 -03:00
|
|
|
|
2012-10-24 17:19:44 -03:00
|
|
|
#include <nuttx/binfmt/elf.h>
|
2012-10-24 13:46:12 -03:00
|
|
|
|
2012-10-25 13:18:20 -03:00
|
|
|
#include "libelf.h"
|
|
|
|
|
2012-10-24 13:46:12 -03:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-Processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-24 22:23:03 -03:00
|
|
|
#define ELF_ALIGN_MASK ((1 << CONFIG_ELF_ALIGN_LOG2) - 1)
|
|
|
|
#define ELF_ALIGNUP(a) (((unsigned long)(a) + ELF_ALIGN_MASK) & ~ELF_ALIGN_MASK)
|
|
|
|
#define ELF_ALIGNDOWN(a) ((unsigned long)(a) & ~ELF_ALIGN_MASK)
|
|
|
|
|
|
|
|
|
2012-10-24 13:46:12 -03:00
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(x,y) ((x) > (y) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-24 22:23:03 -03:00
|
|
|
/****************************************************************************
|
2012-10-29 17:43:35 -03:00
|
|
|
* Name: elf_elfsize
|
2012-10-24 22:23:03 -03:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate total memory allocation for the ELF file.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-29 17:43:35 -03:00
|
|
|
static void elf_elfsize(struct elf_loadinfo_s *loadinfo)
|
2012-10-24 22:23:03 -03:00
|
|
|
{
|
2012-10-29 17:43:35 -03:00
|
|
|
size_t elfsize;
|
2012-10-24 22:23:03 -03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Accumulate the size each section into memory that is marked SHF_ALLOC */
|
|
|
|
|
2012-10-29 17:43:35 -03:00
|
|
|
elfsize = 0;
|
2012-10-24 22:23:03 -03:00
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
2012-10-24 20:40:31 -03:00
|
|
|
{
|
2012-10-25 13:18:20 -03:00
|
|
|
FAR Elf32_Shdr *shdr = &loadinfo->shdr[i];
|
2012-10-24 20:40:31 -03:00
|
|
|
|
2012-10-24 22:23:03 -03:00
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution.
|
|
|
|
*/
|
2012-10-24 20:40:31 -03:00
|
|
|
|
2012-10-24 22:23:03 -03:00
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) != 0)
|
|
|
|
{
|
2012-10-29 17:43:35 -03:00
|
|
|
elfsize += ELF_ALIGNUP(shdr->sh_size);
|
2012-10-24 20:40:31 -03:00
|
|
|
}
|
2012-10-24 22:23:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the allocation size */
|
|
|
|
|
2012-10-29 17:43:35 -03:00
|
|
|
loadinfo->elfsize = elfsize;
|
2012-10-24 22:23:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_loadfile
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate memory for the file and read the section data into the
|
2012-10-25 13:18:20 -03:00
|
|
|
* allocated memory. Section addresses in the shdr[] are updated to point
|
|
|
|
* to the corresponding position in the allocated memory.
|
2012-10-24 22:23:03 -03:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-25 13:18:20 -03:00
|
|
|
static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo)
|
2012-10-24 22:23:03 -03:00
|
|
|
{
|
|
|
|
FAR uint8_t *dest;
|
2012-10-25 13:18:20 -03:00
|
|
|
int ret;
|
2012-10-24 22:23:03 -03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Allocate (and zero) memory for the ELF file. */
|
2012-12-19 13:54:26 -04:00
|
|
|
|
|
|
|
ret = elf_addrenv_alloc(loadinfo, loadinfo->elfsize);
|
|
|
|
if (ret < 0)
|
2012-10-24 22:23:03 -03:00
|
|
|
{
|
2012-12-19 13:54:26 -04:00
|
|
|
bdbg("ERROR: elf_addrenv_alloc() failed: %d\n", ret);
|
|
|
|
return ret;
|
2012-10-24 22:23:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read each section into memory that is marked SHF_ALLOC + SHT_NOBITS */
|
|
|
|
|
|
|
|
bvdbg("Loaded sections:\n");
|
2012-10-29 17:43:35 -03:00
|
|
|
dest = (FAR uint8_t*)loadinfo->elfalloc;
|
2012-10-24 22:23:03 -03:00
|
|
|
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
2012-10-25 13:18:20 -03:00
|
|
|
FAR Elf32_Shdr *shdr = &loadinfo->shdr[i];
|
2012-10-24 22:23:03 -03:00
|
|
|
|
|
|
|
/* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution */
|
|
|
|
|
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) == 0)
|
2012-10-24 20:40:31 -03:00
|
|
|
{
|
2012-10-24 22:23:03 -03:00
|
|
|
continue;
|
2012-10-24 20:40:31 -03:00
|
|
|
}
|
2012-10-24 22:23:03 -03:00
|
|
|
|
|
|
|
/* SHT_NOBITS indicates that there is no data in the file for the
|
|
|
|
* section.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (shdr->sh_type != SHT_NOBITS)
|
2012-10-24 20:40:31 -03:00
|
|
|
{
|
2012-12-19 13:54:26 -04:00
|
|
|
/* If CONFIG_ADDRENV=y, then 'dest' lies in a virtual address space
|
|
|
|
* that may not be in place now. elf_addrenv_select() will
|
|
|
|
* temporarily instantiate that address space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ADDRENV
|
|
|
|
ret = elf_addrenv_select(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
bdbg("ERROR: elf_addrenv_select() failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-24 22:23:03 -03:00
|
|
|
/* Read the section data from sh_offset to dest */
|
|
|
|
|
2012-10-25 00:13:11 -03:00
|
|
|
ret = elf_read(loadinfo, dest, shdr->sh_size, shdr->sh_offset);
|
2012-10-24 22:23:03 -03:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
bdbg("Failed to read section %d: %d\n", i, ret);
|
2012-10-28 21:52:23 -03:00
|
|
|
return ret;
|
2012-10-24 22:23:03 -03:00
|
|
|
}
|
2012-12-19 13:54:26 -04:00
|
|
|
|
|
|
|
/* Restore the original address environment */
|
|
|
|
|
|
|
|
#ifdef CONFIG_ADDRENV
|
|
|
|
ret = elf_addrenv_restore(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
bdbg("ERROR: elf_addrenv_restore() failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2012-10-24 20:40:31 -03:00
|
|
|
}
|
2012-10-24 22:23:03 -03:00
|
|
|
|
2012-10-25 13:18:20 -03:00
|
|
|
/* Update sh_addr to point to copy in memory */
|
2012-10-24 22:23:03 -03:00
|
|
|
|
2012-10-25 19:10:56 -03:00
|
|
|
bvdbg("%d. %08x->%08x\n", i, (long)shdr->sh_addr, (long)dest);
|
2012-10-24 22:23:03 -03:00
|
|
|
shdr->sh_addr = (uintptr_t)dest;
|
|
|
|
|
|
|
|
/* Setup the memory pointer for the next time through the loop */
|
|
|
|
|
|
|
|
dest += ELF_ALIGNUP(shdr->sh_size);
|
2012-10-24 20:40:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2012-10-24 13:46:12 -03:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_load
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Loads the binary into memory, allocating memory, performing relocations
|
|
|
|
* and inializing the data and bss segments.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int elf_load(FAR struct elf_loadinfo_s *loadinfo)
|
|
|
|
{
|
2012-10-25 00:13:11 -03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
bvdbg("loadinfo: %p\n", loadinfo);
|
|
|
|
DEBUGASSERT(loadinfo && loadinfo->filfd >= 0);
|
|
|
|
|
|
|
|
/* Load section headers into memory */
|
|
|
|
|
|
|
|
ret = elf_loadshdrs(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-10-25 16:21:47 -03:00
|
|
|
bdbg("elf_loadshdrs failed: %d\n", ret);
|
2012-10-28 21:52:23 -03:00
|
|
|
goto errout_with_buffers;
|
2012-10-25 00:13:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine total size to allocate */
|
|
|
|
|
2012-10-29 17:43:35 -03:00
|
|
|
elf_elfsize(loadinfo);
|
2012-10-25 00:13:11 -03:00
|
|
|
|
|
|
|
/* Allocate memory and load sections into memory */
|
|
|
|
|
|
|
|
ret = elf_loadfile(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-10-25 16:21:47 -03:00
|
|
|
bdbg("elf_loadfile failed: %d\n", ret);
|
2012-10-28 21:52:23 -03:00
|
|
|
goto errout_with_buffers;
|
2012-10-25 00:13:11 -03:00
|
|
|
}
|
|
|
|
|
2012-10-29 17:43:35 -03:00
|
|
|
/* Load static constructors and destructors. */
|
2012-10-25 16:21:47 -03:00
|
|
|
|
2012-10-29 16:32:05 -03:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2012-10-28 21:52:23 -03:00
|
|
|
ret = elf_loadctors(loadinfo);
|
|
|
|
if (ret < 0)
|
2012-10-25 16:21:47 -03:00
|
|
|
{
|
2012-10-28 21:52:23 -03:00
|
|
|
bdbg("elf_loadctors failed: %d\n", ret);
|
|
|
|
goto errout_with_buffers;
|
2012-10-25 16:21:47 -03:00
|
|
|
}
|
2012-10-29 17:43:35 -03:00
|
|
|
|
|
|
|
ret = elf_loaddtors(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
bdbg("elf_loaddtors failed: %d\n", ret);
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
2012-10-25 16:21:47 -03:00
|
|
|
#endif
|
2012-10-25 13:18:20 -03:00
|
|
|
|
2012-10-25 00:13:11 -03:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* Error exits */
|
|
|
|
|
2012-10-28 21:52:23 -03:00
|
|
|
errout_with_buffers:
|
2012-10-29 17:43:35 -03:00
|
|
|
elf_unload(loadinfo);
|
2012-10-25 00:13:11 -03:00
|
|
|
return ret;
|
2012-10-24 13:46:12 -03:00
|
|
|
}
|
|
|
|
|