NuttX Binary LoaderLast Updated: October 30, 2012 |
Table of Contents |
1.0 Introduction |
Binary Loaders. The purpose of a binary loader is to load and execute modules in various binary formats that reside in a file system. Loading refers instiating 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 thebase 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.
Binary Formats. The binary loader provides generic support for different binary formats. It supports a registration interface 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.
At present, the following binary formats are support by NuttX:
Executables and Libraries 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 formts support exporting of symbols.
binfmt.
In the NuttX source code, the short name binfmt
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.
The name binfmt
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.
2.0 Binary Loader Interface |
The interface to the binary loader is described in the header file
include/nuttx/binfmt/binfmt.h
.
A brief summary of the data structurs and interfaces prototyped in that header file are listed below.
When a binary format registers with the binary loader, it provides a pointer to a write-able instance of the following data structure:
struct binfmt_s { FAR struct binfmt_s *next; /* Supports a singly-linked list */ int (*load)(FAR struct binary_s *bin); /* Verify and load binary into memory */ };
The load
method is used to load the binary format into memory.
It returns either OK
(0) meaning that the binary object was loaded successfully, or a negater errno
indicating why the object was not loaded.
The type struct binary_s
is use both to (2) 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:
struct symtab_s; struct binary_s { /* Information provided to the loader to load and bind a module */ FAR const char *filename; /* Full path to the binary to be loaded */ FAR const char **argv; /* Argument list */ FAR const struct symtab_s *exports; /* Table of exported symbols */ int nexports; /* The number of symbols in exports[] */ /* Information provided from the loader (if successful) describing the * resources used by the loaded module. */ main_t entrypt; /* Entry point into a program module */ FAR void *mapped; /* Memory-mapped, address space */ FAR void *alloc[BINFMT_NALLOC]; /* Allocated address spaces */ #ifdef CONFIG_BINFMT_CONSTRUCTORS FAR binfmt_ctor_t *ctors; /* Pointer to a list of constructors */ FAR binfmt_dtor_t *dtors; /* Pointer to a list of destructors */ uint16_t nctors; /* Number of constructors in the list */ uint16_t ndtors; /* Number of destructors in the list */ #endif size_t mapsize; /* Size of the mapped address region (needed for munmap) */ size_t stacksize; /* Size of the stack in bytes (unallocated) */ };
Where the types binfmt_ctor_t
and binfmt_dtor_t
define the type of one C++ constructor or destructor:
typedef FAR void (*binfmt_ctor_t)(void); typedef FAR void (*binfmt_dtor_t)(void);
register_binfmt()
unregister_binfmt()
load_module()
unload_module()
exec_module()
exec()
exec()
register_binfmt()
Function Prototype:
#include <:nuttx/binfmt/binfmt.h> int register_binfmt(FAR struct binfmt_s *binfmt);
Description:
Returned Value:
OK
) is returned on success and a negated errno
is returned on failure.
unregister_binfmt()
Function Prototype:
#include <:nuttx/binfmt/binfmt.h> int unregister_binfmt(FAR struct binfmt_s *binfmt);
Description:
Returned Value:
OK
) is returned on success and a negated errno
is returned on failure.
load_module()
Function Prototype:
#include <:nuttx/binfmt/binfmt.h> int load_module(FAR struct binary_s *bin);
Description:
Returned Value:
OK
) on success.
On failure, it returns -1 (ERROR
) with errno
set appropriately.
unload_module()
Function Prototype:
#include <:nuttx/binfmt/binfmt.h> int unload_module(FAR const struct binary_s *bin);
Description:
Unload a (non-executing) module from memory.
If the module has been started (via exec_module()
) and has not exited, calling this will be fatal.
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 on_exit()
?
Returned Value:
OK
) is returned on success and a negated errno
is returned on failure.
exec_module()
Function Prototype:
#include <:nuttx/binfmt/binfmt.h> int exec_module(FAR const struct binary_s *bin, int priority);
Description:
load_module()
.
Returned Value:
OK
) on success.
On failure, it returns -1 (ERROR
) with errno
set appropriately.
exec()
Function Prototype:
#include <:nuttx/binfmt/binfmt.h> int exec(FAR const char *filename, FAR const char **argv, FAR const struct symtab_s *exports, int nexports);
Description:
load_
and exec_module()
into one call.
Input Parameters:
filename
: Fulll path to the binary to be loaded.argv
: Argument list.exports
: Table of exported symbols.exports
: The number of symbols in exports.Returned Value:
OK
) on success.
On failure, it returns -1 (ERROR
) with errno
set appropriately.
3.0 Symbol Tables |
Symbol Tables. 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 undefined 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.
The interface to the symbol table logic is described in the header file
include/nuttx/binfmt/symtab.h
.
A brief summary of the data structurs and interfaces prototyped in that header file are listed below.
struct symbtab_s
describes one entry in the symbol table.
struct symtab_s { FAR const char *sym_name; /* A pointer to the symbol name string */ FAR const void *sym_value; /* The value associated witht the string */ };A symbol table is a fixed size array of
struct symtab_s
.
The information is intentionally minimal and supports only:
sym_values
.
Of other kinds of values need to be supported, then typing information would also need to be included in the structure.
symtab_findbyname()
symtab_findorderedbyname()
symtab_findbyvalue()
symtab_findorderedbyvalue()
symtab_findbyname()
Function Prototype:
#include <:nuttx/binfmt/symtab.h> FAR const struct symtab_s * symtab_findbyname(FAR const struct symtab_s *symtab, FAR const char *name, int nsyms);
Description:
nsyms
.
Returned Value:
NULL
is returned if the entry is not found.
symtab_findorderedbyname()
Function Prototype:
#include <:nuttx/binfmt/symtab.h> FAR const struct symtab_s * symtab_findorderedbyname(FAR const struct symtab_s *symtab, FAR const char *name, int nsyms);
Description:
Returned Value:
NULL
is returned if the entry is not found.
symtab_findbyvalue()
Function Prototype:
#include <:nuttx/binfmt/symtab.h> FAR const struct symtab_s * symtab_findbyvalue(FAR const struct symtab_s *symtab, FAR void *value, int nsyms);
Description:
nsyms
.
Returned Value:
NULL
is returned if the entry is not found.
symtab_findorderedbyvalue()
Function Prototype:
#include <:nuttx/binfmt/symtab.h> FAR const struct symtab_s * symtab_findorderedbyvalue(FAR const struct symtab_s *symtab, FAR void *value, int nsyms);
Description:
Returned Value:
NULL
is returned if the entry is not found.
4.0 Configuration Variables |
CONFIG_BINFMT_DISABLE
:
By default, support for loadable binary formats is built.
This logic may be suppressed be defining this setting.
CONFIG_BINFMT_CONSTRUCTORS
:
Build in support for C++ constructors in loaded modules.
CONFIG_SYMTAB_ORDEREDBYNAME
:
Symbol tables are order by name (rather than value).
Additional configuration options may be required for the each enabled binary format.