libelf_load.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /****************************************************************************
  2. * binfmt/libelf/libelf_load.c
  3. *
  4. * Copyright (C) 2012, 2020 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 <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. #include <assert.h>
  45. #include <errno.h>
  46. #include <debug.h>
  47. #include <nuttx/arch.h>
  48. #include <nuttx/addrenv.h>
  49. #include <nuttx/elf.h>
  50. #include <nuttx/mm/mm.h>
  51. #include <nuttx/binfmt/elf.h>
  52. #include "libelf.h"
  53. /****************************************************************************
  54. * Pre-processor Definitions
  55. ****************************************************************************/
  56. #define ELF_ALIGN_MASK ((1 << CONFIG_ELF_ALIGN_LOG2) - 1)
  57. #define ELF_ALIGNUP(a) (((unsigned long)(a) + ELF_ALIGN_MASK) & ~ELF_ALIGN_MASK)
  58. #define ELF_ALIGNDOWN(a) ((unsigned long)(a) & ~ELF_ALIGN_MASK)
  59. #ifndef MAX
  60. # define MAX(x,y) ((x) > (y) ? (x) : (y))
  61. #endif
  62. #ifndef MIN
  63. # define MIN(x,y) ((x) < (y) ? (x) : (y))
  64. #endif
  65. /****************************************************************************
  66. * Private Constant Data
  67. ****************************************************************************/
  68. /****************************************************************************
  69. * Private Functions
  70. ****************************************************************************/
  71. /****************************************************************************
  72. * Name: elf_elfsize
  73. *
  74. * Description:
  75. * Calculate total memory allocation for the ELF file.
  76. *
  77. * Returned Value:
  78. * 0 (OK) is returned on success and a negated errno is returned on
  79. * failure.
  80. *
  81. ****************************************************************************/
  82. static void elf_elfsize(struct elf_loadinfo_s *loadinfo)
  83. {
  84. size_t textsize;
  85. size_t datasize;
  86. int i;
  87. /* Accumulate the size each section into memory that is marked SHF_ALLOC */
  88. textsize = 0;
  89. datasize = 0;
  90. for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
  91. {
  92. FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
  93. /* SHF_ALLOC indicates that the section requires memory during
  94. * execution.
  95. */
  96. if ((shdr->sh_flags & SHF_ALLOC) != 0)
  97. {
  98. /* SHF_WRITE indicates that the section address space is write-
  99. * able
  100. */
  101. if ((shdr->sh_flags & SHF_WRITE) != 0)
  102. {
  103. datasize += ELF_ALIGNUP(shdr->sh_size);
  104. }
  105. else
  106. {
  107. textsize += ELF_ALIGNUP(shdr->sh_size);
  108. }
  109. }
  110. }
  111. /* Save the allocation size */
  112. loadinfo->textsize = textsize;
  113. loadinfo->datasize = datasize;
  114. }
  115. /****************************************************************************
  116. * Name: elf_loadfile
  117. *
  118. * Description:
  119. * Read the section data into memory. Section addresses in the shdr[] are
  120. * updated to point to the corresponding position in the memory.
  121. *
  122. * Returned Value:
  123. * 0 (OK) is returned on success and a negated errno is returned on
  124. * failure.
  125. *
  126. ****************************************************************************/
  127. static inline int elf_loadfile(FAR struct elf_loadinfo_s *loadinfo)
  128. {
  129. FAR uint8_t *text;
  130. FAR uint8_t *data;
  131. FAR uint8_t **pptr;
  132. int ret;
  133. int i;
  134. /* Read each section into memory that is marked SHF_ALLOC + SHT_NOBITS */
  135. binfo("Loaded sections:\n");
  136. text = (FAR uint8_t *)loadinfo->textalloc;
  137. data = (FAR uint8_t *)loadinfo->dataalloc;
  138. for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
  139. {
  140. FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
  141. /* SHF_ALLOC indicates that the section requires memory during
  142. * execution.
  143. */
  144. if ((shdr->sh_flags & SHF_ALLOC) == 0)
  145. {
  146. continue;
  147. }
  148. /* SHF_WRITE indicates that the section address space is write-
  149. * able
  150. */
  151. if ((shdr->sh_flags & SHF_WRITE) != 0)
  152. {
  153. pptr = &data;
  154. }
  155. else
  156. {
  157. pptr = &text;
  158. }
  159. /* SHT_NOBITS indicates that there is no data in the file for the
  160. * section.
  161. */
  162. if (shdr->sh_type != SHT_NOBITS)
  163. {
  164. /* Read the section data from sh_offset to the memory region */
  165. ret = elf_read(loadinfo, *pptr, shdr->sh_size, shdr->sh_offset);
  166. if (ret < 0)
  167. {
  168. berr("ERROR: Failed to read section %d: %d\n", i, ret);
  169. return ret;
  170. }
  171. }
  172. /* If there is no data in an allocated section, then the allocated
  173. * section must be cleared.
  174. */
  175. else
  176. {
  177. memset(*pptr, 0, shdr->sh_size);
  178. }
  179. /* Update sh_addr to point to copy in memory */
  180. binfo("%d. %08lx->%08lx\n", i,
  181. (unsigned long)shdr->sh_addr, (unsigned long)*pptr);
  182. shdr->sh_addr = (uintptr_t)*pptr;
  183. /* Setup the memory pointer for the next time through the loop */
  184. *pptr += ELF_ALIGNUP(shdr->sh_size);
  185. }
  186. return OK;
  187. }
  188. /****************************************************************************
  189. * Public Functions
  190. ****************************************************************************/
  191. /****************************************************************************
  192. * Name: elf_load
  193. *
  194. * Description:
  195. * Loads the binary into memory, allocating memory, performing relocations
  196. * and initializing the data and bss segments.
  197. *
  198. * Returned Value:
  199. * 0 (OK) is returned on success and a negated errno is returned on
  200. * failure.
  201. *
  202. ****************************************************************************/
  203. int elf_load(FAR struct elf_loadinfo_s *loadinfo)
  204. {
  205. size_t heapsize;
  206. #ifdef CONFIG_ELF_EXIDX_SECTNAME
  207. int exidx;
  208. #endif
  209. int ret;
  210. binfo("loadinfo: %p\n", loadinfo);
  211. DEBUGASSERT(loadinfo && loadinfo->filfd >= 0);
  212. /* Load section headers into memory */
  213. ret = elf_loadshdrs(loadinfo);
  214. if (ret < 0)
  215. {
  216. berr("ERROR: elf_loadshdrs failed: %d\n", ret);
  217. goto errout_with_buffers;
  218. }
  219. /* Determine total size to allocate */
  220. elf_elfsize(loadinfo);
  221. /* Determine the heapsize to allocate. heapsize is ignored if there is
  222. * no address environment because the heap is a shared resource in that
  223. * case. If there is no dynamic stack then heapsize must at least as big
  224. * as the fixed stack size since the stack will be allocated from the heap
  225. * in that case.
  226. */
  227. #if !defined(CONFIG_ARCH_ADDRENV)
  228. heapsize = 0;
  229. #elif defined(CONFIG_ARCH_STACK_DYNAMIC)
  230. heapsize = ARCH_HEAP_SIZE;
  231. #else
  232. heapsize = MAX(ARCH_HEAP_SIZE, CONFIG_ELF_STACKSIZE);
  233. #endif
  234. /* Allocate (and zero) memory for the ELF file. */
  235. ret = elf_addrenv_alloc(loadinfo, loadinfo->textsize, loadinfo->datasize,
  236. heapsize);
  237. if (ret < 0)
  238. {
  239. berr("ERROR: elf_addrenv_alloc() failed: %d\n", ret);
  240. goto errout_with_buffers;
  241. }
  242. #ifdef CONFIG_ARCH_ADDRENV
  243. /* If CONFIG_ARCH_ADDRENV=y, then the loaded ELF lies in a virtual address
  244. * space that may not be in place now. elf_addrenv_select() will
  245. * temporarily instantiate that address space.
  246. */
  247. ret = elf_addrenv_select(loadinfo);
  248. if (ret < 0)
  249. {
  250. berr("ERROR: elf_addrenv_select() failed: %d\n", ret);
  251. goto errout_with_buffers;
  252. }
  253. #ifdef CONFIG_BUILD_KERNEL
  254. /* Initialize the user heap */
  255. umm_initialize((FAR void *)CONFIG_ARCH_HEAP_VBASE,
  256. up_addrenv_heapsize(&loadinfo->addrenv));
  257. #endif
  258. #endif
  259. /* Load ELF section data into memory */
  260. ret = elf_loadfile(loadinfo);
  261. if (ret < 0)
  262. {
  263. berr("ERROR: elf_loadfile failed: %d\n", ret);
  264. goto errout_with_addrenv;
  265. }
  266. /* Load static constructors and destructors. */
  267. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  268. ret = elf_loadctors(loadinfo);
  269. if (ret < 0)
  270. {
  271. berr("ERROR: elf_loadctors failed: %d\n", ret);
  272. goto errout_with_addrenv;
  273. }
  274. ret = elf_loaddtors(loadinfo);
  275. if (ret < 0)
  276. {
  277. berr("ERROR: elf_loaddtors failed: %d\n", ret);
  278. goto errout_with_addrenv;
  279. }
  280. #endif
  281. #ifdef CONFIG_ELF_EXIDX_SECTNAME
  282. exidx = elf_findsection(loadinfo, CONFIG_ELF_EXIDX_SECTNAME);
  283. if (exidx < 0)
  284. {
  285. binfo("elf_findsection: Exception Index section not found: %d\n",
  286. exidx);
  287. }
  288. else
  289. {
  290. up_init_exidx(loadinfo->shdr[exidx].sh_addr,
  291. loadinfo->shdr[exidx].sh_size);
  292. }
  293. #endif
  294. #ifdef CONFIG_ARCH_ADDRENV
  295. /* Restore the original address environment */
  296. ret = elf_addrenv_restore(loadinfo);
  297. if (ret < 0)
  298. {
  299. berr("ERROR: elf_addrenv_restore() failed: %d\n", ret);
  300. goto errout_with_buffers;
  301. }
  302. #endif
  303. return OK;
  304. /* Error exits */
  305. errout_with_addrenv:
  306. #ifdef CONFIG_ARCH_ADDRENV
  307. elf_addrenv_restore(loadinfo);
  308. #endif
  309. errout_with_buffers:
  310. elf_unload(loadinfo);
  311. return ret;
  312. }