libelf.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /****************************************************************************
  2. * binfmt/libelf/libelf.h
  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. #ifndef __BINFMT_LIBELF_LIBELF_H
  21. #define __BINFMT_LIBELF_LIBELF_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <sys/types.h>
  27. #include <nuttx/arch.h>
  28. #include <nuttx/binfmt/elf.h>
  29. /****************************************************************************
  30. * Public Function Prototypes
  31. ****************************************************************************/
  32. /****************************************************************************
  33. * Name: elf_verifyheader
  34. *
  35. * Description:
  36. * Given the header from a possible ELF executable, verify that it is
  37. * an ELF executable.
  38. *
  39. * Returned Value:
  40. * 0 (OK) is returned on success and a negated errno is returned on
  41. * failure.
  42. *
  43. ****************************************************************************/
  44. int elf_verifyheader(FAR const Elf_Ehdr *header);
  45. /****************************************************************************
  46. * Name: elf_read
  47. *
  48. * Description:
  49. * Read 'readsize' bytes from the object file at 'offset'. The data is
  50. * read into 'buffer.' If 'buffer' is part of the ELF address environment,
  51. * then the caller is responsible for assuring that that address
  52. * environment is in place before calling this function (i.e., that
  53. * elf_addrenv_select() has been called if CONFIG_ARCH_ADDRENV=y).
  54. *
  55. * Returned Value:
  56. * 0 (OK) is returned on success and a negated errno is returned on
  57. * failure.
  58. *
  59. ****************************************************************************/
  60. int elf_read(FAR struct elf_loadinfo_s *loadinfo, FAR uint8_t *buffer,
  61. size_t readsize, off_t offset);
  62. /****************************************************************************
  63. * Name: elf_loadshdrs
  64. *
  65. * Description:
  66. * Loads section headers into memory.
  67. *
  68. * Returned Value:
  69. * 0 (OK) is returned on success and a negated errno is returned on
  70. * failure.
  71. *
  72. ****************************************************************************/
  73. int elf_loadshdrs(FAR struct elf_loadinfo_s *loadinfo);
  74. /****************************************************************************
  75. * Name: elf_findsection
  76. *
  77. * Description:
  78. * A section by its name.
  79. *
  80. * Input Parameters:
  81. * loadinfo - Load state information
  82. * sectname - Name of the section to find
  83. *
  84. * Returned Value:
  85. * On success, the index to the section is returned; A negated errno value
  86. * is returned on failure.
  87. *
  88. ****************************************************************************/
  89. int elf_findsection(FAR struct elf_loadinfo_s *loadinfo,
  90. FAR const char *sectname);
  91. /****************************************************************************
  92. * Name: elf_findsymtab
  93. *
  94. * Description:
  95. * Find the symbol table section.
  96. *
  97. * Returned Value:
  98. * 0 (OK) is returned on success and a negated errno is returned on
  99. * failure.
  100. *
  101. ****************************************************************************/
  102. int elf_findsymtab(FAR struct elf_loadinfo_s *loadinfo);
  103. /****************************************************************************
  104. * Name: elf_readsym
  105. *
  106. * Description:
  107. * Read the ELF symbol structure at the specified index into memory.
  108. *
  109. * Input Parameters:
  110. * loadinfo - Load state information
  111. * index - Symbol table index
  112. * sym - Location to return the table entry
  113. *
  114. * Returned Value:
  115. * 0 (OK) is returned on success and a negated errno is returned on
  116. * failure.
  117. *
  118. ****************************************************************************/
  119. int elf_readsym(FAR struct elf_loadinfo_s *loadinfo, int index,
  120. FAR Elf_Sym *sym);
  121. /****************************************************************************
  122. * Name: elf_symvalue
  123. *
  124. * Description:
  125. * Get the value of a symbol. The updated value of the symbol is returned
  126. * in the st_value field of the symbol table entry.
  127. *
  128. * Input Parameters:
  129. * loadinfo - Load state information
  130. * sym - Symbol table entry (value might be undefined)
  131. * exports - The symbol table to use for resolving undefined symbols.
  132. * nexports - Number of symbols in the symbol table.
  133. *
  134. * Returned Value:
  135. * 0 (OK) is returned on success and a negated errno is returned on
  136. * failure.
  137. *
  138. * EINVAL - There is something inconsistent in the symbol table (should
  139. * only happen if the file is corrupted)
  140. * ENOSYS - Symbol lies in common
  141. * ESRCH - Symbol has no name
  142. * ENOENT - Symbol undefined and not provided via a symbol table
  143. *
  144. ****************************************************************************/
  145. int elf_symvalue(FAR struct elf_loadinfo_s *loadinfo, FAR Elf_Sym *sym,
  146. FAR const struct symtab_s *exports, int nexports);
  147. /****************************************************************************
  148. * Name: elf_freebuffers
  149. *
  150. * Description:
  151. * Release all working buffers.
  152. *
  153. * Returned Value:
  154. * 0 (OK) is returned on success and a negated errno is returned on
  155. * failure.
  156. *
  157. ****************************************************************************/
  158. int elf_freebuffers(FAR struct elf_loadinfo_s *loadinfo);
  159. /****************************************************************************
  160. * Name: elf_allocbuffer
  161. *
  162. * Description:
  163. * Perform the initial allocation of the I/O buffer, if it has not already
  164. * been allocated.
  165. *
  166. * Returned Value:
  167. * 0 (OK) is returned on success and a negated errno is returned on
  168. * failure.
  169. *
  170. ****************************************************************************/
  171. int elf_allocbuffer(FAR struct elf_loadinfo_s *loadinfo);
  172. /****************************************************************************
  173. * Name: elf_reallocbuffer
  174. *
  175. * Description:
  176. * Increase the size of I/O buffer by the specified buffer increment.
  177. *
  178. * Returned Value:
  179. * 0 (OK) is returned on success and a negated errno is returned on
  180. * failure.
  181. *
  182. ****************************************************************************/
  183. int elf_reallocbuffer(FAR struct elf_loadinfo_s *loadinfo, size_t increment);
  184. /****************************************************************************
  185. * Name: elf_findctors
  186. *
  187. * Description:
  188. * Find C++ static constructors.
  189. *
  190. * Input Parameters:
  191. * loadinfo - Load state information
  192. *
  193. * Returned Value:
  194. * 0 (OK) is returned on success and a negated errno is returned on
  195. * failure.
  196. *
  197. ****************************************************************************/
  198. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  199. int elf_loadctors(FAR struct elf_loadinfo_s *loadinfo);
  200. #endif
  201. /****************************************************************************
  202. * Name: elf_loaddtors
  203. *
  204. * Description:
  205. * Load pointers to static destructors into an in-memory array.
  206. *
  207. * Input Parameters:
  208. * loadinfo - Load state information
  209. *
  210. * Returned Value:
  211. * 0 (OK) is returned on success and a negated errno is returned on
  212. * failure.
  213. *
  214. ****************************************************************************/
  215. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  216. int elf_loaddtors(FAR struct elf_loadinfo_s *loadinfo);
  217. #endif
  218. /****************************************************************************
  219. * Name: elf_addrenv_alloc
  220. *
  221. * Description:
  222. * Allocate memory for the ELF image (textalloc and dataalloc).
  223. * If CONFIG_ARCH_ADDRENV=n, textalloc will be allocated using kmm_zalloc()
  224. * and dataalloc will be a offset from textalloc.
  225. * If CONFIG_ARCH_ADDRENV-y, then textalloc and dataalloc will be allocated
  226. * using up_addrenv_create().
  227. * In either case, there will be a unique instance of textalloc and
  228. * dataalloc (and stack) for each instance of a process.
  229. *
  230. * Input Parameters:
  231. * loadinfo - Load state information
  232. * textsize - The size (in bytes) of the .text address environment needed
  233. * for the ELF image (read/execute).
  234. * datasize - The size (in bytes) of the .bss/.data address environment
  235. * needed for the ELF image (read/write).
  236. * heapsize - The initial size (in bytes) of the heap address environment
  237. * needed by the task. This region may be read/write only.
  238. *
  239. * Returned Value:
  240. * Zero (OK) on success; a negated errno value on failure.
  241. *
  242. ****************************************************************************/
  243. int elf_addrenv_alloc(FAR struct elf_loadinfo_s *loadinfo, size_t textsize,
  244. size_t datasize, size_t heapsize);
  245. /****************************************************************************
  246. * Name: elf_addrenv_select
  247. *
  248. * Description:
  249. * Temporarily select the task's address environment.
  250. *
  251. * Input Parameters:
  252. * loadinfo - Load state information
  253. *
  254. * Returned Value:
  255. * Zero (OK) on success; a negated errno value on failure.
  256. *
  257. ****************************************************************************/
  258. #ifdef CONFIG_ARCH_ADDRENV
  259. # define elf_addrenv_select(l) up_addrenv_select(&(l)->addrenv, &(l)->oldenv)
  260. #endif
  261. /****************************************************************************
  262. * Name: elf_addrenv_restore
  263. *
  264. * Description:
  265. * Restore the address environment before elf_addrenv_select() was called..
  266. *
  267. * Input Parameters:
  268. * loadinfo - Load state information
  269. *
  270. * Returned Value:
  271. * Zero (OK) on success; a negated errno value on failure.
  272. *
  273. ****************************************************************************/
  274. #ifdef CONFIG_ARCH_ADDRENV
  275. # define elf_addrenv_restore(l) up_addrenv_restore(&(l)->oldenv)
  276. #endif
  277. /****************************************************************************
  278. * Name: elf_addrenv_free
  279. *
  280. * Description:
  281. * Release the address environment previously created by
  282. * elf_addrenv_alloc(). This function is called only under certain error
  283. * conditions after the module has been loaded but not yet started.
  284. * After the module has been started, the address environment will
  285. * automatically be freed when the module exits.
  286. *
  287. * Input Parameters:
  288. * loadinfo - Load state information
  289. *
  290. * Returned Value:
  291. * None.
  292. *
  293. ****************************************************************************/
  294. void elf_addrenv_free(FAR struct elf_loadinfo_s *loadinfo);
  295. #endif /* __BINFMT_LIBELF_LIBELF_H */