mirror of
https://github.com/apache/nuttx.git
synced 2026-08-20 13:08:26 +00:00
ARM PIC has used r10 as the base register, but the tree has never been consistent about it. Toolchain.defs gives CONFIG_BUILD_PIC -mpic-register=r9 and CONFIG_PIC -mpic-register=r10, twenty-five lines apart, and arm_initialstate.c sets REG_R9 from inline assembly under one and REG_PIC under the other, with a comment reading "Set the PIC base register (probably R10)". This settles it on r9 for all of PIC: NXFLAT, ELF PIC and CONFIG_BUILD_PIC alike. r9 is the right choice rather than an arbitrary one. It is the AAPCS platform register, the "static base", and it is what GCC itself picks for -msingle-pic-base on an EABI target; r10 is the non-EABI default. It also removes a combination that cannot build today. Stack checking adds -ffixed-r10 in armv7-m/Toolchain.defs and armv8-m/Toolchain.defs, while CONFIG_PIC adds -mpic-register=r10, and GCC rejects the pair with "unable to use 'r10' for PIC register". The comment above REG_PIC has always said the register "can be R9 if stack checking is enabled", but the definition was unconditionally REG_R10, so it would have named the wrong register even had the build succeeded. The thunk generator moves with the firmware. NXFLAT import stubs had the register baked in as "add ip,ip,sl", so a module built for r9 would load and then branch to a wild address on its first call out. The stubs now come from NXFLAT_PIC_REG in the in-tree tool, which is built only when CONFIG_NXFLAT is set, following the CONFIG_BOARD_ETC_ROMFS_PASSWD_ENABLE precedent in tools/Unix.mk. That leaves modules built before this change, and they are the reason for the ABI marker. The NXFLAT header cannot carry a version: h_magic is written by ldnxflat, which is GPL, derived from elf2flt, and stays out of this repository, so it can never be changed in step with the loader. The import table can, because both of its ends are in-tree -- mknxflat emits it and nxflat_bindimports() reads it -- and ldnxflat passes it through untouched. So every module now imports __nxflat_abi_v2, the base firmware defines it, and a module that does not import it is refused. Making the marker a real exported symbol rather than a name the loader special-cases is what keeps it out of the build system's way: a board's symbol table picks it up exactly as it picks up printf, so mksymtab.sh and its equivalents need no change. It also gives the reverse direction a diagnosis for free -- a module built against a newer ABI than its firmware fails with "Exported symbol __nxflat_abi_v2 not found". Most of the remaining churn is boards restating a default. ARCHPICFLAGS is a "?=" default so that a board only speaks up when it differs, and twenty-six were assigning the value the default already had. MKNXFLAT gets the same treatment: thirteen boards named the same tool, and the only thing that varies is ARM versus Thumb-2, which falls out of CONFIG_ARM_THUMB. LDNXFLAT gains a default too -- it stays an out-of-tree PATH lookup, but naming it centrally fixes boards that never assigned it, where it expanded to nothing and handed make a recipe beginning "-e", whose leading dash make ate as "ignore errors". The non-ARM boards carrying -mpic-register=r10 lose it: it is an ARM-only option, reachable only through CPICFLAGS, which is only used to build NXFLAT modules, and no non-ARM board enables NXFLAT. Boards keep nothing about PIC flags any more. ARCHPICFLAGS was set by sixty-three of them and only ever fed CPICFLAGS, which is only used to build NXFLAT modules; no board outside arch/arm enables NXFLAT, so every non-ARM copy was setting a variable nothing read. Those are removed rather than moved somewhere more central, which would only make dead text look load-bearing. LDNXFLAT goes the same way as MKNXFLAT, for the same reason: thirteen boards named the same tool that Toolchain.defs now names once. One of them was not merely redundant. am67/t3-gem-o1 asked for "-mpic-register=r10 -ffixed-r10", which GCC refuses outright with "unable to use 'r10' for PIC register" -- the very combination the filter-out machinery in Toolchain.defs exists to prevent. It has survived because that board does not build NXFLAT modules, so the flags are never handed to a compiler. Renaming the register would have carried the fault forward unchanged, so the line goes. Tested on lm3s6965-ek:qemu-nxflat under QEMU, configured and built with no overrides. The nxflat example runs the errno, hello and struct modules with output identical to the same config built from master. Built with the old out-of-tree thunk generator instead, the same firmware refuses all three with ENOEXEC rather than locking up in a HardFault, which is what this change is for. mps3-an547:picostest, which is CONFIG_PIC without CONFIG_NXFLAT, builds clean and does not build the thunk generator. The .def files pick up two cosmetic changes here alongside the register: a "Dyanamic" typo that codespell rejects, and a reworded comment in each thunk_*.c. Neither appears in the emitted thunk -- both are in C comments -- so the generated text is still what the upstream tool produces, modulo the register itself. BREAKING CHANGE: ARM PIC moves from r10 to r9. An NXFLAT module built before this change has r10 baked into its import stubs and will not run against a firmware carrying it; the two cannot be mixed. The module is refused with ENOEXEC rather than branching to a wild address, by way of the __nxflat_abi_v2 marker described below. Quick fix: rebuild the module against this tree. Its source needs no change. A board that reserved r10 by hand, or that assigned ARCHPICFLAGS or MKNXFLAT to restate a default, should drop those assignments; nothing else is affected, and CONFIG_PIC without CONFIG_NXFLAT needs no action. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
656 lines
20 KiB
C
656 lines
20 KiB
C
/****************************************************************************
|
|
* binfmt/libnxflat/libnxflat_bind.c
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* 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
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* 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.
|
|
*
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <nuttx/config.h>
|
|
#include <nuttx/compiler.h>
|
|
|
|
#include <inttypes.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <nxflat.h>
|
|
#include <errno.h>
|
|
#include <assert.h>
|
|
#include <nuttx/debug.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <nuttx/binfmt/nxflat.h>
|
|
#include <nuttx/symtab.h>
|
|
|
|
#include "libnxflat.h"
|
|
|
|
/****************************************************************************
|
|
* Pre-processor Definitions
|
|
****************************************************************************/
|
|
|
|
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT have to
|
|
* be defined or CONFIG_NXFLAT_DUMPBUFFER does nothing.
|
|
*/
|
|
|
|
#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
|
|
# undef CONFIG_NXFLAT_DUMPBUFFER
|
|
#endif
|
|
|
|
#ifdef CONFIG_NXFLAT_DUMPBUFFER
|
|
# define nxflat_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
|
|
#else
|
|
# define nxflat_dumpbuffer(m,b,n)
|
|
#endif
|
|
|
|
/****************************************************************************
|
|
* Public Data
|
|
****************************************************************************/
|
|
|
|
/* The module ABI marker. Every module built by tools/nxflat/mknxflat
|
|
* imports this, and a board's exported symbol table picks it up the same
|
|
* way it picks up any other imported name, so nothing has to special-case
|
|
* it in the build. Its value is never used; only its presence matters.
|
|
*
|
|
* A module built against a newer ABI than the firmware therefore fails
|
|
* with "Exported symbol __nxflat_abi_vN not found", which names the
|
|
* problem, and a module built against an older one is caught below.
|
|
*/
|
|
|
|
void *NXFLAT_ABI_MARKER;
|
|
|
|
/****************************************************************************
|
|
* Private Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: nxflat_bindrel32i
|
|
*
|
|
* Description:
|
|
* Perform the NXFLAT_RELOC_TYPE_REL32I binding:
|
|
*
|
|
* Meaning: Object file contains a 32-bit offset into I-Space at the
|
|
* offset.
|
|
* 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.
|
|
*
|
|
****************************************************************************/
|
|
|
|
static inline int nxflat_bindrel32i(FAR struct nxflat_loadinfo_s *loadinfo,
|
|
uint32_t offset)
|
|
{
|
|
FAR uint32_t *addr;
|
|
|
|
binfo("NXFLAT_RELOC_TYPE_REL32I Offset: %08" PRIx32
|
|
" I-Space: %" PRIxPTR "\n",
|
|
offset, loadinfo->ispace + sizeof(struct nxflat_hdr_s));
|
|
|
|
if (offset < loadinfo->dsize)
|
|
{
|
|
addr = (FAR uint32_t *)(offset + loadinfo->dspace->region);
|
|
binfo(" Before: %08" PRIx32 "\n", *addr);
|
|
*addr += (uint32_t)(loadinfo->ispace + sizeof(struct nxflat_hdr_s));
|
|
binfo(" After: %08" PRIx32 "\n", *addr);
|
|
return OK;
|
|
}
|
|
else
|
|
{
|
|
berr("Offset: %08" PRIx32 " does not lie in "
|
|
"D-Space size: %08" PRIx32 "\n",
|
|
offset, loadinfo->dsize);
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: nxflat_bindrel32d
|
|
*
|
|
* Description:
|
|
* Perform the NXFLAT_RELOC_TYPE_REL32D binding:
|
|
*
|
|
* Meaning: Object file contains a 32-bit offset into D-Space at the
|
|
* offset.
|
|
* 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,
|
|
uint32_t offset)
|
|
{
|
|
FAR uint32_t *addr;
|
|
|
|
binfo("NXFLAT_RELOC_TYPE_REL32D Offset: %08" PRIx32 " D-Space: %p\n",
|
|
offset, loadinfo->dspace->region);
|
|
|
|
if (offset < loadinfo->dsize)
|
|
{
|
|
addr = (FAR uint32_t *)(offset + loadinfo->dspace->region);
|
|
binfo(" Before: %08" PRIx32 "\n", *addr);
|
|
*addr += (uint32_t)(loadinfo->dspace->region);
|
|
binfo(" After: %08" PRIx32 "\n", *addr);
|
|
return OK;
|
|
}
|
|
else
|
|
{
|
|
berr("Offset: %08" PRIx32 " does not lie in "
|
|
"D-Space size: %08" PRIx32 "\n",
|
|
offset, loadinfo->dsize);
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
|
|
/****************************************************************************
|
|
* 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,
|
|
uint32_t offset)
|
|
{
|
|
FAR uint32_t *addr;
|
|
|
|
binfo("NXFLAT_RELOC_TYPE_REL32D Offset: %08x D-Space: %p\n",
|
|
offset, loadinfo->dspace->region);
|
|
|
|
if (offset < loadinfo->dsize)
|
|
{
|
|
addr = (FAR uint32_t *)(offset + loadinfo->dspace->region);
|
|
binfo(" Before: %08x\n", *addr);
|
|
|
|
*addr += ((uint32_t)loadinfo->ispace -
|
|
(uint32_t)(loadinfo->dspace->region));
|
|
binfo(" After: %08x\n", *addr);
|
|
|
|
return OK;
|
|
}
|
|
else
|
|
{
|
|
berr("Offset: %08 does not lie in D-Space size: %08x\n",
|
|
offset, loadinfo->dsize);
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
/****************************************************************************
|
|
* 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;
|
|
uint32_t offset;
|
|
uint16_t nrelocs;
|
|
int ret;
|
|
int result;
|
|
int i;
|
|
|
|
/* The NXFLAT header is the first thing at the beginning of the ISpace. */
|
|
|
|
hdr = (FAR struct nxflat_hdr_s *)loadinfo->ispace;
|
|
|
|
/* From this, we can get the offset to the list of relocation entries */
|
|
|
|
offset = NTOHL(hdr->h_relocstart);
|
|
nrelocs = NTOHS(hdr->h_reloccount);
|
|
binfo("offset: %08lx nrelocs: %d\n", (long)offset, nrelocs);
|
|
|
|
/* 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.
|
|
*/
|
|
|
|
DEBUGASSERT(offset >= loadinfo->isize);
|
|
DEBUGASSERT(offset + nrelocs * sizeof(struct nxflat_reloc_s)
|
|
<= (loadinfo->isize + loadinfo->dsize));
|
|
|
|
relocs = (FAR struct nxflat_reloc_s *)
|
|
(offset - loadinfo->isize + loadinfo->dspace->region);
|
|
binfo("isize: %08lx dpsace: %p relocs: %p\n",
|
|
(long)loadinfo->isize, loadinfo->dspace->region, relocs);
|
|
|
|
/* All relocations are performed 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 relocations can be performed.
|
|
*/
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
ret = nxflat_addrenv_select(loadinfo);
|
|
if (ret < 0)
|
|
{
|
|
berr("ERROR: nxflat_addrenv_select() failed: %d\n", ret);
|
|
return ret;
|
|
}
|
|
#endif
|
|
|
|
/* Now, traverse the relocation list of and bind each GOT relocation. */
|
|
|
|
ret = OK; /* Assume success */
|
|
for (i = 0; i < nrelocs; i++)
|
|
{
|
|
/* Handle the relocation by the relocation type */
|
|
|
|
reloc = *relocs++;
|
|
result = OK;
|
|
|
|
switch (NXFLAT_RELOC_TYPE(reloc.r_info))
|
|
{
|
|
/* NXFLAT_RELOC_TYPE_REL32I Meaning: Object file contains a 32-bit
|
|
* offset into I-Space at the
|
|
* offset.
|
|
* Fixup: Add mapped I-Space address
|
|
* to the offset.
|
|
*/
|
|
|
|
case NXFLAT_RELOC_TYPE_REL32I:
|
|
{
|
|
result = nxflat_bindrel32i(loadinfo,
|
|
NXFLAT_RELOC_OFFSET(reloc.r_info));
|
|
}
|
|
break;
|
|
|
|
/* NXFLAT_RELOC_TYPE_REL32D Meaning: Object file contains a 32-bit
|
|
* offset into D-Space at the
|
|
* offset.
|
|
* Fixup: Add allocated D-Space address
|
|
* to the offset.
|
|
*/
|
|
|
|
case NXFLAT_RELOC_TYPE_REL32D:
|
|
{
|
|
result = nxflat_bindrel32d(loadinfo,
|
|
NXFLAT_RELOC_OFFSET(reloc.r_info));
|
|
}
|
|
break;
|
|
|
|
/* 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.
|
|
*/
|
|
|
|
#ifdef NXFLAT_RELOC_TYPE_REL32ID
|
|
case NXFLAT_RELOC_TYPE_REL32ID:
|
|
{
|
|
result = nxflat_bindrel32id(loadinfo,
|
|
NXFLAT_RELOC_OFFSET(reloc.r_info));
|
|
}
|
|
break;
|
|
#endif
|
|
|
|
default:
|
|
{
|
|
berr("ERROR: Unrecognized relocation type: %" PRId32 "\n",
|
|
(uint32_t)NXFLAT_RELOC_TYPE(reloc.r_info));
|
|
result = -EINVAL;
|
|
}
|
|
break;
|
|
}
|
|
|
|
/* Check for failures */
|
|
|
|
if (result < 0 && ret == OK)
|
|
{
|
|
ret = result;
|
|
}
|
|
}
|
|
|
|
/* Dump the relocation got */
|
|
|
|
#ifdef CONFIG_NXFLAT_DUMPBUFFER
|
|
if (ret == OK && nrelocs > 0)
|
|
{
|
|
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));
|
|
}
|
|
#endif
|
|
|
|
/* Restore the original address environment */
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
ret = nxflat_addrenv_restore(loadinfo);
|
|
if (ret < 0)
|
|
{
|
|
berr("ERROR: nxflat_addrenv_restore() failed: %d\n", ret);
|
|
}
|
|
#endif
|
|
|
|
return ret;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Name: nxflat_bindimports
|
|
*
|
|
* 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.
|
|
*
|
|
****************************************************************************/
|
|
|
|
static inline int nxflat_bindimports(FAR struct nxflat_loadinfo_s *loadinfo,
|
|
FAR const struct symtab_s *exports,
|
|
int nexports)
|
|
{
|
|
FAR struct nxflat_import_s *imports;
|
|
FAR struct nxflat_hdr_s *hdr;
|
|
FAR const struct symtab_s *symbol;
|
|
|
|
FAR char *symname;
|
|
uint32_t offset;
|
|
uint16_t nimports;
|
|
bool abi_ok = false;
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
int ret;
|
|
#endif
|
|
int i;
|
|
|
|
/* The NXFLAT header is the first thing at the beginning of the ISpace. */
|
|
|
|
hdr = (FAR struct nxflat_hdr_s *)loadinfo->ispace;
|
|
|
|
/* 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.
|
|
*/
|
|
|
|
offset = NTOHL(hdr->h_importsymbols);
|
|
nimports = NTOHS(hdr->h_importcount);
|
|
binfo("Imports offset: %08" PRIx32 " nimports: %d\n", offset, nimports);
|
|
|
|
/* The import[] table 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 import[] table can be modified.
|
|
*/
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
ret = nxflat_addrenv_select(loadinfo);
|
|
if (ret < 0)
|
|
{
|
|
berr("ERROR: nxflat_addrenv_select() failed: %d\n", ret);
|
|
return ret;
|
|
}
|
|
#endif
|
|
|
|
/* Verify that this module requires imported symbols */
|
|
|
|
if (offset != 0 && nimports > 0)
|
|
{
|
|
/* It does.. make sure that exported symbols are provided */
|
|
|
|
DEBUGASSERT(exports && nexports > 0);
|
|
|
|
/* 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);
|
|
|
|
imports = (FAR struct nxflat_import_s *)
|
|
(offset - loadinfo->isize + loadinfo->dspace->region);
|
|
|
|
/* Now, traverse the list of imported symbols and attempt to bind
|
|
* each symbol to the value exported by from the exported symbol
|
|
* table.
|
|
*/
|
|
|
|
for (i = 0; i < nimports; i++)
|
|
{
|
|
binfo("Import[%d] (%p) "
|
|
"offset: %08" PRIx32 " func: %08" PRIx32 "\n",
|
|
i, &imports[i], imports[i].i_funcname,
|
|
imports[i].i_funcaddress);
|
|
|
|
/* 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.
|
|
*/
|
|
|
|
offset = imports[i].i_funcname;
|
|
DEBUGASSERT(offset < loadinfo->isize);
|
|
|
|
symname = (FAR char *)
|
|
(offset + loadinfo->ispace + sizeof(struct nxflat_hdr_s));
|
|
|
|
/* Note the ABI marker as it goes past. It resolves like any
|
|
* other import -- the base firmware defines it below -- so the
|
|
* only thing special about it is that its absence means the
|
|
* module was built before the ABI it names.
|
|
*/
|
|
|
|
if (strcmp(symname, NXFLAT_ABI_SYMBOL) == 0)
|
|
{
|
|
abi_ok = true;
|
|
}
|
|
|
|
/* Find the exported symbol value for this symbol name. */
|
|
|
|
symbol = symtab_findbyname(exports, symname, nexports);
|
|
if (!symbol)
|
|
{
|
|
berr("Exported symbol \"%s\" not found\n", symname);
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
nxflat_addrenv_restore(loadinfo);
|
|
#endif
|
|
return -ENOENT;
|
|
}
|
|
|
|
/* And put this into the module's import structure. */
|
|
|
|
imports[i].i_funcaddress = (uint32_t)symbol->sym_value;
|
|
|
|
binfo("Bound import[%d] (%p) to export '%s' (%08" PRIx32 ")\n",
|
|
i, &imports[i], symname, imports[i].i_funcaddress);
|
|
}
|
|
}
|
|
|
|
/* Dump the relocation import table */
|
|
|
|
/* A module that never declared the ABI was built by a toolchain older
|
|
* than the move of the PIC base register to r9. Its import thunks add
|
|
* r10, so it would load here and then branch to a wild address on its
|
|
* first call into the base firmware. Refuse it while there is still
|
|
* something useful to say about it.
|
|
*/
|
|
|
|
if (!abi_ok)
|
|
{
|
|
berr("ERROR: Module does not declare " NXFLAT_ABI_SYMBOL ": it was "
|
|
"built by a toolchain predating the r9 PIC base register. "
|
|
"Rebuild it.\n");
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
nxflat_addrenv_restore(loadinfo);
|
|
#endif
|
|
return -ENOEXEC;
|
|
}
|
|
|
|
#ifdef CONFIG_NXFLAT_DUMPBUFFER
|
|
if (nimports > 0)
|
|
{
|
|
nxflat_dumpbuffer("Imports", (FAR const uint8_t *)imports,
|
|
nimports * sizeof(struct nxflat_import_s));
|
|
}
|
|
#endif
|
|
|
|
/* Restore the original address environment */
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
ret = nxflat_addrenv_restore(loadinfo);
|
|
if (ret < 0)
|
|
{
|
|
berr("ERROR: nxflat_addrenv_restore() failed: %d\n", ret);
|
|
}
|
|
|
|
return ret;
|
|
#else
|
|
return OK;
|
|
#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)
|
|
{
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
int ret;
|
|
#endif
|
|
|
|
/* .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
|
|
* accessed.
|
|
*/
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
ret = nxflat_addrenv_select(loadinfo);
|
|
if (ret < 0)
|
|
{
|
|
berr("ERROR: nxflat_addrenv_select() failed: %d\n", ret);
|
|
return ret;
|
|
}
|
|
#endif
|
|
|
|
/* Zero the BSS area */
|
|
|
|
memset((FAR void *)(loadinfo->dspace->region + loadinfo->datasize), 0,
|
|
loadinfo->bsssize);
|
|
|
|
/* Restore the original address environment */
|
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
ret = nxflat_addrenv_restore(loadinfo);
|
|
if (ret < 0)
|
|
{
|
|
berr("ERROR: nxflat_addrenv_restore() failed: %d\n", ret);
|
|
}
|
|
|
|
return ret;
|
|
#else
|
|
return OK;
|
|
#endif
|
|
}
|
|
|
|
/****************************************************************************
|
|
* 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'.
|
|
* After binding the module, clear the BSS region (which held the
|
|
* relocation data) in preparation for execution.
|
|
*
|
|
* 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)
|
|
{
|
|
/* Bind the imported symbol, absolute relocations separately. This is done
|
|
* before the standard relocations because that logic may modify the
|
|
* import list (for the better hopefully, but we don't want to depend on
|
|
* it).
|
|
*/
|
|
|
|
int ret = nxflat_bindimports(loadinfo, exports, nexports);
|
|
if (ret == OK)
|
|
{
|
|
/* Then bind all GOT relocations */
|
|
|
|
ret = nxflat_gotrelocs(loadinfo);
|
|
if (ret == OK)
|
|
{
|
|
/* Zero the BSS area, trashing the relocations that lived in that
|
|
* space in the loaded file.
|
|
*/
|
|
|
|
ret = nxflat_clearbss(loadinfo);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|