elf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /****************************************************************************
  2. * binfmt/elf.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <debug.h>
  28. #include <errno.h>
  29. #include <nuttx/arch.h>
  30. #include <nuttx/binfmt/binfmt.h>
  31. #include <nuttx/binfmt/elf.h>
  32. #include "libelf/libelf.h"
  33. #ifdef CONFIG_ELF
  34. /****************************************************************************
  35. * Pre-processor Definitions
  36. ****************************************************************************/
  37. /* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT
  38. * have to be defined or CONFIG_ELF_DUMPBUFFER does nothing.
  39. */
  40. #if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
  41. # undef CONFIG_ELF_DUMPBUFFER
  42. #endif
  43. #ifndef CONFIG_ELF_STACKSIZE
  44. # define CONFIG_ELF_STACKSIZE 2048
  45. #endif
  46. #ifdef CONFIG_ELF_DUMPBUFFER
  47. # define elf_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
  48. #else
  49. # define elf_dumpbuffer(m,b,n)
  50. #endif
  51. #ifndef MIN
  52. # define MIN(a,b) (a < b ? a : b)
  53. #endif
  54. /****************************************************************************
  55. * Private Function Prototypes
  56. ****************************************************************************/
  57. static int elf_loadbinary(FAR struct binary_s *binp);
  58. #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
  59. static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo);
  60. #endif
  61. /****************************************************************************
  62. * Private Data
  63. ****************************************************************************/
  64. static struct binfmt_s g_elfbinfmt =
  65. {
  66. NULL, /* next */
  67. elf_loadbinary, /* load */
  68. NULL, /* unload */
  69. };
  70. /****************************************************************************
  71. * Private Functions
  72. ****************************************************************************/
  73. /****************************************************************************
  74. * Name: elf_dumploadinfo
  75. ****************************************************************************/
  76. #if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_BINFMT)
  77. static void elf_dumploadinfo(FAR struct elf_loadinfo_s *loadinfo)
  78. {
  79. int i;
  80. binfo("LOAD_INFO:\n");
  81. binfo(" textalloc: %08lx\n", (long)loadinfo->textalloc);
  82. binfo(" dataalloc: %08lx\n", (long)loadinfo->dataalloc);
  83. binfo(" textsize: %ld\n", (long)loadinfo->textsize);
  84. binfo(" datasize: %ld\n", (long)loadinfo->datasize);
  85. binfo(" filelen: %ld\n", (long)loadinfo->filelen);
  86. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  87. binfo(" ctoralloc: %08lx\n", (long)loadinfo->ctoralloc);
  88. binfo(" ctors: %08lx\n", (long)loadinfo->ctors);
  89. binfo(" nctors: %d\n", loadinfo->nctors);
  90. binfo(" dtoralloc: %08lx\n", (long)loadinfo->dtoralloc);
  91. binfo(" dtors: %08lx\n", (long)loadinfo->dtors);
  92. binfo(" ndtors: %d\n", loadinfo->ndtors);
  93. #endif
  94. binfo(" filfd: %d\n", loadinfo->filfd);
  95. binfo(" symtabidx: %d\n", loadinfo->symtabidx);
  96. binfo(" strtabidx: %d\n", loadinfo->strtabidx);
  97. binfo("ELF Header:\n");
  98. binfo(" e_ident: %02x %02x %02x %02x\n",
  99. loadinfo->ehdr.e_ident[0], loadinfo->ehdr.e_ident[1],
  100. loadinfo->ehdr.e_ident[2], loadinfo->ehdr.e_ident[3]);
  101. binfo(" e_type: %04x\n", loadinfo->ehdr.e_type);
  102. binfo(" e_machine: %04x\n", loadinfo->ehdr.e_machine);
  103. binfo(" e_version: %08x\n", loadinfo->ehdr.e_version);
  104. binfo(" e_entry: %08lx\n", (long)loadinfo->ehdr.e_entry);
  105. binfo(" e_phoff: %d\n", loadinfo->ehdr.e_phoff);
  106. binfo(" e_shoff: %d\n", loadinfo->ehdr.e_shoff);
  107. binfo(" e_flags: %08x\n" , loadinfo->ehdr.e_flags);
  108. binfo(" e_ehsize: %d\n", loadinfo->ehdr.e_ehsize);
  109. binfo(" e_phentsize: %d\n", loadinfo->ehdr.e_phentsize);
  110. binfo(" e_phnum: %d\n", loadinfo->ehdr.e_phnum);
  111. binfo(" e_shentsize: %d\n", loadinfo->ehdr.e_shentsize);
  112. binfo(" e_shnum: %d\n", loadinfo->ehdr.e_shnum);
  113. binfo(" e_shstrndx: %d\n", loadinfo->ehdr.e_shstrndx);
  114. if (loadinfo->shdr && loadinfo->ehdr.e_shnum > 0)
  115. {
  116. for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
  117. {
  118. FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
  119. binfo("Sections %d:\n", i);
  120. binfo(" sh_name: %08x\n", shdr->sh_name);
  121. binfo(" sh_type: %08x\n", shdr->sh_type);
  122. binfo(" sh_flags: %08x\n", shdr->sh_flags);
  123. binfo(" sh_addr: %08x\n", shdr->sh_addr);
  124. binfo(" sh_offset: %d\n", shdr->sh_offset);
  125. binfo(" sh_size: %d\n", shdr->sh_size);
  126. binfo(" sh_link: %d\n", shdr->sh_link);
  127. binfo(" sh_info: %d\n", shdr->sh_info);
  128. binfo(" sh_addralign: %d\n", shdr->sh_addralign);
  129. binfo(" sh_entsize: %d\n", shdr->sh_entsize);
  130. }
  131. }
  132. }
  133. #else
  134. # define elf_dumploadinfo(i)
  135. #endif
  136. /****************************************************************************
  137. * Name: elf_dumpentrypt
  138. ****************************************************************************/
  139. #ifdef CONFIG_ELF_DUMPBUFFER
  140. static void elf_dumpentrypt(FAR struct binary_s *binp,
  141. FAR struct elf_loadinfo_s *loadinfo)
  142. {
  143. #ifdef CONFIG_ARCH_ADDRENV
  144. int ret;
  145. /* If CONFIG_ARCH_ADDRENV=y, then the loaded ELF lies in a virtual address
  146. * space that may not be in place now. elf_addrenv_select() will
  147. * temporarily instantiate that address space.
  148. */
  149. ret = elf_addrenv_select(loadinfo);
  150. if (ret < 0)
  151. {
  152. berr("ERROR: elf_addrenv_select() failed: %d\n", ret);
  153. return;
  154. }
  155. #endif
  156. elf_dumpbuffer("Entry code", (FAR const uint8_t *)binp->entrypt,
  157. MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));
  158. #ifdef CONFIG_ARCH_ADDRENV
  159. /* Restore the original address environment */
  160. ret = elf_addrenv_restore(loadinfo);
  161. if (ret < 0)
  162. {
  163. berr("ERROR: elf_addrenv_restore() failed: %d\n", ret);
  164. }
  165. #endif
  166. }
  167. #else
  168. # define elf_dumpentrypt(b,l)
  169. #endif
  170. /****************************************************************************
  171. * Name: elf_loadbinary
  172. *
  173. * Description:
  174. * Verify that the file is an ELF binary and, if so, load the ELF
  175. * binary into memory
  176. *
  177. ****************************************************************************/
  178. static int elf_loadbinary(FAR struct binary_s *binp)
  179. {
  180. struct elf_loadinfo_s loadinfo; /* Contains globals for libelf */
  181. int ret;
  182. binfo("Loading file: %s\n", binp->filename);
  183. /* Initialize the ELF library to load the program binary. */
  184. ret = elf_init(binp->filename, &loadinfo);
  185. elf_dumploadinfo(&loadinfo);
  186. if (ret != 0)
  187. {
  188. berr("Failed to initialize for load of ELF program: %d\n", ret);
  189. goto errout;
  190. }
  191. /* Load the program binary */
  192. ret = elf_load(&loadinfo);
  193. elf_dumploadinfo(&loadinfo);
  194. if (ret != 0)
  195. {
  196. berr("Failed to load ELF program binary: %d\n", ret);
  197. goto errout_with_init;
  198. }
  199. /* Bind the program to the exported symbol table */
  200. ret = elf_bind(&loadinfo, binp->exports, binp->nexports);
  201. if (ret != 0)
  202. {
  203. berr("Failed to bind symbols program binary: %d\n", ret);
  204. goto errout_with_load;
  205. }
  206. /* Return the load information */
  207. binp->entrypt = (main_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
  208. binp->stacksize = CONFIG_ELF_STACKSIZE;
  209. /* Add the ELF allocation to the alloc[] only if there is no address
  210. * environment. If there is an address environment, it will automatically
  211. * be freed when the function exits
  212. *
  213. * REVISIT: If the module is loaded then unloaded, wouldn't this cause
  214. * a memory leak?
  215. */
  216. #ifdef CONFIG_ARCH_ADDRENV
  217. /* Save the address environment in the binfmt structure. This will be
  218. * needed when the module is executed.
  219. */
  220. up_addrenv_clone(&loadinfo.addrenv, &binp->addrenv);
  221. #else
  222. binp->alloc[0] = (FAR void *)loadinfo.textalloc;
  223. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  224. binp->alloc[1] = loadinfo.ctoralloc;
  225. binp->alloc[2] = loadinfo.dtoralloc;
  226. #endif
  227. #endif
  228. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  229. /* Save information about constructors and destructors. */
  230. binp->ctors = loadinfo.ctors;
  231. binp->nctors = loadinfo.nctors;
  232. binp->dtors = loadinfo.dtors;
  233. binp->ndtors = loadinfo.ndtors;
  234. #endif
  235. elf_dumpentrypt(binp, &loadinfo);
  236. elf_uninit(&loadinfo);
  237. return OK;
  238. errout_with_load:
  239. elf_unload(&loadinfo);
  240. errout_with_init:
  241. elf_uninit(&loadinfo);
  242. errout:
  243. return ret;
  244. }
  245. /****************************************************************************
  246. * Public Functions
  247. ****************************************************************************/
  248. /****************************************************************************
  249. * Name: elf_initialize
  250. *
  251. * Description:
  252. * In order to use the ELF binary format, this function must be called
  253. * during system initialization to register the ELF binary format.
  254. *
  255. * Returned Value:
  256. * This is a NuttX internal function so it follows the convention that
  257. * 0 (OK) is returned on success and a negated errno is returned on
  258. * failure.
  259. *
  260. ****************************************************************************/
  261. int elf_initialize(void)
  262. {
  263. int ret;
  264. /* Register ourselves as a binfmt loader */
  265. binfo("Registering ELF\n");
  266. ret = register_binfmt(&g_elfbinfmt);
  267. if (ret != 0)
  268. {
  269. berr("Failed to register binfmt: %d\n", ret);
  270. }
  271. return ret;
  272. }
  273. /****************************************************************************
  274. * Name: elf_uninitialize
  275. *
  276. * Description:
  277. * Unregister the ELF binary loader
  278. *
  279. * Returned Value:
  280. * None
  281. *
  282. ****************************************************************************/
  283. void elf_uninitialize(void)
  284. {
  285. unregister_binfmt(&g_elfbinfmt);
  286. }
  287. #endif /* CONFIG_ELF */