2009-06-17 00:08:59 +02:00
|
|
|
/****************************************************************************
|
2009-06-17 18:28:50 +02:00
|
|
|
* binfmt/libnxflat/libnxflat_load.c
|
2009-06-17 00:08:59 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* 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>
|
2009-06-17 18:28:50 +02:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2009-06-17 00:08:59 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <nxflat.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
2009-06-17 18:28:50 +02:00
|
|
|
|
2009-06-17 00:08:59 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <nuttx/nxflat.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-Processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(x,y) ((x) > (y) ? (x) : (y))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
#if defined(CONFIG_DEBUG_VERBOSE) && defined(CONFIG_DEBUG_BINFMT)
|
|
|
|
static const char g_textsegment[] = "TEXT";
|
|
|
|
static const char g_datasegment[] = "DATA";
|
|
|
|
static const char g_bsssegment[] = "BSS";
|
|
|
|
static const char g_unksegment[] = "UNKNOWN";
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
static const char *g_segment[] =
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
g_textsegment,
|
|
|
|
g_datasegment,
|
|
|
|
g_bsssegment,
|
|
|
|
g_unksegment
|
2009-06-17 00:08:59 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_reloc
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
static void nxflat_reloc(struct nxflat_loadinfo_s *loadinfo, uint32 rl)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
|
|
|
uint32 *ptr;
|
|
|
|
uint32 datastart;
|
|
|
|
|
2009-06-18 00:13:21 +02:00
|
|
|
/* We only support relocations in the data sections. Verify that the
|
|
|
|
* relocation address lies in the data section of the file image.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-06-18 00:13:21 +02:00
|
|
|
if (NXFLAT_RELOC_OFFSET(rl) > loadinfo->datasize)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
bdbg("ERROR: Relocation at 0x%08x invalid -- "
|
2009-06-17 00:08:59 +02:00
|
|
|
"does not lie in the data segment, size=0x%08x\n",
|
2009-06-18 00:13:21 +02:00
|
|
|
NXFLAT_RELOC_OFFSET(rl), loadinfo->datasize);
|
2009-06-17 18:28:50 +02:00
|
|
|
bdbg(" Relocation not performed!\n");
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
2009-06-18 00:13:21 +02:00
|
|
|
else if ((NXFLAT_RELOC_OFFSET(rl) & 0x00000003) != 0)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
bdbg("ERROR: Relocation at 0x%08x invalid -- "
|
2009-06-17 00:08:59 +02:00
|
|
|
"Improperly aligned\n",
|
2009-06-18 00:13:21 +02:00
|
|
|
NXFLAT_RELOC_OFFSET(rl));
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get a reference to the "real" start of data. It is
|
|
|
|
* offset slightly from the beginning of the allocated
|
|
|
|
* DSpace to hold information needed by ld.so at run time.
|
|
|
|
*/
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
datastart = loadinfo->dspace;
|
2009-06-17 00:08:59 +02:00
|
|
|
|
|
|
|
/* Get a pointer to the value that needs relocation in
|
|
|
|
* DSpace.
|
|
|
|
*/
|
|
|
|
|
2009-06-18 00:13:21 +02:00
|
|
|
ptr = (uint32*)(datastart + NXFLAT_RELOC_OFFSET(rl));
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
bvdbg("Relocation of variable at DATASEG+0x%08x "
|
2009-06-17 00:08:59 +02:00
|
|
|
"(address 0x%p, currently 0x%08x) into segment %s\n",
|
2009-06-18 00:13:21 +02:00
|
|
|
NXFLAT_RELOC_OFFSET(rl), ptr, *ptr, g_segment[NXFLAT_RELOC_TYPE(rl)]);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-18 00:13:21 +02:00
|
|
|
switch (NXFLAT_RELOC_TYPE(rl))
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
/* TEXT is located at an offset of sizeof(struct nxflat_hdr_s) from
|
2009-06-17 00:08:59 +02:00
|
|
|
* the allocated/mapped ISpace region.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case NXFLAT_RELOC_TYPE_TEXT:
|
2009-06-17 18:28:50 +02:00
|
|
|
*ptr += loadinfo->ispace + sizeof(struct nxflat_hdr_s);
|
2009-06-17 00:08:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* DATA and BSS are always contiguous regions. DATA
|
2009-06-17 20:45:48 +02:00
|
|
|
* begins at the beginning of the allocated data segment.
|
2009-06-17 00:08:59 +02:00
|
|
|
* BSS is positioned after DATA, unrelocated references
|
|
|
|
* to BSS include the data offset.
|
|
|
|
*
|
2009-06-17 20:45:48 +02:00
|
|
|
* In other contexts, is it necessary to add the datasize
|
2009-06-17 00:08:59 +02:00
|
|
|
* to get the BSS offset like:
|
|
|
|
*
|
2009-06-17 20:45:48 +02:00
|
|
|
* *ptr += datastart + loadinfo->datasize;
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case NXFLAT_RELOC_TYPE_DATA:
|
|
|
|
case NXFLAT_RELOC_TYPE_BSS:
|
|
|
|
*ptr += datastart;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* This case happens normally if the symbol is a weak
|
|
|
|
* undefined symbol. We permit these.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case NXFLAT_RELOC_TYPE_NONE:
|
2009-06-17 18:28:50 +02:00
|
|
|
bdbg("NULL relocation!\n");
|
2009-06-17 00:08:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-06-18 00:13:21 +02:00
|
|
|
bdbg("ERROR: Unknown relocation type=%d\n", NXFLAT_RELOC_TYPE(rl));
|
2009-06-17 00:08:59 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
bvdbg("Relocation became 0x%08x\n", *ptr);
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_load
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
int nxflat_load(struct nxflat_loadinfo_s *loadinfo)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
off_t doffset; /* Offset to .data in the NXFLAT file */
|
|
|
|
uint32 *reloctab; /* Address of the relocation table */
|
|
|
|
uint32 dreadsize; /* Total number of bytes of .data to be read */
|
|
|
|
uint32 ret;
|
|
|
|
int i;
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
/* Calculate the extra space we need to allocate. This extra space will be
|
|
|
|
* the size of the BSS section. This extra space will also be used
|
|
|
|
* temporarily to hold relocation information. So the allocated size of this
|
|
|
|
* region will either be the size of .data + size of.bss section OR, the
|
|
|
|
* size of .data + the relocation entries, whichever is larger
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
uint32 relocsize;
|
|
|
|
uint32 extrasize;
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* This is the amount of memory that we have to have to hold the
|
|
|
|
* relocations.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
relocsize = loadinfo->reloccount * sizeof(uint32);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* In the file, the relocations should lie at the same offset as BSS.
|
|
|
|
* The additional amount that we allocate have to be either (1) the
|
2009-06-17 20:45:48 +02:00
|
|
|
* BSS size, or (2) the size of the relocation records, whicher is
|
|
|
|
* larger.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
extrasize = MAX(loadinfo->bsssize, relocsize);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* Use this addtional amount to adjust the total size of the dspace
|
|
|
|
* region.
|
|
|
|
*/
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
loadinfo->dsize = loadinfo->datasize + extrasize;
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* The number of bytes of data that we have to read from the file is
|
|
|
|
* the data size plus the size of the relocation table.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
dreadsize = loadinfo->datasize + relocsize;
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We'll need this a few times as well. */
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
doffset = loadinfo->isize;
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* We will make two mmap calls create an address space for the executable.
|
|
|
|
* We will attempt to map the file to get the ISpace address space and
|
|
|
|
* to allocate RAM to get the DSpace address space. If the filesystem does
|
|
|
|
* not support file mapping, the map() implementation should do the
|
2009-06-17 00:08:59 +02:00
|
|
|
* right thing.
|
|
|
|
*/
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* The following call will give as a pointer to the mapped file ISpace.
|
|
|
|
* This may be in ROM, RAM, Flash, ... We don't really care where the memory
|
|
|
|
* resides as long as it is fully initialized and ready to execute.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
loadinfo->ispace = (uint32)mmap(NULL, loadinfo->isize, PROT_READ,
|
2009-06-17 18:28:50 +02:00
|
|
|
MAP_SHARED|MAP_FILE, loadinfo->filfd, 0);
|
|
|
|
if (loadinfo->ispace == (uint32)MAP_FAILED)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
bdbg("Failed to map NXFLAT ISpace: %d\n", errno);
|
|
|
|
return -errno;
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
bvdbg("Mapped ISpace (%d bytes) at 0x%08x\n", loadinfo->isize, loadinfo->ispace);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* The following call will give a pointer to the allocated but
|
|
|
|
* uninitialized ISpace memory.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
loadinfo->dspace = (uint32)malloc(loadinfo->dsize);
|
2009-06-17 18:28:50 +02:00
|
|
|
if (loadinfo->dspace == 0)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
bdbg("Failed to allocate DSpace\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
bvdbg("Allocated DSpace (%d bytes) at %08x\n",
|
2009-06-17 20:45:48 +02:00
|
|
|
loadinfo->dsize, loadinfo->dspace);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* Now, read the data into allocated DSpace at doffset into the
|
|
|
|
* allocated DSpace memory.
|
2009-06-17 00:08:59 +02:00
|
|
|
*/
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
ret = nxflat_read(loadinfo, (char*)loadinfo->dspace, dreadsize, doffset);
|
2009-06-17 18:28:50 +02:00
|
|
|
if (ret < 0)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
bdbg("Failed to read .data section: %d\n", ret);
|
|
|
|
goto errout;
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
2009-06-17 18:28:50 +02:00
|
|
|
|
|
|
|
bvdbg("TEXT=0x%x Entry point offset=0x%08x, datastart is 0x%08x\n",
|
2009-06-17 20:45:48 +02:00
|
|
|
loadinfo->ispace, loadinfo->entryoffs, doffset);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
|
|
|
/* Resolve the address of the relocation table. In the file, the
|
|
|
|
* relocations should lie at the same offset as BSS. The current
|
2009-06-17 20:45:48 +02:00
|
|
|
* value of relocstart is the offset from the beginning of the file.
|
2009-06-17 00:08:59 +02:00
|
|
|
* The following adjustment will convert it to an address in DSpace.
|
|
|
|
*/
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
reloctab = (uint32*)(loadinfo->relocstart + loadinfo->dspace - loadinfo->isize);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
bvdbg("Relocation table at 0x%p, reloccount=%d\n",
|
|
|
|
reloctab, loadinfo->reloccount);
|
2009-06-17 00:08:59 +02:00
|
|
|
|
|
|
|
/* Now run through the relocation entries. */
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
for (i=0; i < loadinfo->reloccount; i++)
|
2009-06-17 00:08:59 +02:00
|
|
|
{
|
2009-06-17 18:28:50 +02:00
|
|
|
nxflat_reloc(loadinfo, htonl(reloctab[i]));
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
/* Zero the BSS area, trashing the relocations that lived in space
|
|
|
|
* in the file.
|
|
|
|
*/
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
memset((void*)(loadinfo->dspace + loadinfo->datasize),
|
|
|
|
0, loadinfo->bsssize);
|
2009-06-17 18:28:50 +02:00
|
|
|
return OK;
|
2009-06-17 00:08:59 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
errout:
|
|
|
|
(void)nxflat_unload(loadinfo);
|
|
|
|
return ret;
|
2009-06-17 00:08:59 +02:00
|
|
|
}
|
|
|
|
|