libelf_load.c 9.6 KB

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