libelf_sections.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /****************************************************************************
  2. * binfmt/libelf/libelf_sections.c
  3. *
  4. * Copyright (C) 2012 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 <stdlib.h>
  40. #include <string.h>
  41. #include <assert.h>
  42. #include <errno.h>
  43. #include <debug.h>
  44. #include <nuttx/kmalloc.h>
  45. #include <nuttx/binfmt/elf.h>
  46. #include "libelf.h"
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Private Constant Data
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Private Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: elf_sectname
  58. *
  59. * Description:
  60. * Get the symbol name in loadinfo->iobuffer[].
  61. *
  62. * Returned Value:
  63. * 0 (OK) is returned on success and a negated errno is returned on
  64. * failure.
  65. *
  66. ****************************************************************************/
  67. static inline int elf_sectname(FAR struct elf_loadinfo_s *loadinfo,
  68. FAR const Elf_Shdr *shdr)
  69. {
  70. FAR Elf_Shdr *shstr;
  71. FAR uint8_t *buffer;
  72. off_t offset;
  73. size_t readlen;
  74. size_t bytesread;
  75. int shstrndx;
  76. int ret;
  77. /* Get the section header table index of the entry associated with the
  78. * section name string table. If the file has no section name string table,
  79. * this member holds the value SH_UNDEF.
  80. */
  81. shstrndx = loadinfo->ehdr.e_shstrndx;
  82. if (shstrndx == SHN_UNDEF)
  83. {
  84. berr("No section header string table\n");
  85. return -EINVAL;
  86. }
  87. /* Get the section name string table section header */
  88. shstr = &loadinfo->shdr[shstrndx];
  89. /* Get the file offset to the string that is the name of the section. This
  90. * is the sum of:
  91. *
  92. * shstr->sh_offset: The file offset to the first byte of the section
  93. * header string table data.
  94. * shdr->sh_name: The offset to the name of the section in the section
  95. * name table
  96. */
  97. offset = shstr->sh_offset + shdr->sh_name;
  98. /* Loop until we get the entire section name into memory */
  99. bytesread = 0;
  100. for (; ; )
  101. {
  102. /* Get the number of bytes to read */
  103. readlen = loadinfo->buflen - bytesread;
  104. if (offset + readlen > loadinfo->filelen)
  105. {
  106. if (loadinfo->filelen <= offset)
  107. {
  108. berr("At end of file\n");
  109. return -EINVAL;
  110. }
  111. readlen = loadinfo->filelen - offset;
  112. }
  113. /* Read that number of bytes into the array */
  114. buffer = &loadinfo->iobuffer[bytesread];
  115. ret = elf_read(loadinfo, buffer, readlen, offset + bytesread);
  116. if (ret < 0)
  117. {
  118. berr("Failed to read section name\n");
  119. return ret;
  120. }
  121. bytesread += readlen;
  122. /* Did we read the NUL terminator? */
  123. if (memchr(buffer, '\0', readlen) != NULL)
  124. {
  125. /* Yes, the buffer contains a NUL terminator. */
  126. return OK;
  127. }
  128. /* No.. then we have to read more */
  129. ret = elf_reallocbuffer(loadinfo, CONFIG_ELF_BUFFERINCR);
  130. if (ret < 0)
  131. {
  132. berr("elf_reallocbuffer failed: %d\n", ret);
  133. return ret;
  134. }
  135. }
  136. /* We will not get here */
  137. return OK;
  138. }
  139. /****************************************************************************
  140. * Public Functions
  141. ****************************************************************************/
  142. /****************************************************************************
  143. * Name: elf_loadshdrs
  144. *
  145. * Description:
  146. * Loads section headers into memory.
  147. *
  148. * Returned Value:
  149. * 0 (OK) is returned on success and a negated errno is returned on
  150. * failure.
  151. *
  152. ****************************************************************************/
  153. int elf_loadshdrs(FAR struct elf_loadinfo_s *loadinfo)
  154. {
  155. size_t shdrsize;
  156. int ret;
  157. DEBUGASSERT(loadinfo->shdr == NULL);
  158. /* Verify that there are sections */
  159. if (loadinfo->ehdr.e_shnum < 1)
  160. {
  161. berr("No sections(?)\n");
  162. return -EINVAL;
  163. }
  164. /* Get the total size of the section header table */
  165. shdrsize = (size_t)loadinfo->ehdr.e_shentsize *
  166. (size_t)loadinfo->ehdr.e_shnum;
  167. if (loadinfo->ehdr.e_shoff + shdrsize > loadinfo->filelen)
  168. {
  169. berr("Insufficient space in file for section header table\n");
  170. return -ESPIPE;
  171. }
  172. /* Allocate memory to hold a working copy of the sector header table */
  173. loadinfo->shdr = (FAR FAR Elf_Shdr *)kmm_malloc(shdrsize);
  174. if (!loadinfo->shdr)
  175. {
  176. berr("Failed to allocate the section header table. Size: %ld\n",
  177. (long)shdrsize);
  178. return -ENOMEM;
  179. }
  180. /* Read the section header table into memory */
  181. ret = elf_read(loadinfo, (FAR uint8_t *)loadinfo->shdr, shdrsize,
  182. loadinfo->ehdr.e_shoff);
  183. if (ret < 0)
  184. {
  185. berr("Failed to read section header table: %d\n", ret);
  186. }
  187. return ret;
  188. }
  189. /****************************************************************************
  190. * Name: elf_findsection
  191. *
  192. * Description:
  193. * A section by its name.
  194. *
  195. * Input Parameters:
  196. * loadinfo - Load state information
  197. * sectname - Name of the section to find
  198. *
  199. * Returned Value:
  200. * On success, the index to the section is returned; A negated errno value
  201. * is returned on failure.
  202. *
  203. ****************************************************************************/
  204. int elf_findsection(FAR struct elf_loadinfo_s *loadinfo,
  205. FAR const char *sectname)
  206. {
  207. FAR const Elf_Shdr *shdr;
  208. int ret;
  209. int i;
  210. /* Search through the shdr[] array in loadinfo for a section named
  211. * 'sectname'
  212. */
  213. for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
  214. {
  215. /* Get the name of this section */
  216. shdr = &loadinfo->shdr[i];
  217. ret = elf_sectname(loadinfo, shdr);
  218. if (ret < 0)
  219. {
  220. berr("elf_sectname failed: %d\n", ret);
  221. return ret;
  222. }
  223. /* Check if the name of this section is 'sectname' */
  224. binfo("%d. Comparing \"%s\" and .\"%s\"\n",
  225. i, loadinfo->iobuffer, sectname);
  226. if (strcmp((FAR const char *)loadinfo->iobuffer, sectname) == 0)
  227. {
  228. /* We found it... return the index */
  229. return i;
  230. }
  231. }
  232. /* We failed to find a section with this name. */
  233. return -ENOENT;
  234. }