2009-06-21 15:41:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libnxflat/libnxflat_bind.c
|
|
|
|
*
|
2024-09-09 09:12:59 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-04-11 19:17:55 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2009-06-21 15:41:39 +02:00
|
|
|
*
|
2020-04-11 19:17:55 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-21 15:41:39 +02:00
|
|
|
*
|
2020-04-11 19:17:55 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2009-06-21 15:41:39 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2012-12-19 22:16:03 +01:00
|
|
|
#include <nuttx/compiler.h>
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2020-11-25 12:24:27 +01:00
|
|
|
#include <inttypes.h>
|
2009-12-15 18:06:59 +01:00
|
|
|
#include <stdint.h>
|
2009-06-23 00:12:19 +02:00
|
|
|
#include <string.h>
|
2009-06-21 15:41:39 +02:00
|
|
|
#include <nxflat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2012-12-19 18:54:26 +01:00
|
|
|
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/nxflat.h>
|
2023-07-02 18:43:49 +02:00
|
|
|
#include <nuttx/symtab.h>
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
#include "libnxflat.h"
|
|
|
|
|
2009-06-21 15:41:39 +02:00
|
|
|
/****************************************************************************
|
2009-06-25 19:44:35 +02:00
|
|
|
* Pre-processor Definitions
|
2009-06-21 15:41:39 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-11 19:43:37 +02:00
|
|
|
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT have to
|
|
|
|
* be defined or CONFIG_NXFLAT_DUMPBUFFER does nothing.
|
2009-06-25 19:44:35 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
|
2009-06-25 23:59:30 +02:00
|
|
|
# undef CONFIG_NXFLAT_DUMPBUFFER
|
2009-06-25 19:44:35 +02:00
|
|
|
#endif
|
|
|
|
|
2009-06-25 23:59:30 +02:00
|
|
|
#ifdef CONFIG_NXFLAT_DUMPBUFFER
|
2023-05-20 00:32:34 +02:00
|
|
|
# define nxflat_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
|
2009-06-25 19:44:35 +02:00
|
|
|
#else
|
2023-05-20 00:32:34 +02:00
|
|
|
# define nxflat_dumpbuffer(m,b,n)
|
2009-06-25 19:44:35 +02:00
|
|
|
#endif
|
|
|
|
|
2009-06-21 15:41:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2009-06-23 00:12:19 +02:00
|
|
|
* Name: nxflat_bindrel32i
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform the NXFLAT_RELOC_TYPE_REL32I binding:
|
|
|
|
*
|
2020-04-11 19:43:37 +02:00
|
|
|
* Meaning: Object file contains a 32-bit offset into I-Space at the
|
|
|
|
* offset.
|
2009-06-23 00:12:19 +02:00
|
|
|
* Fixup: Add mapped I-Space address to the offset.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
2009-06-21 15:41:39 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2009-06-23 00:12:19 +02:00
|
|
|
static inline int nxflat_bindrel32i(FAR struct nxflat_loadinfo_s *loadinfo,
|
2009-12-15 18:06:59 +01:00
|
|
|
uint32_t offset)
|
2009-06-23 00:12:19 +02:00
|
|
|
{
|
2015-10-09 03:20:17 +02:00
|
|
|
FAR uint32_t *addr;
|
2009-06-23 00:12:19 +02:00
|
|
|
|
2020-11-25 12:24:27 +01:00
|
|
|
binfo("NXFLAT_RELOC_TYPE_REL32I Offset: %08" PRIx32
|
|
|
|
" I-Space: %" PRIxPTR "\n",
|
2009-06-23 01:09:55 +02:00
|
|
|
offset, loadinfo->ispace + sizeof(struct nxflat_hdr_s));
|
2009-06-23 00:12:19 +02:00
|
|
|
|
|
|
|
if (offset < loadinfo->dsize)
|
|
|
|
{
|
2015-10-09 03:20:17 +02:00
|
|
|
addr = (FAR uint32_t *)(offset + loadinfo->dspace->region);
|
2020-11-25 12:24:27 +01:00
|
|
|
binfo(" Before: %08" PRIx32 "\n", *addr);
|
2009-12-15 18:06:59 +01:00
|
|
|
*addr += (uint32_t)(loadinfo->ispace + sizeof(struct nxflat_hdr_s));
|
2020-11-25 12:24:27 +01:00
|
|
|
binfo(" After: %08" PRIx32 "\n", *addr);
|
2009-06-23 00:12:19 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-25 12:24:27 +01:00
|
|
|
berr("Offset: %08" PRIx32 " does not lie in "
|
|
|
|
"D-Space size: %08" PRIx32 "\n",
|
2009-06-23 00:12:19 +02:00
|
|
|
offset, loadinfo->dsize);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_bindrel32d
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform the NXFLAT_RELOC_TYPE_REL32D binding:
|
|
|
|
*
|
2020-04-11 19:43:37 +02:00
|
|
|
* Meaning: Object file contains a 32-bit offset into D-Space at the
|
|
|
|
* offset.
|
2009-06-23 00:12:19 +02:00
|
|
|
* Fixup: Add allocated D-Space address to the offset.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxflat_bindrel32d(FAR struct nxflat_loadinfo_s *loadinfo,
|
2009-12-15 18:06:59 +01:00
|
|
|
uint32_t offset)
|
2009-06-23 00:12:19 +02:00
|
|
|
{
|
2015-10-09 03:20:17 +02:00
|
|
|
FAR uint32_t *addr;
|
2009-06-23 00:12:19 +02:00
|
|
|
|
2020-11-25 12:24:27 +01:00
|
|
|
binfo("NXFLAT_RELOC_TYPE_REL32D Offset: %08" PRIx32 " D-Space: %p\n",
|
2009-06-23 00:12:19 +02:00
|
|
|
offset, loadinfo->dspace->region);
|
|
|
|
|
|
|
|
if (offset < loadinfo->dsize)
|
|
|
|
{
|
2015-10-09 03:20:17 +02:00
|
|
|
addr = (FAR uint32_t *)(offset + loadinfo->dspace->region);
|
2020-11-25 12:24:27 +01:00
|
|
|
binfo(" Before: %08" PRIx32 "\n", *addr);
|
2009-12-15 18:06:59 +01:00
|
|
|
*addr += (uint32_t)(loadinfo->dspace->region);
|
2020-11-25 12:24:27 +01:00
|
|
|
binfo(" After: %08" PRIx32 "\n", *addr);
|
2009-06-23 00:12:19 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-25 12:24:27 +01:00
|
|
|
berr("Offset: %08" PRIx32 " does not lie in "
|
|
|
|
"D-Space size: %08" PRIx32 "\n",
|
2009-06-23 00:12:19 +02:00
|
|
|
offset, loadinfo->dsize);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-23 22:22:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_bindrel32id
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform the NXFLAT_RELOC_TYPE_REL32ID binding:
|
|
|
|
*
|
|
|
|
* Meaning: Object file contains a 32-bit offset into I-Space at the offset
|
|
|
|
* that will unfortunately be references relative to the GOT
|
|
|
|
* Fixup: Add allocated the mapped I-Space address MINUS the allocated
|
|
|
|
* D-Space address to the offset.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NXFLAT_RELOC_TYPE_REL32ID
|
|
|
|
static inline int nxflat_bindrel32id(FAR struct nxflat_loadinfo_s *loadinfo,
|
2009-12-15 18:06:59 +01:00
|
|
|
uint32_t offset)
|
2009-06-23 22:22:50 +02:00
|
|
|
{
|
2015-10-09 03:20:17 +02:00
|
|
|
FAR uint32_t *addr;
|
2009-06-23 22:22:50 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("NXFLAT_RELOC_TYPE_REL32D Offset: %08x D-Space: %p\n",
|
2009-06-23 22:22:50 +02:00
|
|
|
offset, loadinfo->dspace->region);
|
|
|
|
|
|
|
|
if (offset < loadinfo->dsize)
|
|
|
|
{
|
2020-04-11 19:43:37 +02:00
|
|
|
addr = (FAR uint32_t *)(offset + loadinfo->dspace->region);
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo(" Before: %08x\n", *addr);
|
2020-04-11 19:43:37 +02:00
|
|
|
|
|
|
|
*addr += ((uint32_t)loadinfo->ispace -
|
|
|
|
(uint32_t)(loadinfo->dspace->region));
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo(" After: %08x\n", *addr);
|
2020-04-11 19:43:37 +02:00
|
|
|
|
2009-06-23 22:22:50 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Offset: %08 does not lie in D-Space size: %08x\n",
|
2009-06-23 22:22:50 +02:00
|
|
|
offset, loadinfo->dsize);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-23 00:12:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_gotrelocs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Bind all of the GOT relocations in the loaded module described by
|
|
|
|
* 'loadinfo'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxflat_gotrelocs(FAR struct nxflat_loadinfo_s *loadinfo)
|
|
|
|
{
|
|
|
|
FAR struct nxflat_reloc_s *relocs;
|
|
|
|
FAR struct nxflat_reloc_s reloc;
|
|
|
|
FAR struct nxflat_hdr_s *hdr;
|
2009-12-15 18:06:59 +01:00
|
|
|
uint32_t offset;
|
|
|
|
uint16_t nrelocs;
|
|
|
|
int ret;
|
|
|
|
int result;
|
|
|
|
int i;
|
2009-06-23 00:12:19 +02:00
|
|
|
|
|
|
|
/* The NXFLAT header is the first thing at the beginning of the ISpace. */
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
hdr = (FAR struct nxflat_hdr_s *)loadinfo->ispace;
|
2009-06-23 00:12:19 +02:00
|
|
|
|
|
|
|
/* From this, we can get the offset to the list of relocation entries */
|
|
|
|
|
2022-01-18 08:38:00 +01:00
|
|
|
offset = NTOHL(hdr->h_relocstart);
|
|
|
|
nrelocs = NTOHS(hdr->h_reloccount);
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("offset: %08lx nrelocs: %d\n", (long)offset, nrelocs);
|
2009-06-23 00:12:19 +02:00
|
|
|
|
|
|
|
/* The value of the relocation list that we get from the header is a
|
|
|
|
* file offset. We will have to convert this to an offset into the
|
|
|
|
* DSpace segment to get the pointer to the beginning of the relocation
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
DEBUGASSERT(offset >= loadinfo->isize);
|
2009-06-26 15:43:32 +02:00
|
|
|
DEBUGASSERT(offset + nrelocs * sizeof(struct nxflat_reloc_s)
|
|
|
|
<= (loadinfo->isize + loadinfo->dsize));
|
2009-06-25 02:05:11 +02:00
|
|
|
|
2012-12-19 22:16:03 +01:00
|
|
|
relocs = (FAR struct nxflat_reloc_s *)
|
2009-06-26 15:43:32 +02:00
|
|
|
(offset - loadinfo->isize + loadinfo->dspace->region);
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("isize: %08lx dpsace: %p relocs: %p\n",
|
2009-06-26 15:43:32 +02:00
|
|
|
(long)loadinfo->isize, loadinfo->dspace->region, relocs);
|
2009-06-23 00:12:19 +02:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* All relocations are performed within the D-Space allocation. If
|
2014-08-24 14:42:11 +02:00
|
|
|
* CONFIG_ARCH_ADDRENV=y, then that D-Space allocation lies in an address
|
2012-12-19 18:54:26 +01:00
|
|
|
* environment that may not be in place. So, in that case, we must call
|
|
|
|
* nxflat_addrenv_select to temporarily instantiate that address space
|
|
|
|
* before the relocations can be performed.
|
|
|
|
*/
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_addrenv_select(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: nxflat_addrenv_select() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-23 20:55:57 +02:00
|
|
|
/* Now, traverse the relocation list of and bind each GOT relocation. */
|
2009-06-23 00:12:19 +02:00
|
|
|
|
|
|
|
ret = OK; /* Assume success */
|
|
|
|
for (i = 0; i < nrelocs; i++)
|
|
|
|
{
|
|
|
|
/* Handle the relocation by the relocation type */
|
|
|
|
|
2020-04-11 19:17:55 +02:00
|
|
|
reloc = *relocs++;
|
2009-06-23 00:12:19 +02:00
|
|
|
result = OK;
|
2020-04-11 19:17:55 +02:00
|
|
|
|
2009-06-23 00:12:19 +02:00
|
|
|
switch (NXFLAT_RELOC_TYPE(reloc.r_info))
|
|
|
|
{
|
2020-01-11 17:10:49 +01:00
|
|
|
/* NXFLAT_RELOC_TYPE_REL32I Meaning: Object file contains a 32-bit
|
2020-04-11 19:43:37 +02:00
|
|
|
* offset into I-Space at the
|
2020-01-11 17:10:49 +01:00
|
|
|
* offset.
|
2020-04-11 19:43:37 +02:00
|
|
|
* Fixup: Add mapped I-Space address
|
|
|
|
* to the offset.
|
2009-06-23 00:12:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case NXFLAT_RELOC_TYPE_REL32I:
|
|
|
|
{
|
2020-04-11 19:43:37 +02:00
|
|
|
result = nxflat_bindrel32i(loadinfo,
|
|
|
|
NXFLAT_RELOC_OFFSET(reloc.r_info));
|
2009-06-23 00:12:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-04-11 19:43:37 +02:00
|
|
|
/* NXFLAT_RELOC_TYPE_REL32D Meaning: Object file contains a 32-bit
|
|
|
|
* offset into D-Space at the
|
2009-06-23 00:12:19 +02:00
|
|
|
* offset.
|
2020-04-11 19:43:37 +02:00
|
|
|
* Fixup: Add allocated D-Space address
|
|
|
|
* to the offset.
|
2009-06-23 00:12:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case NXFLAT_RELOC_TYPE_REL32D:
|
|
|
|
{
|
2020-04-11 19:43:37 +02:00
|
|
|
result = nxflat_bindrel32d(loadinfo,
|
|
|
|
NXFLAT_RELOC_OFFSET(reloc.r_info));
|
2009-06-23 00:12:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-04-11 19:43:37 +02:00
|
|
|
/* NXFLAT_RELOC_TYPE_REL32ID Meaning: Object file contains a 32-bit
|
|
|
|
* offset into I-Space at the
|
|
|
|
* offset that will unfortunately
|
|
|
|
* be references relative to the
|
|
|
|
* GOT
|
|
|
|
* Fixup: Add allocated the mapped
|
|
|
|
* I-Space address MINUS the
|
|
|
|
* allocated D-Space address to
|
|
|
|
* the offset.
|
2009-06-23 22:22:50 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef NXFLAT_RELOC_TYPE_REL32ID
|
|
|
|
case NXFLAT_RELOC_TYPE_REL32ID:
|
|
|
|
{
|
2020-01-11 17:10:49 +01:00
|
|
|
result = nxflat_bindrel32id(loadinfo,
|
|
|
|
NXFLAT_RELOC_OFFSET(reloc.r_info));
|
2009-06-23 22:22:50 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2009-06-23 20:55:57 +02:00
|
|
|
default:
|
2009-06-23 00:12:19 +02:00
|
|
|
{
|
2020-11-25 12:24:27 +01:00
|
|
|
berr("ERROR: Unrecognized relocation type: %" PRId32 "\n",
|
|
|
|
(uint32_t)NXFLAT_RELOC_TYPE(reloc.r_info));
|
2009-06-23 20:55:57 +02:00
|
|
|
result = -EINVAL;
|
2009-06-23 00:12:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for failures */
|
|
|
|
|
|
|
|
if (result < 0 && ret == OK)
|
|
|
|
{
|
|
|
|
ret = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-25 19:44:35 +02:00
|
|
|
/* Dump the relocation got */
|
|
|
|
|
2009-06-25 23:59:30 +02:00
|
|
|
#ifdef CONFIG_NXFLAT_DUMPBUFFER
|
2009-06-25 19:44:35 +02:00
|
|
|
if (ret == OK && nrelocs > 0)
|
|
|
|
{
|
2020-01-11 17:10:49 +01:00
|
|
|
relocs = (FAR struct nxflat_reloc_s *)
|
|
|
|
(offset - loadinfo->isize + loadinfo->dspace->region);
|
|
|
|
nxflat_dumpbuffer("GOT", (FAR const uint8_t *)relocs,
|
|
|
|
nrelocs * sizeof(struct nxflat_reloc_s));
|
2009-06-25 19:44:35 +02:00
|
|
|
}
|
|
|
|
#endif
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* Restore the original address environment */
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_addrenv_restore(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: nxflat_addrenv_restore() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-23 00:12:19 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_bindimports
|
2009-06-21 15:41:39 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Bind the imported symbol names in the loaded module described by
|
|
|
|
* 'loadinfo' using the exported symbol values provided by 'symtab'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
2009-06-23 00:12:19 +02:00
|
|
|
****************************************************************************/
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2009-06-23 00:12:19 +02:00
|
|
|
static inline int nxflat_bindimports(FAR struct nxflat_loadinfo_s *loadinfo,
|
2012-12-19 18:54:26 +01:00
|
|
|
FAR const struct symtab_s *exports,
|
|
|
|
int nexports)
|
2009-06-21 15:41:39 +02:00
|
|
|
{
|
|
|
|
FAR struct nxflat_import_s *imports;
|
|
|
|
FAR struct nxflat_hdr_s *hdr;
|
2012-12-19 18:54:26 +01:00
|
|
|
FAR const struct symtab_s *symbol;
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR char *symname;
|
2009-12-15 18:06:59 +01:00
|
|
|
uint32_t offset;
|
|
|
|
uint16_t nimports;
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
int ret;
|
|
|
|
#endif
|
2009-12-15 18:06:59 +01:00
|
|
|
int i;
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2009-06-23 00:12:19 +02:00
|
|
|
/* The NXFLAT header is the first thing at the beginning of the ISpace. */
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
hdr = (FAR struct nxflat_hdr_s *)loadinfo->ispace;
|
2009-06-21 15:41:39 +02:00
|
|
|
|
|
|
|
/* From this, we can get the offset to the list of symbols imported by
|
|
|
|
* this module and the number of symbols imported by this module.
|
|
|
|
*/
|
|
|
|
|
2022-01-18 08:38:00 +01:00
|
|
|
offset = NTOHL(hdr->h_importsymbols);
|
|
|
|
nimports = NTOHS(hdr->h_importcount);
|
2020-11-25 12:24:27 +01:00
|
|
|
binfo("Imports offset: %08" PRIx32 " nimports: %d\n", offset, nimports);
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* The import[] table resides within the D-Space allocation. If
|
2014-08-24 14:42:11 +02:00
|
|
|
* CONFIG_ARCH_ADDRENV=y, then that D-Space allocation lies in an address
|
2012-12-19 18:54:26 +01:00
|
|
|
* environment that may not be in place. So, in that case, we must call
|
|
|
|
* nxflat_addrenv_select to temporarily instantiate that address space
|
|
|
|
* before the import[] table can be modified.
|
|
|
|
*/
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_addrenv_select(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: nxflat_addrenv_select() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-21 15:41:39 +02:00
|
|
|
/* Verify that this module requires imported symbols */
|
|
|
|
|
|
|
|
if (offset != 0 && nimports > 0)
|
|
|
|
{
|
|
|
|
/* It does.. make sure that exported symbols are provided */
|
|
|
|
|
2009-06-24 22:49:43 +02:00
|
|
|
DEBUGASSERT(exports && nexports > 0);
|
2009-06-21 15:41:39 +02:00
|
|
|
|
|
|
|
/* If non-zero, the value of the imported symbol list that we get
|
|
|
|
* from the header is a file offset. We will have to convert this
|
|
|
|
* to an offset into the DSpace segment to get the pointer to the
|
|
|
|
* beginning of the imported symbol list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(offset >= loadinfo->isize &&
|
|
|
|
offset < loadinfo->isize + loadinfo->dsize);
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
imports = (FAR struct nxflat_import_s *)
|
2012-10-24 18:46:12 +02:00
|
|
|
(offset - loadinfo->isize + loadinfo->dspace->region);
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2009-06-23 00:12:19 +02:00
|
|
|
/* Now, traverse the list of imported symbols and attempt to bind
|
2009-06-21 15:41:39 +02:00
|
|
|
* each symbol to the value exported by from the exported symbol
|
|
|
|
* table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < nimports; i++)
|
2012-10-24 18:46:12 +02:00
|
|
|
{
|
2020-11-25 12:24:27 +01:00
|
|
|
binfo("Import[%d] (%p) "
|
|
|
|
"offset: %08" PRIx32 " func: %08" PRIx32 "\n",
|
2020-04-11 19:43:37 +02:00
|
|
|
i, &imports[i], imports[i].i_funcname,
|
|
|
|
imports[i].i_funcaddress);
|
2009-06-26 20:58:37 +02:00
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/* Get a pointer to the imported symbol name. The name itself
|
|
|
|
* lies in the TEXT segment. But the reference to the name
|
|
|
|
* lies in DATA segment. Therefore, the name reference should
|
|
|
|
* have been relocated when the module was loaded.
|
|
|
|
*/
|
2009-06-21 15:41:39 +02:00
|
|
|
|
|
|
|
offset = imports[i].i_funcname;
|
|
|
|
DEBUGASSERT(offset < loadinfo->isize);
|
|
|
|
|
2020-01-11 17:10:49 +01:00
|
|
|
symname = (FAR char *)
|
|
|
|
(offset + loadinfo->ispace + sizeof(struct nxflat_hdr_s));
|
2009-06-21 15:41:39 +02:00
|
|
|
|
Sources and Docs: Fix typos and nxstyle issues
Documentation/contributing/coding_style.rst:
* Fix repeated words: ("this this").
* Remove trailing spaces.
boards/z80/z80/z80sim/README.txt:
* Fix repeated words: ("this this") and rewrap lines.
graphics/Kconfig,
libs/libc/math/Kconfig:
* Fix repeated words: ("this this").
arch/arm/src/armv7-a/arm_assert.c,
arch/arm/src/armv7-r/arm_assert.c,
arch/arm/src/imxrt/imxrt_enet.c,
arch/arm/src/kinetis/kinetis_enet.c,
arch/arm/src/kinetis/kinetis_flexcan.c,
arch/arm/src/s32k1xx/s32k1xx_enet.c,
arch/arm/src/s32k1xx/s32k1xx_flexcan.c,
arch/arm/src/stm32/stm32_pwm.c,
arch/arm/src/stm32h7/stm32_pwm.c,
arch/arm/src/stm32l4/stm32l4_pwm.c,
arch/renesas/src/rx65n/rx65n_usbdev.c,
binfmt/libnxflat/libnxflat_bind.c,
drivers/pipes/pipe_common.c,
net/igmp/igmp_input.c,
net/tcp/tcp_conn.c,
sched/sched/sched_roundrobin.c:
* Fix typo in comment ("this this").
arch/arm/src/cxd56xx/cxd56_usbdev.c,
arch/arm/src/lc823450/lc823450_usbdev.c:
* Fix typo in comment and rewrap lines.
arch/arm/src/imxrt/imxrt_usbdev.c,
arch/arm/src/stm32/stm32_dac.c,
arch/arm/src/stm32f0l0g0/stm32_pwm.c,
arch/arm/src/stm32f7/stm32_pwm.c,
arch/arm/src/tiva/lm/lm4f_gpio.h,
fs/nxffs/nxffs_write.c,
include/nuttx/analog/pga11x.h,
include/nuttx/usb/usbdev.h,
net/mld/mld_join.c:
* Fix typo in comment ("this this").
* Fix nxstyle issues.
2020-10-02 01:29:35 +02:00
|
|
|
/* Find the exported symbol value for this symbol name. */
|
2009-06-21 15:41:39 +02:00
|
|
|
|
|
|
|
symbol = symtab_findbyname(exports, symname, nexports);
|
2012-10-24 18:46:12 +02:00
|
|
|
if (!symbol)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Exported symbol \"%s\" not found\n", symname);
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2020-01-02 17:49:34 +01:00
|
|
|
nxflat_addrenv_restore(loadinfo);
|
2012-12-19 18:54:26 +01:00
|
|
|
#endif
|
2009-06-21 15:41:39 +02:00
|
|
|
return -ENOENT;
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/* And put this into the module's import structure. */
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
imports[i].i_funcaddress = (uint32_t)symbol->sym_value;
|
2009-06-21 15:41:39 +02:00
|
|
|
|
2020-11-25 12:24:27 +01:00
|
|
|
binfo("Bound import[%d] (%p) to export '%s' (%08" PRIx32 ")\n",
|
2012-10-24 18:46:12 +02:00
|
|
|
i, &imports[i], symname, imports[i].i_funcaddress);
|
|
|
|
}
|
2009-06-21 15:41:39 +02:00
|
|
|
}
|
|
|
|
|
2009-06-25 19:44:35 +02:00
|
|
|
/* Dump the relocation import table */
|
|
|
|
|
2009-06-25 23:59:30 +02:00
|
|
|
#ifdef CONFIG_NXFLAT_DUMPBUFFER
|
2009-06-25 19:44:35 +02:00
|
|
|
if (nimports > 0)
|
|
|
|
{
|
2020-01-11 17:10:49 +01:00
|
|
|
nxflat_dumpbuffer("Imports", (FAR const uint8_t *)imports,
|
|
|
|
nimports * sizeof(struct nxflat_import_s));
|
2009-06-25 19:44:35 +02:00
|
|
|
}
|
|
|
|
#endif
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* Restore the original address environment */
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_addrenv_restore(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: nxflat_addrenv_restore() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#else
|
2009-06-21 15:41:39 +02:00
|
|
|
return OK;
|
2012-12-19 18:54:26 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_clearbss
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clear uninitialized .bss memory
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxflat_clearbss(FAR struct nxflat_loadinfo_s *loadinfo)
|
|
|
|
{
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
int ret;
|
|
|
|
#endif
|
|
|
|
|
2020-04-11 19:43:37 +02:00
|
|
|
/* .bss resides within the D-Space allocation. If CONFIG_ARCH_ADDRENV=y,
|
|
|
|
* then that D-Space allocation lies in an address environment that may
|
|
|
|
* not be in place. So, in that case, we must call nxflat_addrenv_select
|
|
|
|
* to temporarily instantiate that address space before the .bss can be
|
2012-12-19 18:54:26 +01:00
|
|
|
* accessed.
|
|
|
|
*/
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_addrenv_select(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: nxflat_addrenv_select() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Zero the BSS area */
|
|
|
|
|
2020-01-11 17:10:49 +01:00
|
|
|
memset((FAR void *)(loadinfo->dspace->region + loadinfo->datasize), 0,
|
|
|
|
loadinfo->bsssize);
|
2012-12-19 18:54:26 +01:00
|
|
|
|
|
|
|
/* Restore the original address environment */
|
|
|
|
|
2014-08-24 14:42:11 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_addrenv_restore(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: nxflat_addrenv_restore() failed: %d\n", ret);
|
2012-12-19 18:54:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return OK;
|
|
|
|
#endif
|
2009-06-21 15:41:39 +02:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:12:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Bind the imported symbol names in the loaded module described by
|
|
|
|
* 'loadinfo' using the exported symbol values provided by 'symtab'.
|
2020-04-11 19:43:37 +02:00
|
|
|
* After binding the module, clear the BSS region (which held the
|
|
|
|
* relocation data) in preparation for execution.
|
2009-06-23 00:12:19 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxflat_bind(FAR struct nxflat_loadinfo_s *loadinfo,
|
|
|
|
FAR const struct symtab_s *exports, int nexports)
|
|
|
|
{
|
2009-06-26 20:58:37 +02:00
|
|
|
/* Bind the imported symbol, absolute relocations separately. This is done
|
|
|
|
* before the standard relocations because that logic may modify the
|
2020-04-11 19:43:37 +02:00
|
|
|
* import list (for the better hopefully, but we don't want to depend on
|
|
|
|
* it).
|
2009-06-26 20:58:37 +02:00
|
|
|
*/
|
2009-06-23 00:12:19 +02:00
|
|
|
|
2009-06-26 20:58:37 +02:00
|
|
|
int ret = nxflat_bindimports(loadinfo, exports, nexports);
|
2009-06-23 00:12:19 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2009-06-26 20:58:37 +02:00
|
|
|
/* Then bind all GOT relocations */
|
2009-06-23 00:12:19 +02:00
|
|
|
|
2009-06-26 20:58:37 +02:00
|
|
|
ret = nxflat_gotrelocs(loadinfo);
|
2009-06-23 00:12:19 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2009-06-23 20:55:57 +02:00
|
|
|
/* Zero the BSS area, trashing the relocations that lived in that
|
|
|
|
* space in the loaded file.
|
2009-06-23 00:12:19 +02:00
|
|
|
*/
|
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
ret = nxflat_clearbss(loadinfo);
|
2009-06-23 00:12:19 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2009-06-23 00:12:19 +02:00
|
|
|
return ret;
|
|
|
|
}
|