modlib_load.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /****************************************************************************
  2. * libs/libc/modlib/modlib_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/lib/modlib.h>
  33. #include "libc.h"
  34. #include "modlib/modlib.h"
  35. /****************************************************************************
  36. * Pre-processor Definitions
  37. ****************************************************************************/
  38. #define ELF_ALIGN_MASK ((1 << CONFIG_MODLIB_ALIGN_LOG2) - 1)
  39. #define ELF_ALIGNUP(a) (((unsigned long)(a) + ELF_ALIGN_MASK) & ~ELF_ALIGN_MASK)
  40. #define ELF_ALIGNDOWN(a) ((unsigned long)(a) & ~ELF_ALIGN_MASK)
  41. #ifndef MAX
  42. # define MAX(x,y) ((x) > (y) ? (x) : (y))
  43. #endif
  44. #ifndef MIN
  45. # define MIN(x,y) ((x) < (y) ? (x) : (y))
  46. #endif
  47. /****************************************************************************
  48. * Private Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: modlib_elfsize
  52. *
  53. * Description:
  54. * Calculate total memory allocation for the ELF file.
  55. *
  56. * Returned Value:
  57. * 0 (OK) is returned on success and a negated errno is returned on
  58. * failure.
  59. *
  60. ****************************************************************************/
  61. static void modlib_elfsize(struct mod_loadinfo_s *loadinfo)
  62. {
  63. size_t textsize;
  64. size_t datasize;
  65. int i;
  66. /* Accumulate the size each section into memory that is marked SHF_ALLOC */
  67. textsize = 0;
  68. datasize = 0;
  69. for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
  70. {
  71. FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
  72. /* SHF_ALLOC indicates that the section requires memory during
  73. * execution.
  74. */
  75. if ((shdr->sh_flags & SHF_ALLOC) != 0)
  76. {
  77. /* SHF_WRITE indicates that the section address space is write-
  78. * able
  79. */
  80. if ((shdr->sh_flags & SHF_WRITE) != 0)
  81. {
  82. datasize += ELF_ALIGNUP(shdr->sh_size);
  83. }
  84. else
  85. {
  86. textsize += ELF_ALIGNUP(shdr->sh_size);
  87. }
  88. }
  89. }
  90. /* Save the allocation size */
  91. loadinfo->textsize = textsize;
  92. loadinfo->datasize = datasize;
  93. }
  94. /****************************************************************************
  95. * Name: modlib_loadfile
  96. *
  97. * Description:
  98. * Read the section data into memory. Section addresses in the shdr[] are
  99. * updated to point to the corresponding position in the memory.
  100. *
  101. * Returned Value:
  102. * 0 (OK) is returned on success and a negated errno is returned on
  103. * failure.
  104. *
  105. ****************************************************************************/
  106. static inline int modlib_loadfile(FAR struct mod_loadinfo_s *loadinfo)
  107. {
  108. FAR uint8_t *text;
  109. FAR uint8_t *data;
  110. FAR uint8_t **pptr;
  111. int ret;
  112. int i;
  113. /* Read each section into memory that is marked SHF_ALLOC + SHT_NOBITS */
  114. binfo("Loaded sections:\n");
  115. text = (FAR uint8_t *)loadinfo->textalloc;
  116. data = (FAR uint8_t *)loadinfo->datastart;
  117. for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
  118. {
  119. FAR Elf_Shdr *shdr = &loadinfo->shdr[i];
  120. /* SHF_ALLOC indicates that the section requires memory during
  121. * execution
  122. */
  123. if ((shdr->sh_flags & SHF_ALLOC) == 0)
  124. {
  125. continue;
  126. }
  127. /* SHF_WRITE indicates that the section address space is write-
  128. * able
  129. */
  130. if ((shdr->sh_flags & SHF_WRITE) != 0)
  131. {
  132. pptr = &data;
  133. }
  134. else
  135. {
  136. pptr = &text;
  137. }
  138. /* SHT_NOBITS indicates that there is no data in the file for the
  139. * section.
  140. */
  141. if (shdr->sh_type != SHT_NOBITS)
  142. {
  143. /* Read the section data from sh_offset to the memory region */
  144. ret = modlib_read(loadinfo, *pptr, shdr->sh_size, shdr->sh_offset);
  145. if (ret < 0)
  146. {
  147. berr("ERROR: Failed to read section %d: %d\n", i, ret);
  148. return ret;
  149. }
  150. }
  151. /* If there is no data in an allocated section, then the allocated
  152. * section must be cleared.
  153. */
  154. else
  155. {
  156. memset(*pptr, 0, shdr->sh_size);
  157. }
  158. /* Update sh_addr to point to copy in memory */
  159. binfo("%d. %08lx->%08lx\n", i,
  160. (unsigned long)shdr->sh_addr, (unsigned long)*pptr);
  161. shdr->sh_addr = (uintptr_t)*pptr;
  162. /* Setup the memory pointer for the next time through the loop */
  163. *pptr += ELF_ALIGNUP(shdr->sh_size);
  164. }
  165. return OK;
  166. }
  167. /****************************************************************************
  168. * Public Functions
  169. ****************************************************************************/
  170. /****************************************************************************
  171. * Name: modlib_load
  172. *
  173. * Description:
  174. * Loads the binary into memory, allocating memory, performing relocations
  175. * and initializing the data and bss segments.
  176. *
  177. * Returned Value:
  178. * 0 (OK) is returned on success and a negated errno is returned on
  179. * failure.
  180. *
  181. ****************************************************************************/
  182. int modlib_load(FAR struct mod_loadinfo_s *loadinfo)
  183. {
  184. int ret;
  185. binfo("loadinfo: %p\n", loadinfo);
  186. DEBUGASSERT(loadinfo && loadinfo->filfd >= 0);
  187. /* Load section headers into memory */
  188. ret = modlib_loadshdrs(loadinfo);
  189. if (ret < 0)
  190. {
  191. berr("ERROR: modlib_loadshdrs failed: %d\n", ret);
  192. goto errout_with_buffers;
  193. }
  194. /* Determine total size to allocate */
  195. modlib_elfsize(loadinfo);
  196. /* Allocate (and zero) memory for the ELF file. */
  197. /* Allocate memory to hold the ELF image */
  198. #if defined(CONFIG_ARCH_USE_MODULE_TEXT)
  199. if (loadinfo->textsize > 0)
  200. {
  201. loadinfo->textalloc = (uintptr_t)
  202. up_module_text_alloc(loadinfo->textsize);
  203. if (!loadinfo->textalloc)
  204. {
  205. berr("ERROR: Failed to allocate memory for the module text\n");
  206. ret = -ENOMEM;
  207. goto errout_with_buffers;
  208. }
  209. }
  210. if (loadinfo->datasize > 0)
  211. {
  212. loadinfo->datastart = (uintptr_t)lib_malloc(loadinfo->datasize);
  213. if (!loadinfo->datastart)
  214. {
  215. berr("ERROR: Failed to allocate memory for the module data\n");
  216. ret = -ENOMEM;
  217. goto errout_with_buffers;
  218. }
  219. }
  220. #else
  221. loadinfo->textalloc = (uintptr_t)lib_malloc(loadinfo->textsize +
  222. loadinfo->datasize);
  223. if (!loadinfo->textalloc)
  224. {
  225. berr("ERROR: Failed to allocate memory for the module\n");
  226. ret = -ENOMEM;
  227. goto errout_with_buffers;
  228. }
  229. loadinfo->datastart = loadinfo->textalloc + loadinfo->textsize;
  230. #endif
  231. /* Load ELF section data into memory */
  232. ret = modlib_loadfile(loadinfo);
  233. if (ret < 0)
  234. {
  235. berr("ERROR: modlib_loadfile failed: %d\n", ret);
  236. goto errout_with_buffers;
  237. }
  238. return OK;
  239. /* Error exits */
  240. errout_with_buffers:
  241. modlib_unload(loadinfo);
  242. return ret;
  243. }