mod_insmod.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /****************************************************************************
  2. * sched/module/mod_insmod.c
  3. *
  4. * Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdint.h>
  41. #include <string.h>
  42. #include <assert.h>
  43. #include <errno.h>
  44. #include <debug.h>
  45. #include <nuttx/arch.h>
  46. #include <nuttx/kmalloc.h>
  47. #include <nuttx/module.h>
  48. #include <nuttx/lib/modlib.h>
  49. #ifdef CONFIG_MODULE
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. #ifndef MIN
  54. # define MIN(a,b) (a < b ? a : b)
  55. #endif
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Name: mod_dumploadinfo
  61. ****************************************************************************/
  62. #ifdef CONFIG_DEBUG_BINFMT_INFO
  63. static void mod_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
  64. {
  65. int i;
  66. binfo("LOAD_INFO:\n");
  67. binfo(" textalloc: %08lx\n", (long)loadinfo->textalloc);
  68. binfo(" datastart: %08lx\n", (long)loadinfo->datastart);
  69. binfo(" textsize: %ld\n", (long)loadinfo->textsize);
  70. binfo(" datasize: %ld\n", (long)loadinfo->datasize);
  71. binfo(" filelen: %ld\n", (long)loadinfo->filelen);
  72. binfo(" filfd: %d\n", loadinfo->filfd);
  73. binfo(" symtabidx: %d\n", loadinfo->symtabidx);
  74. binfo(" strtabidx: %d\n", loadinfo->strtabidx);
  75. binfo("ELF Header:\n");
  76. binfo(" e_ident: %02x %02x %02x %02x\n",
  77. loadinfo->ehdr.e_ident[0], loadinfo->ehdr.e_ident[1],
  78. loadinfo->ehdr.e_ident[2], loadinfo->ehdr.e_ident[3]);
  79. binfo(" e_type: %04x\n", loadinfo->ehdr.e_type);
  80. binfo(" e_machine: %04x\n", loadinfo->ehdr.e_machine);
  81. binfo(" e_version: %08x\n", loadinfo->ehdr.e_version);
  82. binfo(" e_entry: %08lx\n", (long)loadinfo->ehdr.e_entry);
  83. binfo(" e_phoff: %d\n", loadinfo->ehdr.e_phoff);
  84. binfo(" e_shoff: %d\n", loadinfo->ehdr.e_shoff);
  85. binfo(" e_flags: %08x\n", loadinfo->ehdr.e_flags);
  86. binfo(" e_ehsize: %d\n", loadinfo->ehdr.e_ehsize);
  87. binfo(" e_phentsize: %d\n", loadinfo->ehdr.e_phentsize);
  88. binfo(" e_phnum: %d\n", loadinfo->ehdr.e_phnum);
  89. binfo(" e_shentsize: %d\n", loadinfo->ehdr.e_shentsize);
  90. binfo(" e_shnum: %d\n", loadinfo->ehdr.e_shnum);
  91. binfo(" e_shstrndx: %d\n", loadinfo->ehdr.e_shstrndx);
  92. if (loadinfo->shdr && loadinfo->ehdr.e_shnum > 0)
  93. {
  94. for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
  95. {
  96. FAR Elf32_Shdr *shdr = &loadinfo->shdr[i];
  97. binfo("Sections %d:\n", i);
  98. binfo(" sh_name: %08x\n", shdr->sh_name);
  99. binfo(" sh_type: %08x\n", shdr->sh_type);
  100. binfo(" sh_flags: %08x\n", shdr->sh_flags);
  101. binfo(" sh_addr: %08x\n", shdr->sh_addr);
  102. binfo(" sh_offset: %d\n", shdr->sh_offset);
  103. binfo(" sh_size: %d\n", shdr->sh_size);
  104. binfo(" sh_link: %d\n", shdr->sh_link);
  105. binfo(" sh_info: %d\n", shdr->sh_info);
  106. binfo(" sh_addralign: %d\n", shdr->sh_addralign);
  107. binfo(" sh_entsize: %d\n", shdr->sh_entsize);
  108. }
  109. }
  110. }
  111. #else
  112. # define mod_dumploadinfo(i)
  113. #endif
  114. /****************************************************************************
  115. * Name: mod_dumpinitializer
  116. ****************************************************************************/
  117. #ifdef CONFIG_MODLIB_DUMPBUFFER
  118. static void mod_dumpinitializer(mod_initializer_t initializer,
  119. FAR struct mod_loadinfo_s *loadinfo)
  120. {
  121. modlib_dumpbuffer("Initializer code", (FAR const uint8_t *)initializer,
  122. MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));
  123. }
  124. #else
  125. # define mod_dumpinitializer(b,l)
  126. #endif
  127. /****************************************************************************
  128. * Public Functions
  129. ****************************************************************************/
  130. /****************************************************************************
  131. * Name: insmod
  132. *
  133. * Description:
  134. * Verify that the file is an ELF module binary and, if so, load the
  135. * module into kernel memory and initialize it for use.
  136. *
  137. * NOTE: modlib_setsymtab() had to have been called in board-specific OS
  138. * logic prior to calling this function from application logic (perhaps via
  139. * boardctl(BOARDIOC_OS_SYMTAB). Otherwise, insmod will be unable to
  140. * resolve symbols in the OS module.
  141. *
  142. * Input Parameters:
  143. *
  144. * filename - Full path to the module binary to be loaded
  145. * modname - The name that can be used to refer to the module after
  146. * it has been loaded.
  147. *
  148. * Returned Value:
  149. * A non-NULL module handle that can be used on subsequent calls to other
  150. * module interfaces is returned on success. If insmod() was unable to
  151. * load the module insmod() will return a NULL handle and the errno
  152. * variable will be set appropriately.
  153. *
  154. ****************************************************************************/
  155. FAR void *insmod(FAR const char *filename, FAR const char *modname)
  156. {
  157. struct mod_loadinfo_s loadinfo;
  158. FAR struct module_s *modp;
  159. mod_initializer_t initializer;
  160. int ret;
  161. DEBUGASSERT(filename != NULL && modname != NULL);
  162. binfo("Loading file: %s\n", filename);
  163. /* Get exclusive access to the module registry */
  164. modlib_registry_lock();
  165. /* Check if this module is already installed */
  166. if (modlib_registry_find(modname) != NULL)
  167. {
  168. modlib_registry_unlock();
  169. ret = -EEXIST;
  170. goto errout_with_lock;
  171. }
  172. /* Initialize the ELF library to load the program binary. */
  173. ret = modlib_initialize(filename, &loadinfo);
  174. mod_dumploadinfo(&loadinfo);
  175. if (ret != 0)
  176. {
  177. berr("ERROR: Failed to initialize to load module: %d\n", ret);
  178. goto errout_with_lock;
  179. }
  180. /* Allocate a module registry entry to hold the module data */
  181. modp = (FAR struct module_s *)kmm_zalloc(sizeof(struct module_s));
  182. if (ret != 0)
  183. {
  184. binfo("Failed to initialize for load of ELF program: %d\n", ret);
  185. goto errout_with_loadinfo;
  186. }
  187. /* Save the module name in the registry entry */
  188. strncpy(modp->modname, modname, MODLIB_NAMEMAX);
  189. /* Load the program binary */
  190. ret = modlib_load(&loadinfo);
  191. mod_dumploadinfo(&loadinfo);
  192. if (ret != 0)
  193. {
  194. binfo("Failed to load ELF program binary: %d\n", ret);
  195. goto errout_with_registry_entry;
  196. }
  197. /* Bind the program to the kernel symbol table */
  198. ret = modlib_bind(modp, &loadinfo);
  199. if (ret != 0)
  200. {
  201. binfo("Failed to bind symbols program binary: %d\n", ret);
  202. goto errout_with_load;
  203. }
  204. /* Save the load information */
  205. modp->alloc = (FAR void *)loadinfo.textalloc;
  206. #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
  207. modp->textsize = loadinfo.textsize;
  208. modp->datasize = loadinfo.datasize;
  209. #endif
  210. /* Get the module initializer entry point */
  211. initializer = (mod_initializer_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
  212. #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
  213. modp->initializer = initializer;
  214. #endif
  215. mod_dumpinitializer(initializer, &loadinfo);
  216. /* Call the module initializer */
  217. ret = initializer(&modp->modinfo);
  218. if (ret < 0)
  219. {
  220. binfo("Failed to initialize the module: %d\n", ret);
  221. goto errout_with_load;
  222. }
  223. /* Add the new module entry to the registry */
  224. modlib_registry_add(modp);
  225. modlib_uninitialize(&loadinfo);
  226. modlib_registry_unlock();
  227. return (FAR void *)modp;
  228. errout_with_load:
  229. modlib_unload(&loadinfo);
  230. (void)modlib_undepend(modp);
  231. errout_with_registry_entry:
  232. kmm_free(modp);
  233. errout_with_loadinfo:
  234. modlib_uninitialize(&loadinfo);
  235. errout_with_lock:
  236. modlib_registry_unlock();
  237. set_errno(-ret);
  238. return NULL;
  239. }
  240. #endif /* CONFIG_MODULE */