blob: 8a270a80914e0eb72dc421ab929918241bf44009 [file] [log] [blame]
<!--
Documentation/_templates/layout.html
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.
-->
<!DOCTYPE html>
<html class="writer-html5" lang="en">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Binary Loader &mdash; NuttX latest documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/css/theme.css" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="../_static/custom.css" />
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<script src="../_static/jquery.js"></script>
<script src="../_static/_sphinx_javascript_frameworks_compat.js"></script>
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>
<script src="../_static/clipboard.min.js"></script>
<script src="../_static/copybutton.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Device Drivers" href="drivers/index.html" />
<link rel="prev" title="OS Components" href="index.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> NuttX
</a>
<!-- this version selector is quite ugly, should be probably replaced by something
more modern -->
<div class="version-selector">
<select onchange="javascript:location.href = this.value;">
<option value="../../latest" selected="selected">latest</option>
<option value="../../10.0.0" >10.0.0</option>
<option value="../../10.0.1" >10.0.1</option>
<option value="../../10.1.0" >10.1.0</option>
<option value="../../10.2.0" >10.2.0</option>
<option value="../../10.3.0" >10.3.0</option>
<option value="../../11.0.0" >11.0.0</option>
<option value="../../12.0.0" >12.0.0</option>
<option value="../../12.1.0" >12.1.0</option>
<option value="../../12.2.0" >12.2.0</option>
<option value="../../12.2.1" >12.2.1</option>
<option value="../../12.3.0" >12.3.0</option>
<option value="../../12.4.0" >12.4.0</option>
<option value="../../12.5.0" >12.5.0</option>
<option value="../../12.5.1" >12.5.1</option>
<option value="../../12.6.0" >12.6.0</option>
<option value="../../12.7.0" >12.7.0</option>
<option value="../../12.8.0" >12.8.0</option>
<option value="../../12.9.0" >12.9.0</option>
<option value="../../12.10.0" >12.10.0</option>
<option value="../../12.11.0" >12.11.0</option>
</select>
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Table of Contents</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">Home</a></li>
<li class="toctree-l1"><a class="reference internal" href="../introduction/index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quickstart/index.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../introduction/inviolables.html">The Inviolable Principles of NuttX</a></li>
<li class="toctree-l1"><a class="reference internal" href="../platforms/index.html">Supported Platforms</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">OS Components</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="#">Binary Loader</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#binary-loader-interface">Binary Loader Interface</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#header-files">Header Files</a></li>
<li class="toctree-l4"><a class="reference internal" href="#data-structures">Data Structures</a></li>
<li class="toctree-l4"><a class="reference internal" href="#function-interfaces">Function Interfaces</a><ul>
<li class="toctree-l5"><a class="reference internal" href="#binary-format-management">Binary format management</a></li>
<li class="toctree-l5"><a class="reference internal" href="#basic-module-management">Basic module management</a></li>
<li class="toctree-l5"><a class="reference internal" href="#path-traversal-logic"><code class="docutils literal notranslate"><span class="pre">PATH</span></code> traversal logic</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#symbol-tables">Symbol Tables</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#symbol-table-header-files">Symbol Table Header Files</a></li>
<li class="toctree-l4"><a class="reference internal" href="#symbol-table-data-structures">Symbol Table Data Structures</a></li>
<li class="toctree-l4"><a class="reference internal" href="#symbol-table-function-interfaces">Symbol Table Function Interfaces</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#configuration-variables">Configuration Variables</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="drivers/index.html">Device Drivers</a></li>
<li class="toctree-l2"><a class="reference internal" href="nxflat.html">NXFLAT</a></li>
<li class="toctree-l2"><a class="reference internal" href="nxgraphics/index.html">NX Graphics Subsystem</a></li>
<li class="toctree-l2"><a class="reference internal" href="paging.html">On-Demand Paging</a></li>
<li class="toctree-l2"><a class="reference internal" href="audio/index.html">Audio Subsystem</a></li>
<li class="toctree-l2"><a class="reference internal" href="filesystem/index.html">NuttX File System</a></li>
<li class="toctree-l2"><a class="reference internal" href="libs/index.html">NuttX libraries</a></li>
<li class="toctree-l2"><a class="reference internal" href="net/index.html">Network Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="mm/index.html">Memory Management</a></li>
<li class="toctree-l2"><a class="reference internal" href="syscall.html">Syscall Layer</a></li>
<li class="toctree-l2"><a class="reference internal" href="tools/index.html"><code class="docutils literal notranslate"><span class="pre">/tools</span></code> Host Tools</a></li>
<li class="toctree-l2"><a class="reference internal" href="arch/index.html">Architecture-Specific Code</a></li>
<li class="toctree-l2"><a class="reference internal" href="boards.html">Boards Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="cmake.html">CMake Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="openamp.html">OpenAMP Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="video.html">Video Subsystem</a></li>
<li class="toctree-l2"><a class="reference internal" href="crypto.html">Crypto API Subsystem</a></li>
<li class="toctree-l2"><a class="reference internal" href="wireless.html">Wireless Subsystem</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../applications/index.html">Applications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../implementation/index.html">Implementation Details</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index.html">FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="../guides/index.html">Guides</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
<li class="toctree-l1"><a class="reference internal" href="../logos/index.html">NuttX Logos</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">NuttX</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item"><a href="index.html">OS Components</a></li>
<li class="breadcrumb-item active">Binary Loader</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/components/binfmt.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="binary-loader">
<span id="binfmt"></span><h1>Binary Loader<a class="headerlink" href="#binary-loader" title="Permalink to this heading"></a></h1>
<p>The purpose of a <em>binary loader</em> is to load and
execute modules in various <em>binary formats</em> that reside in a file
system. Loading refers instantiating the binary module in some fashion,
usually copy all or some of the binary module into memory and then
linking the module with other components. In most architectures, it is
the base FLASH code that is the primary component that the binary module
must link with because that is where the RTOS and primary tasks reside.
Program modules can then be executed after they have been loaded.</p>
<p><strong>Binary Formats</strong>. The binary loader provides generic support for
different binary formats. It supports a <em>registration interface</em> that
allows the number of support binary formats to be loaded at run time.
Each binary format provides a common, interface for use by the binary
loader. When asked to load a binary, the binary loader will query each
registered binary format, providing it with the path of the binary
object to be loaded. The binary loader will stop when first binary
format the recognizes the binary object and successfully loads it or
when all registered binary formats have attempt loading the binary
object and failed.</p>
<p>At present, the following binary formats are support by NuttX:</p>
<blockquote>
<div><ul class="simple">
<li><p><strong>ELF</strong>. Standard ELF formatted files.</p></li>
<li><p><strong>NXFLAT</strong>. NuttX NXFLAT formatted files. More information about the
NXFLAT binary format can be found in the <a class="reference internal" href="nxflat.html#nxflat"><span class="std std-ref">NXFLAT
documentation</span></a>.</p></li>
</ul>
</div></blockquote>
<p><strong>Executables and Libraries</strong> The generic binary loader logic does not
care what it is that it being loaded. It could load an executable
program or a library. There are no strict rules, but a library will tend
to export symbols and a program will tend to import symbols: The program
will use the symbols exported by the library. However, at this point in
time, none of the supported binary formats support exporting of symbols.</p>
<p><strong>binfmt</strong>. In the NuttX source code, the short name <code class="docutils literal notranslate"><span class="pre">binfmt</span></code> is used
to refer to the NuttX binary loader. This is the name of the directory
containing the binary loader and the name of the header files and
variables used by the binary loader.</p>
<p>The name <code class="docutils literal notranslate"><span class="pre">binfmt</span></code> is the same name used by the Linux binary loader.
However, the NuttX binary loader is an independent development and
shares nothing with the Linux binary loader other the same name and the
same basic functionality.</p>
<section id="binary-loader-interface">
<h2>Binary Loader Interface<a class="headerlink" href="#binary-loader-interface" title="Permalink to this heading"></a></h2>
<section id="header-files">
<h3>Header Files<a class="headerlink" href="#header-files" title="Permalink to this heading"></a></h3>
<p>The interface to the binary loader is described in the header file
<code class="docutils literal notranslate"><span class="pre">include/nuttx/binfmt/binfmt.h</span></code>.
A brief summary of the data structures and interfaces prototyped in that
header file are listed below.</p>
</section>
<section id="data-structures">
<h3>Data Structures<a class="headerlink" href="#data-structures" title="Permalink to this heading"></a></h3>
<p>When a binary format registers with the binary loader, it provides a
pointer to a write-able instance of <a class="reference internal" href="#c.binfmt_s" title="binfmt_s"><code class="xref c c-struct docutils literal notranslate"><span class="pre">binfmt_s</span></code></a>.</p>
<dl class="c struct">
<dt class="sig sig-object c" id="c.binfmt_s">
<span class="k"><span class="pre">struct</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">binfmt_s</span></span></span><a class="headerlink" href="#c.binfmt_s" title="Permalink to this definition"></a><br /></dt>
<dd><div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">binfmt_s</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">binfmt_s</span><span class="w"> </span><span class="o">*</span><span class="n">next</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Supports a singly-linked list */</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">load</span><span class="p">)(</span><span class="n">FAR</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">binary_s</span><span class="w"> </span><span class="o">*</span><span class="n">bin</span><span class="p">);</span><span class="w"> </span><span class="cm">/* Verify and load binary into memory */</span>
<span class="p">};</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">load</span></code> method is used to load the binary format into memory. It
returns either <code class="docutils literal notranslate"><span class="pre">OK</span></code> (0) meaning that the binary object was loaded
successfully, or a negated <code class="docutils literal notranslate"><span class="pre">errno</span></code> indicating why the object was not
loaded.</p>
</dd></dl>
<dl class="c struct">
<dt class="sig sig-object c" id="c.binary_s">
<span class="k"><span class="pre">struct</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">binary_s</span></span></span><a class="headerlink" href="#c.binary_s" title="Permalink to this definition"></a><br /></dt>
<dd><p>The type <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">binary_s</span></code> is use both to (1) describe the binary
object to be loaded, and if successfully loaded, (2) to provide
information about where and how the binary object was loaded. That
structure is shown below:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">symtab_s</span><span class="p">;</span>
<span class="k">struct</span><span class="w"> </span><span class="nc">binary_s</span>
<span class="p">{</span>
<span class="w"> </span><span class="cm">/* Information provided to the loader to load and bind a module */</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">filename</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Full path to the binary to be loaded */</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">argv</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Argument list */</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">symtab_s</span><span class="w"> </span><span class="o">*</span><span class="n">exports</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Table of exported symbols */</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">nexports</span><span class="p">;</span><span class="w"> </span><span class="cm">/* The number of symbols in exports[] */</span>
<span class="w"> </span><span class="cm">/* Information provided from the loader (if successful) describing the</span>
<span class="cm"> * resources used by the loaded module.</span>
<span class="cm"> */</span>
<span class="w"> </span><span class="n">main_t</span><span class="w"> </span><span class="n">entrypt</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Entry point into a program module */</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">mapped</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Memory-mapped, address space */</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">alloc</span><span class="p">[</span><span class="n">BINFMT_NALLOC</span><span class="p">];</span><span class="w"> </span><span class="cm">/* Allocated address spaces */</span>
<span class="w"> </span><span class="cm">/* Constructors/destructors */</span>
<span class="cp">#ifdef CONFIG_BINFMT_CONSTRUCTORS</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="n">binfmt_ctor_t</span><span class="w"> </span><span class="o">*</span><span class="n">ctors</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Pointer to a list of constructors */</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="n">binfmt_dtor_t</span><span class="w"> </span><span class="o">*</span><span class="n">dtors</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Pointer to a list of destructors */</span>
<span class="w"> </span><span class="kt">uint16_t</span><span class="w"> </span><span class="n">nctors</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Number of constructors in the list */</span>
<span class="w"> </span><span class="kt">uint16_t</span><span class="w"> </span><span class="n">ndtors</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Number of destructors in the list */</span>
<span class="cp">#endif</span>
<span class="w"> </span><span class="cm">/* Address environment.</span>
<span class="cm"> *</span>
<span class="cm"> * addrenv - This is the handle created by up_addrenv_create() that can be</span>
<span class="cm"> * used to manage the tasks address space.</span>
<span class="cm"> */</span>
<span class="cp">#ifdef CONFIG_ARCH_ADDRENV</span>
<span class="w"> </span><span class="n">arch_addrenv_t</span><span class="w"> </span><span class="n">addrenv</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Task group address environment */</span>
<span class="cp">#endif</span>
<span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">mapsize</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Size of the mapped address region (needed for munmap) */</span>
<span class="w"> </span><span class="cm">/* Start-up information that is provided by the loader, but may be modified</span>
<span class="cm"> * by the caller between load_module() and exec_module() calls.</span>
<span class="cm"> */</span>
<span class="w"> </span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">priority</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Task execution priority */</span>
<span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">stacksize</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Size of the stack in bytes (unallocated) */</span>
<span class="cp">#ifndef CONFIG_BUILD_KERNEL</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">stackaddr</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Task stack address */</span>
<span class="cp">#endif</span>
<span class="p">};</span>
</pre></div>
</div>
<p>Where the types <code class="docutils literal notranslate"><span class="pre">binfmt_ctor_t</span></code> and <code class="docutils literal notranslate"><span class="pre">binfmt_dtor_t</span></code> define the type
of one C++ constructor or destructor:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="nf">void</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">binfmt_ctor_t</span><span class="p">)(</span><span class="kt">void</span><span class="p">);</span>
<span class="k">typedef</span><span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="nf">void</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">binfmt_dtor_t</span><span class="p">)(</span><span class="kt">void</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
</section>
<section id="function-interfaces">
<h3>Function Interfaces<a class="headerlink" href="#function-interfaces" title="Permalink to this heading"></a></h3>
<section id="binary-format-management">
<h4>Binary format management<a class="headerlink" href="#binary-format-management" title="Permalink to this heading"></a></h4>
<dl class="c function">
<dt class="sig sig-object c" id="c.register_binfmt">
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">register_binfmt</span></span></span><span class="sig-paren">(</span><span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><a class="reference internal" href="#c.binfmt_s" title="binfmt_s"><span class="n"><span class="pre">binfmt_s</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">binfmt</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.register_binfmt" title="Permalink to this definition"></a><br /></dt>
<dd><p>Register a loader for a binary format.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns<span class="colon">:</span></dt>
<dd class="field-odd"><p>This is a NuttX internal function so it follows the convention
that 0 (OK) is returned on success and a negated errno is returned on
failure.</p>
</dd>
</dl>
</dd></dl>
<dl class="c function">
<dt class="sig sig-object c" id="c.unregister_binfmt">
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">unregister_binfmt</span></span></span><span class="sig-paren">(</span><span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><a class="reference internal" href="#c.binfmt_s" title="binfmt_s"><span class="n"><span class="pre">binfmt_s</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">binfmt</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.unregister_binfmt" title="Permalink to this definition"></a><br /></dt>
<dd><p>Register a loader for a binary format.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns<span class="colon">:</span></dt>
<dd class="field-odd"><p>This is a NuttX internal function so it follows the convention
that 0 (OK) is returned on success and a negated errno is returned on
failure.</p>
</dd>
</dl>
</dd></dl>
</section>
<section id="basic-module-management">
<h4>Basic module management<a class="headerlink" href="#basic-module-management" title="Permalink to this heading"></a></h4>
<dl class="c function">
<dt class="sig sig-object c" id="c.load_module">
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">load_module</span></span></span><span class="sig-paren">(</span><span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><a class="reference internal" href="#c.binary_s" title="binary_s"><span class="n"><span class="pre">binary_s</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">bin</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.load_module" title="Permalink to this definition"></a><br /></dt>
<dd><p>Load a module into memory, bind it to an exported symbol take,
and prep the module for execution.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>bin</strong> – The <code class="docutils literal notranslate"><span class="pre">filename</span></code> field will be used
in order to locate the module to be loaded from the file system.
The filename must be the full, absolute path to the file to be executed
unless <code class="docutils literal notranslate"><span class="pre">CONFIG_LIBC_ENVPATH</span></code> is defined. In that case, filename may be
a relative path; a set of candidate absolute paths will be generated using
the <code class="docutils literal notranslate"><span class="pre">PATH</span></code> environment variable and <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> will attempt to load each
file that is found at those absolute paths.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p>This is a NuttX internal function so it follows the convention that 0 (<code class="docutils literal notranslate"><span class="pre">OK</span></code>)
is returned on success and a negated <code class="docutils literal notranslate"><span class="pre">errno</span></code> is returned on failure.</p>
</dd>
</dl>
</dd></dl>
<dl class="c function">
<dt class="sig sig-object c" id="c.unload_module">
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">unload_module</span></span></span><span class="sig-paren">(</span><span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><a class="reference internal" href="#c.binary_s" title="binary_s"><span class="n"><span class="pre">binary_s</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">bin</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.unload_module" title="Permalink to this definition"></a><br /></dt>
<dd><p>Unload a (non-executing) module from memory. If the module has been started
(via <a class="reference internal" href="#c.exec_module" title="exec_module"><code class="xref c c-func docutils literal notranslate"><span class="pre">exec_module()</span></code></a>) and has not exited, calling this will be fatal.</p>
<p>However, this function must be called after the module exist. How this is
done is up to your logic. Perhaps you register it to be called by <a class="reference internal" href="../reference/user/03_task_control.html#c.on_exit" title="on_exit"><code class="xref c c-func docutils literal notranslate"><span class="pre">on_exit()</span></code></a>?</p>
<dl class="field-list simple">
<dt class="field-odd">Returns<span class="colon">:</span></dt>
<dd class="field-odd"><p>This is a NuttX internal function so it follows the convention that 0 (<code class="docutils literal notranslate"><span class="pre">OK</span></code>)
is returned on success and a negated <code class="docutils literal notranslate"><span class="pre">errno</span></code> is returned on failure.</p>
</dd>
</dl>
</dd></dl>
<dl class="c function">
<dt class="sig sig-object c" id="c.exec_module">
<span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">exec_module</span></span></span><span class="sig-paren">(</span><span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><a class="reference internal" href="#c.binary_s" title="binary_s"><span class="n"><span class="pre">binary_s</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">bin</span></span><span class="sig-paren">)</span><span class="p"><span class="pre">;</span></span><a class="headerlink" href="#c.exec_module" title="Permalink to this definition"></a><br /></dt>
<dd><p>Execute a module that has been loaded into memory by <a class="reference internal" href="#c.load_module" title="load_module"><code class="xref c c-func docutils literal notranslate"><span class="pre">load_module()</span></code></a>.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns<span class="colon">:</span></dt>
<dd class="field-odd"><p>This is a NuttX internal function so it follows the convention that 0 (<code class="docutils literal notranslate"><span class="pre">OK</span></code>)
is returned on success and a negated <code class="docutils literal notranslate"><span class="pre">errno</span></code> is returned on failure.</p>
</dd>
</dl>
</dd></dl>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>The function <a class="reference internal" href="../reference/user/01_task_control.html#c.exec" title="exec"><code class="xref c c-func docutils literal notranslate"><span class="pre">exec()</span></code></a> is a convenience function that wraps
<a class="reference internal" href="#c.load_module" title="load_module"><code class="xref c c-func docutils literal notranslate"><span class="pre">load_module()</span></code></a> and <a class="reference internal" href="#c.exec_module" title="exec_module"><code class="xref c c-func docutils literal notranslate"><span class="pre">exec_module()</span></code></a> into one call.</p>
</div>
</section>
<section id="path-traversal-logic">
<h4><code class="docutils literal notranslate"><span class="pre">PATH</span></code> traversal logic<a class="headerlink" href="#path-traversal-logic" title="Permalink to this heading"></a></h4>
<dl class="c function">
<dt class="sig sig-object c" id="c.envpath_init">
<span class="n"><span class="pre">ENVPATH_HANDLE</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">envpath_init</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><span class="p"><span class="pre">;</span></span><a class="headerlink" href="#c.envpath_init" title="Permalink to this definition"></a><br /></dt>
<dd><p>Initialize for the traversal of each value in the <code class="docutils literal notranslate"><span class="pre">PATH</span></code> variable. The
usage is sequence is as follows:</p>
<ol class="arabic simple">
<li><p>Call <a class="reference internal" href="#c.envpath_init" title="envpath_init"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_init()</span></code></a> to initialize for the traversal.
<code class="docutils literal notranslate"><span class="pre">envpath_init()</span></code> will return an opaque handle that can then be
provided to <a class="reference internal" href="#c.envpath_next" title="envpath_next"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_next()</span></code></a> and <a class="reference internal" href="#c.envpath_release" title="envpath_release"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_release()</span></code></a>.</p></li>
<li><p>Call <a class="reference internal" href="#c.envpath_next" title="envpath_next"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_next()</span></code></a> repeatedly to examine every file that lies in
the directories of the <code class="docutils literal notranslate"><span class="pre">PATH</span></code> variable.</p></li>
<li><p>Call <a class="reference internal" href="#c.envpath_release" title="envpath_release"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_release()</span></code></a> to free resources set aside by
<a class="reference internal" href="#c.envpath_init" title="envpath_init"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_init()</span></code></a>.</p></li>
</ol>
<dl class="field-list simple">
<dt class="field-odd">Returns<span class="colon">:</span></dt>
<dd class="field-odd"><p>On success, <a class="reference internal" href="#c.envpath_init" title="envpath_init"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_init()</span></code></a> return a non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>, opaque handle
that may subsequently be used in calls to <a class="reference internal" href="#c.envpath_next" title="envpath_next"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_next()</span></code></a> and
<a class="reference internal" href="#c.envpath_release" title="envpath_release"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_release()</span></code></a>. On error, a <code class="docutils literal notranslate"><span class="pre">NULL</span></code> handle value will be returned.
The most likely cause of an error would be that there is no value
associated with the <code class="docutils literal notranslate"><span class="pre">PATH</span></code> variable.</p>
</dd>
</dl>
</dd></dl>
<dl class="c function">
<dt class="sig sig-object c" id="c.envpath_next">
<span class="pre">FAR</span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">envpath_next</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">ENVPATH_HANDLE</span></span><span class="w"> </span><span class="n"><span class="pre">handle</span></span>, <span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">relpath</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.envpath_next" title="Permalink to this definition"></a><br /></dt>
<dd><p>Traverse all possible values in the PATH variable in attempt to find the
full path to an executable file when only a relative path is provided.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>handle</strong> – The handle value returned by <a class="reference internal" href="#c.envpath_init" title="envpath_init"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_init()</span></code></a>.</p></li>
<li><p><strong>relpath</strong> – The relative path to the file to be found.</p></li>
</ul>
</dd>
<dt class="field-even">Returns<span class="colon">:</span></dt>
<dd class="field-even"><p>On success, a non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer to a null-terminated string is provided.
This is the full path to a file that exists in the file system.
This function will verify that the file exists (but will not verify that it is marked executable).</p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The string pointer return in the success case points to allocated memory.
This memory must be freed by the called by calling <code class="xref c c-func docutils literal notranslate"><span class="pre">kmm_free()</span></code>.</p>
</div>
<p><code class="docutils literal notranslate"><span class="pre">NULL</span></code> relpath from any absolute path in the <code class="docutils literal notranslate"><span class="pre">PATH</span></code> variable.
In this case, there is no point in calling <a class="reference internal" href="#c.envpath_next" title="envpath_next"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_next()</span></code></a> further;
<a class="reference internal" href="#c.envpath_release" title="envpath_release"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_release()</span></code></a> must be called to release resources set aside by
<a class="reference internal" href="#c.envpath_init" title="envpath_init"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_init()</span></code></a>.</p>
</dd></dl>
<dl class="c function">
<dt class="sig sig-object c" id="c.envpath_release">
<span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">envpath_release</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">ENVPATH_HANDLE</span></span><span class="w"> </span><span class="n"><span class="pre">handle</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.envpath_release" title="Permalink to this definition"></a><br /></dt>
<dd></dd></dl>
<p>Release all resources set aside by envpath_init when the
handle value was created. The handle value is invalid on
return from this function. Attempts to all <a class="reference internal" href="#c.envpath_next" title="envpath_next"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_next()</span></code></a>
or <a class="reference internal" href="#c.envpath_release" title="envpath_release"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_release()</span></code></a> with such a stale handle will result
in undefined (i.e., not good) behavior.</p>
<blockquote>
<div><dl class="field-list simple">
<dt class="field-odd">param handle<span class="colon">:</span></dt>
<dd class="field-odd"><p>The handle value returned by <a class="reference internal" href="#c.envpath_init" title="envpath_init"><code class="xref c c-func docutils literal notranslate"><span class="pre">envpath_init()</span></code></a>.</p>
</dd>
</dl>
</div></blockquote>
</section>
</section>
</section>
<section id="symbol-tables">
<h2>Symbol Tables<a class="headerlink" href="#symbol-tables" title="Permalink to this heading"></a></h2>
<p><strong>Symbol Tables</strong>. Symbol tables are lists of name value mappings: The
name is a string that identifies a symbol, and the value is an address
in memory where the symbol of that name has been positioned. In most
NuttX architectures symbol tables are required, as a minimum, in order
to dynamically link the loaded binary object with the base code on
FLASH. Since the binary object was separately built and separately
linked, these symbols will appear as <em>undefined</em> symbols in the binary
object. The binary loader will use the symbol table to look up the
symbol by its name and to provide the address associated with the symbol
as needed to perform the dynamic linking of the binary object to the
base FLASH code.</p>
<p>Some toolchains will prefix symbols with an underscore. To support these
toolchains the <code class="docutils literal notranslate"><span class="pre">CONFIG_SYMTAB_DECORATED</span></code> setting may be defined. This
will cause a leading underscore to be ignored on <em>undefined</em> symbols
during dynamic linking.</p>
<section id="symbol-table-header-files">
<h3>Symbol Table Header Files<a class="headerlink" href="#symbol-table-header-files" title="Permalink to this heading"></a></h3>
<p>The interface to the symbol table logic is described in the header file
<code class="docutils literal notranslate"><span class="pre">include/nuttx/binfmt/symtab.h</span></code>.
A brief summary of the data structurs and interfaces prototyped in that
header file are listed below.</p>
</section>
<section id="symbol-table-data-structures">
<h3>Symbol Table Data Structures<a class="headerlink" href="#symbol-table-data-structures" title="Permalink to this heading"></a></h3>
<dl class="c struct">
<dt class="sig sig-object c" id="c.symbtab_s">
<span class="k"><span class="pre">struct</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">symbtab_s</span></span></span><a class="headerlink" href="#c.symbtab_s" title="Permalink to this definition"></a><br /></dt>
<dd><p>Describes one entry in the symbol table.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">symtab_s</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">sym_name</span><span class="p">;</span><span class="w"> </span><span class="cm">/* A pointer to the symbol name string */</span>
<span class="w"> </span><span class="n">FAR</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">sym_value</span><span class="p">;</span><span class="w"> </span><span class="cm">/* The value associated with the string */</span>
<span class="p">};</span>
</pre></div>
</div>
<p>A symbol table is a fixed size array of <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">symtab_s</span></code>. The
information is intentionally minimal and supports only:</p>
<ol class="arabic simple">
<li><p>Function pointers as <code class="docutils literal notranslate"><span class="pre">sym_values</span></code>. Of other kinds of values need to
be supported, then typing information would also need to be included
in the structure.</p></li>
<li><p>Fixed size arrays. There is no explicit provisional for dynamically
adding or removing entries from the symbol table (realloc might be
used for that purpose if needed). The intention is to support only
fixed size arrays completely defined at compilation or link time.</p></li>
</ol>
</dd></dl>
</section>
<section id="symbol-table-function-interfaces">
<h3>Symbol Table Function Interfaces<a class="headerlink" href="#symbol-table-function-interfaces" title="Permalink to this heading"></a></h3>
<dl class="c function">
<dt class="sig sig-object c" id="c.symtab_findbyname">
<span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><span class="n"><span class="pre">symtab_s</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">symtab_findbyname</span></span></span><span class="sig-paren">(</span><span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><span class="n"><span class="pre">symtab_s</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">symtab</span></span>, <span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">nsyms</span></span><span class="sig-paren">)</span><span class="p"><span class="pre">;</span></span><a class="headerlink" href="#c.symtab_findbyname" title="Permalink to this definition"></a><br /></dt>
<dd><p>Find the symbol in the symbol table with the matching name.
The implementation will be linear with respect to <code class="docutils literal notranslate"><span class="pre">nsyms</span></code> if
<code class="docutils literal notranslate"><span class="pre">CONFIG_SYMTAB_ORDEREDBYNAME</span></code> is not selected, and logarithmic
if it is.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns<span class="colon">:</span></dt>
<dd class="field-odd"><p>A reference to the symbol table entry if an entry with
the matching name is found; NULL is returned if the entry is not found.</p>
</dd>
</dl>
</dd></dl>
<dl class="c function">
<dt class="sig sig-object c" id="c.symtab_findbyvalue">
<span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><span class="n"><span class="pre">symtab_s</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">symtab_findbyvalue</span></span></span><span class="sig-paren">(</span><span class="pre">FAR</span><span class="w"> </span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="k"><span class="pre">struct</span></span><span class="w"> </span><span class="n"><span class="pre">symtab_s</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">symtab</span></span>, <span class="pre">FAR</span><span class="w"> </span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">value</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">nsyms</span></span><span class="sig-paren">)</span><span class="p"><span class="pre">;</span></span><a class="headerlink" href="#c.symtab_findbyvalue" title="Permalink to this definition"></a><br /></dt>
<dd><p>Find the symbol in the symbol table whose value closest
(but not greater than), the provided value. This version assumes
that table is not ordered with respect to symbol value and, hence,
access time will be linear with respect to <code class="docutils literal notranslate"><span class="pre">nsyms</span></code>.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns<span class="colon">:</span></dt>
<dd class="field-odd"><p>A reference to the symbol table entry if an entry with the matching
value is found; <code class="docutils literal notranslate"><span class="pre">NULL</span></code> is returned if the entry is not found.</p>
</dd>
</dl>
</dd></dl>
</section>
</section>
<section id="configuration-variables">
<h2>Configuration Variables<a class="headerlink" href="#configuration-variables" title="Permalink to this heading"></a></h2>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">CONFIG_BINFMT_DISABLE</span></code>: By default, support for loadable binary formats is built.
This logic may be suppressed be defining this setting.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">CONFIG_BINFMT_CONSTRUCTORS</span></code>: Build in support for C++ constructors in loaded modules.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">CONFIG_SYMTAB_ORDEREDBYNAME</span></code>: Symbol tables are order by name (rather than value).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">CONFIG_SYMTAB_DECORATED</span></code>: Symbols will have a leading underscore in object files.</p></li>
</ul>
</div></blockquote>
<p>Additional configuration options may be required for the each enabled
binary format.</p>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="index.html" class="btn btn-neutral float-left" title="OS Components" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="drivers/index.html" class="btn btn-neutral float-right" title="Device Drivers" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>&#169; Copyright 2023, The Apache Software Foundation.</p>
</div>
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>