binfmt.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. .. _binfmt:
  2. =============
  3. Binary Loader
  4. =============
  5. The purpose of a *binary loader* is to load and
  6. execute modules in various *binary formats* that reside in a file
  7. system. Loading refers instantiating the binary module in some fashion,
  8. usually copy all or some of the binary module into memory and then
  9. linking the module with other components. In most architectures, it is
  10. the base FLASH code that is the primary component that the binary module
  11. must link with because that is where the RTOS and primary tasks reside.
  12. Program modules can then be executed after they have been loaded.
  13. **Binary Formats**. The binary loader provides generic support for
  14. different binary formats. It supports a *registration interface* that
  15. allows the number of support binary formats to be loaded at run time.
  16. Each binary format provides a common, interface for use by the binary
  17. loader. When asked to load a binary, the binary loader will query each
  18. registered binary format, providing it with the path of the binary
  19. object to be loaded. The binary loader will stop when first binary
  20. format the recognizes the binary object and successfully loads it or
  21. when all registered binary formats have attempt loading the binary
  22. object and failed.
  23. At present, the following binary formats are support by NuttX:
  24. - **ELF**. Standard ELF formatted files.
  25. - **NXFLAT**. NuttX NXFLAT formatted files. More information about the
  26. NXFLAT binary format can be found in the :ref:`NXFLAT
  27. documentation <nxflat>`.
  28. **Executables and Libraries** The generic binary loader logic does not
  29. care what it is that it being loaded. It could load an executable
  30. program or a library. There are no strict rules, but a library will tend
  31. to export symbols and a program will tend to import symbols: The program
  32. will use the symbols exported by the library. However, at this point in
  33. time, none of the supported binary formats support exporting of symbols.
  34. **binfmt**. In the NuttX source code, the short name ``binfmt`` is used
  35. to refer to the NuttX binary loader. This is the name of the directory
  36. containing the binary loader and the name of the header files and
  37. variables used by the binary loader.
  38. The name ``binfmt`` is the same name used by the Linux binary loader.
  39. However, the NuttX binary loader is an independent development and
  40. shares nothing with the Linux binary loader other the same name and the
  41. same basic functionality.
  42. Binary Loader Interface
  43. =======================
  44. Header Files
  45. ------------
  46. The interface to the binary loader is described in the header file
  47. ``include/nuttx/binfmt/binfmt.h``.
  48. A brief summary of the data structurs and interfaces prototyped in that
  49. header file are listed below.
  50. Data Structures
  51. ---------------
  52. When a binary format registers with the binary loader, it provides a
  53. pointer to a write-able instance of :c:struct:`binfmt_s`.
  54. .. c:struct:: binfmt_s
  55. .. code-block:: c
  56. struct binfmt_s
  57. {
  58. FAR struct binfmt_s *next; /* Supports a singly-linked list */
  59. int (*load)(FAR struct binary_s *bin); /* Verify and load binary into memory */
  60. };
  61. The ``load`` method is used to load the binary format into memory. It
  62. returns either ``OK`` (0) meaning that the binary object was loaded
  63. successfully, or a negated ``errno`` indicating why the object was not
  64. loaded.
  65. .. c:struct:: binary_s
  66. The type ``struct binary_s`` is use both to (1) describe the binary
  67. object to be loaded, and if successfully loaded, (2) to provide
  68. information about where and how the binary object was loaded. That
  69. structure is shown below:
  70. .. code-block:: c
  71. struct symtab_s;
  72. struct binary_s
  73. {
  74. /* Information provided to the loader to load and bind a module */
  75. FAR const char *filename; /* Full path to the binary to be loaded */
  76. FAR const char **argv; /* Argument list */
  77. FAR const struct symtab_s *exports; /* Table of exported symbols */
  78. int nexports; /* The number of symbols in exports[] */
  79. /* Information provided from the loader (if successful) describing the
  80. * resources used by the loaded module.
  81. */
  82. main_t entrypt; /* Entry point into a program module */
  83. FAR void *mapped; /* Memory-mapped, address space */
  84. FAR void *alloc[BINFMT_NALLOC]; /* Allocated address spaces */
  85. /* Constructors/destructors */
  86. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  87. FAR binfmt_ctor_t *ctors; /* Pointer to a list of constructors */
  88. FAR binfmt_dtor_t *dtors; /* Pointer to a list of destructors */
  89. uint16_t nctors; /* Number of constructors in the list */
  90. uint16_t ndtors; /* Number of destructors in the list */
  91. #endif
  92. /* Address environment.
  93. *
  94. * addrenv - This is the handle created by up_addrenv_create() that can be
  95. * used to manage the tasks address space.
  96. */
  97. #ifdef CONFIG_ARCH_ADDRENV
  98. group_addrenv_t addrenv; /* Task group address environment */
  99. #endif
  100. size_t mapsize; /* Size of the mapped address region (needed for munmap) */
  101. /* Start-up information that is provided by the loader, but may be modified
  102. * by the caller between load_module() and exec_module() calls.
  103. */
  104. uint8_t priority; /* Task execution priority */
  105. size_t stacksize; /* Size of the stack in bytes (unallocated) */
  106. };
  107. Where the types ``binfmt_ctor_t`` and ``binfmt_dtor_t`` define the type
  108. of one C++ constructor or destructor:
  109. .. code-block:: c
  110. typedef FAR void (*binfmt_ctor_t)(void);
  111. typedef FAR void (*binfmt_dtor_t)(void);
  112. Function Interfaces
  113. -------------------
  114. Binary format management
  115. ~~~~~~~~~~~~~~~~~~~~~~~~
  116. .. c:function:: int register_binfmt(FAR struct binfmt_s *binfmt)
  117. Register a loader for a binary format.
  118. :return: This is a NuttX internal function so it follows the convention
  119. that 0 (OK) is returned on success and a negated errno is returned on
  120. failure.
  121. .. c:function:: int unregister_binfmt(FAR struct binfmt_s *binfmt)
  122. Register a loader for a binary format.
  123. :return:
  124. This is a NuttX internal function so it follows the convention
  125. that 0 (OK) is returned on success and a negated errno is returned on
  126. failure.
  127. Basic module management
  128. ~~~~~~~~~~~~~~~~~~~~~~~
  129. .. c:function:: int load_module(FAR struct binary_s *bin)
  130. Load a module into memory, bind it to an exported symbol take,
  131. and prep the module for execution.
  132. :param bin:
  133. The ``filename`` field will be used
  134. in order to locate the module to be loaded from the file system.
  135. The filename must be the full, absolute path to the file to be executed
  136. unless ``CONFIG_LIB_ENVPATH`` is defined. In that case, filename may be
  137. a relative path; a set of candidate absolute paths will be generated using
  138. the ``PATH`` environment variable and ``load_module()`` will attempt to load each
  139. file that is found at those absolute paths.
  140. :return:
  141. This is a NuttX internal function so it follows the convention that 0 (``OK``)
  142. is returned on success and a negated ``errno`` is returned on failure.
  143. .. c:function:: int unload_module(FAR struct binary_s *bin)
  144. Unload a (non-executing) module from memory. If the module has been started
  145. (via :c:func:`exec_module`) and has not exited, calling this will be fatal.
  146. However, this function must be called after the module exist. How this is
  147. done is up to your logic. Perhaps you register it to be called by :c:func:`on_exit`?
  148. :return:
  149. This is a NuttX internal function so it follows the convention that 0 (``OK``)
  150. is returned on success and a negated ``errno`` is returned on failure.
  151. .. c:function:: int exec_module(FAR const struct binary_s *bin);
  152. Execute a module that has been loaded into memory by :c:func:`load_module`.
  153. :return:
  154. This is a NuttX internal function so it follows the convention that 0 (``OK``)
  155. is returned on success and a negated ``errno`` is returned on failure.
  156. .. tip::
  157. The function :c:func:`exec` is a convenience function that wraps
  158. :c:func:`load_module` and :c:func:`exec_module` into one call.
  159. ``PATH`` traversal logic
  160. ~~~~~~~~~~~~~~~~~~~~~~~~
  161. .. c:function:: ENVPATH_HANDLE envpath_init(void);
  162. Initialize for the traversal of each value in the ``PATH`` variable. The
  163. usage is sequence is as follows:
  164. #. Call :c:func:`envpath_init` to initialize for the traversal.
  165. ``envpath_init()`` will return an opaque handle that can then be
  166. provided to :c:func:`envpath_next` and :c:func:`envpath_release`.
  167. #. Call :c:func:`envpath_next` repeatedly to examine every file that lies in
  168. the directories of the ``PATH`` variable.
  169. #. Call :c:func:`envpath_release` to free resources set aside by
  170. :c:func:`envpath_init`.
  171. :return:
  172. On success, :c:func:`envpath_init` return a non-``NULL``, opaque handle
  173. that may subsequently be used in calls to :c:func:`envpath_next` and
  174. :c:func:`envpath_release`. On error, a ``NULL`` handle value will be returned.
  175. The most likely cause of an error would be that there is no value
  176. associated with the ``PATH`` variable.
  177. .. c:function:: FAR char *envpath_next(ENVPATH_HANDLE handle, FAR const char *relpath)
  178. Traverse all possible values in the PATH variable in attempt to find the
  179. full path to an executable file when only a relative path is provided.
  180. :param handle: The handle value returned by :c:func:`envpath_init`.
  181. :param relpath: The relative path to the file to be found.
  182. :return:
  183. On success, a non-``NULL`` pointer to a null-terminated string is provided.
  184. This is the full path to a file that exists in the file system.
  185. This function will verify that the file exists (but will not verify that it is marked executable).
  186. .. note::
  187. The string pointer return in the success case points to allocated memory.
  188. This memory must be freed by the called by calling :c:func:`kmm_free`.
  189. ``NULL`` relpath from any absolute path in the ``PATH`` variable.
  190. In this case, there is no point in calling :c:func:`envpath_next` further;
  191. :c:func:`envpath_release` must be called to release resources set aside by
  192. :c:func:`envpath_init`.
  193. .. c:function:: void envpath_release(ENVPATH_HANDLE handle)
  194. Release all resources set aside by envpath_init when the
  195. handle value was created. The handle value is invalid on
  196. return from this function. Attempts to all :c:func:`envpath_next`
  197. or :c:func:`envpath_release` with such a stale handle will result
  198. in undefined (i.e., not good) behavior.
  199. :param handle: The handle value returned by :c:func:`envpath_init`.
  200. Symbol Tables
  201. =============
  202. **Symbol Tables**. Symbol tables are lists of name value mappings: The
  203. name is a string that identifies a symbol, and the value is an address
  204. in memory where the symbol of that name has been positioned. In most
  205. NuttX architectures symbol tables are required, as a minimum, in order
  206. to dynamically link the loaded binary object with the base code on
  207. FLASH. Since the binary object was separately built and separately
  208. linked, these symbols will appear as *undefined* symbols in the binary
  209. object. The binary loader will use the symbol table to look up the
  210. symbol by its name and to provide the address associated with the symbol
  211. as needed to perform the dynamic linking of the binary object to the
  212. base FLASH code.
  213. Symbol Table Header Files
  214. -------------------------
  215. The interface to the symbol table logic is described in the header file
  216. ``include/nuttx/binfmt/symtab.h``.
  217. A brief summary of the data structurs and interfaces prototyped in that
  218. header file are listed below.
  219. Symbol Table Data Structures
  220. ----------------------------
  221. .. c:struct:: symbtab_s
  222. Describes one entry in the symbol table.
  223. .. code-block:: c
  224. struct symtab_s
  225. {
  226. FAR const char *sym_name; /* A pointer to the symbol name string */
  227. FAR const void *sym_value; /* The value associated with the string */
  228. };
  229. A symbol table is a fixed size array of ``struct symtab_s``. The
  230. information is intentionally minimal and supports only:
  231. #. Function pointers as ``sym_values``. Of other kinds of values need to
  232. be supported, then typing information would also need to be included
  233. in the structure.
  234. #. Fixed size arrays. There is no explicit provisional for dynamically
  235. adding or removing entries from the symbol table (realloc might be
  236. used for that purpose if needed). The intention is to support only
  237. fixed size arrays completely defined at compilation or link time.
  238. Symbol Table Function Interfaces
  239. --------------------------------
  240. .. c:function:: FAR const struct symtab_s *symtab_findbyname(FAR const struct symtab_s *symtab, FAR const char *name, int nsyms);
  241. Find the symbol in the symbol table with the matching name.
  242. This version assumes that table is not ordered with respect to
  243. symbol name and, hence, access time will be linear with respect
  244. to ``nsyms``.
  245. :return:
  246. 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.
  247. .. c:function:: FAR const struct symtab_s *symtab_findorderedbyname(FAR const struct symtab_s *symtab, FAR const char *name, int nsyms);
  248. Find the symbol in the symbol table with the matching name.
  249. This version assumes that table ordered with respect to symbol name.
  250. :return:
  251. A reference to the symbol table entry if an entry with
  252. the matching name is found; NULL is returned if the entry is not found.
  253. .. c:function:: FAR const struct symtab_s *symtab_findbyvalue(FAR const struct symtab_s *symtab, FAR void *value, int nsyms);
  254. Find the symbol in the symbol table whose value closest
  255. (but not greater than), the provided value. This version assumes
  256. that table is not ordered with respect to symbol name and, hence,
  257. access time will be linear with respect to ``nsyms``.
  258. :return:
  259. A reference to the symbol table entry if an entry with the matching
  260. name is found; ``NULL`` is returned if the entry is not found.
  261. Configuration Variables
  262. =======================
  263. - ``CONFIG_BINFMT_DISABLE``: By default, support for loadable binary formats is built.
  264. This logic may be suppressed be defining this setting.
  265. - ``CONFIG_BINFMT_CONSTRUCTORS``: Build in support for C++ constructors in loaded modules.
  266. - ``CONFIG_SYMTAB_ORDEREDBYNAME``: Symbol tables are order by name (rather than value).
  267. Additional configuration options may be required for the each enabled
  268. binary format.