libelf_ctors.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /****************************************************************************
  2. * binfmt/libelf/libelf_ctors.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 <string.h>
  40. #include <errno.h>
  41. #include <assert.h>
  42. #include <debug.h>
  43. #include <nuttx/kmalloc.h>
  44. #include <nuttx/binfmt/elf.h>
  45. #include "libelf.h"
  46. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Private Types
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Private Constant Data
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Public Functions
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Name: elf_loadctors
  64. *
  65. * Description:
  66. * Load pointers to static constructors into an in-memory array.
  67. *
  68. * Input Parameters:
  69. * loadinfo - Load state information
  70. *
  71. * Returned Value:
  72. * 0 (OK) is returned on success and a negated errno is returned on
  73. * failure.
  74. *
  75. ****************************************************************************/
  76. int elf_loadctors(FAR struct elf_loadinfo_s *loadinfo)
  77. {
  78. FAR Elf_Shdr *shdr;
  79. size_t ctorsize;
  80. int ctoridx;
  81. int ret;
  82. int i;
  83. DEBUGASSERT(loadinfo->ctors == NULL);
  84. /* Allocate an I/O buffer if necessary. This buffer is used by
  85. * elf_sectname() to accumulate the variable length symbol name.
  86. */
  87. ret = elf_allocbuffer(loadinfo);
  88. if (ret < 0)
  89. {
  90. berr("elf_allocbuffer failed: %d\n", ret);
  91. return -ENOMEM;
  92. }
  93. /* Find the index to the section named ".ctors." NOTE: On old ABI system,
  94. * .ctors is the name of the section containing the list of constructors;
  95. * On newer systems, the similar section is called .init_array. It is
  96. * expected that the linker script will force the section name to be
  97. * ".ctors" in either case.
  98. */
  99. ctoridx = elf_findsection(loadinfo, ".ctors");
  100. if (ctoridx < 0)
  101. {
  102. /* This may not be a failure. -ENOENT indicates that the file has no
  103. * static constructor section.
  104. */
  105. binfo("elf_findsection .ctors section failed: %d\n", ctoridx);
  106. return ret == -ENOENT ? OK : ret;
  107. }
  108. /* Now we can get a pointer to the .ctor section in the section header
  109. * table.
  110. */
  111. shdr = &loadinfo->shdr[ctoridx];
  112. /* Get the size of the .ctor section and the number of constructors that
  113. * will need to be called.
  114. */
  115. ctorsize = shdr->sh_size;
  116. loadinfo->nctors = ctorsize / sizeof(binfmt_ctor_t);
  117. binfo("ctoridx=%d ctorsize=%d sizeof(binfmt_ctor_t)=%d nctors=%d\n",
  118. ctoridx, ctorsize, sizeof(binfmt_ctor_t), loadinfo->nctors);
  119. /* Check if there are any constructors. It is not an error if there
  120. * are none.
  121. */
  122. if (loadinfo->nctors > 0)
  123. {
  124. /* Check an assumption that we made above */
  125. DEBUGASSERT(shdr->sh_size == loadinfo->nctors * sizeof(binfmt_ctor_t));
  126. /* In the old ABI, the .ctors section is not allocated. In that case,
  127. * we need to allocate memory to hold the .ctors and then copy the
  128. * from the file into the allocated memory.
  129. *
  130. * SHF_ALLOC indicates that the section requires memory during
  131. * execution.
  132. */
  133. if ((shdr->sh_flags & SHF_ALLOC) == 0)
  134. {
  135. /* Allocate memory to hold a copy of the .ctor section */
  136. loadinfo->ctoralloc = (binfmt_ctor_t *)kumm_malloc(ctorsize);
  137. if (!loadinfo->ctoralloc)
  138. {
  139. berr("Failed to allocate memory for .ctors\n");
  140. return -ENOMEM;
  141. }
  142. loadinfo->ctors = (binfmt_ctor_t *)loadinfo->ctoralloc;
  143. /* Read the section header table into memory */
  144. ret = elf_read(loadinfo, (FAR uint8_t *)loadinfo->ctors, ctorsize,
  145. shdr->sh_offset);
  146. if (ret < 0)
  147. {
  148. berr("Failed to allocate .ctors: %d\n", ret);
  149. return ret;
  150. }
  151. /* Fix up all of the .ctor addresses. Since the addresses
  152. * do not lie in allocated memory, there will be no relocation
  153. * section for them.
  154. */
  155. for (i = 0; i < loadinfo->nctors; i++)
  156. {
  157. FAR uintptr_t *ptr = (uintptr_t *)
  158. ((FAR void *)(&loadinfo->ctors)[i]);
  159. binfo("ctor %d: %08lx + %08lx = %08lx\n",
  160. i, *ptr, (unsigned long)loadinfo->textalloc,
  161. (unsigned long)(*ptr + loadinfo->textalloc));
  162. *ptr += loadinfo->textalloc;
  163. }
  164. }
  165. else
  166. {
  167. /* Save the address of the .ctors (actually, .init_array) where
  168. * it was loaded into memory. Since the .ctors lie in allocated
  169. * memory, they will be relocated via the normal mechanism.
  170. */
  171. loadinfo->ctors = (binfmt_ctor_t *)shdr->sh_addr;
  172. }
  173. }
  174. return OK;
  175. }
  176. #endif /* CONFIG_BINFMT_CONSTRUCTORS */