binfmt_unloadmodule.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /****************************************************************************
  2. * binfmt/binfmt_unloadmodule.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/mman.h>
  25. #include <stdlib.h>
  26. #include <sched.h>
  27. #include <debug.h>
  28. #include <errno.h>
  29. #include <nuttx/kmalloc.h>
  30. #include <nuttx/binfmt/binfmt.h>
  31. #include "binfmt.h"
  32. #ifndef CONFIG_BINFMT_DISABLE
  33. /****************************************************************************
  34. * Private Functions
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Name: exec_dtors
  38. *
  39. * Description:
  40. * Execute C++ static destructors.
  41. *
  42. * Input Parameters:
  43. * binp - Load state information
  44. *
  45. * Returned Value:
  46. * 0 (OK) is returned on success and a negated errno is returned on
  47. * failure.
  48. *
  49. ****************************************************************************/
  50. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  51. static inline int exec_dtors(FAR struct binary_s *binp)
  52. {
  53. binfmt_dtor_t *dtor = binp->dtors;
  54. #ifdef CONFIG_ARCH_ADDRENV
  55. save_addrenv_t oldenv;
  56. int ret;
  57. #endif
  58. int i;
  59. /* Instantiate the address environment containing the destructors */
  60. #ifdef CONFIG_ARCH_ADDRENV
  61. ret = up_addrenv_select(&binp->addrenv, &oldenv);
  62. if (ret < 0)
  63. {
  64. berr("ERROR: up_addrenv_select() failed: %d\n", ret);
  65. return ret;
  66. }
  67. #endif
  68. /* Execute each destructor */
  69. for (i = 0; i < binp->ndtors; i++)
  70. {
  71. binfo("Calling dtor %d at %p\n", i, (FAR void *)dtor);
  72. (*dtor)();
  73. dtor++;
  74. }
  75. /* Restore the address environment */
  76. #ifdef CONFIG_ARCH_ADDRENV
  77. return up_addrenv_restore(&oldenv);
  78. #else
  79. return OK;
  80. #endif
  81. }
  82. #endif
  83. /****************************************************************************
  84. * Public Functions
  85. ****************************************************************************/
  86. /****************************************************************************
  87. * Name: unload_module
  88. *
  89. * Description:
  90. * Unload a (non-executing) module from memory. If the module has
  91. * been started (via exec_module) and has not exited, calling this will
  92. * be fatal.
  93. *
  94. * However, this function must be called after the module exits. How
  95. * this is done is up to your logic. Perhaps you register it to be
  96. * called by on_exit()?
  97. *
  98. * Returned Value:
  99. * This is a NuttX internal function so it follows the convention that
  100. * 0 (OK) is returned on success and a negated errno is returned on
  101. * failure.
  102. *
  103. ****************************************************************************/
  104. int unload_module(FAR struct binary_s *binp)
  105. {
  106. int ret;
  107. int i;
  108. if (binp)
  109. {
  110. /* Perform any format-specific unload operations */
  111. if (binp->unload)
  112. {
  113. ret = binp->unload(binp);
  114. if (ret < 0)
  115. {
  116. berr("binp->unload() failed: %d\n", ret);
  117. return ret;
  118. }
  119. }
  120. #ifdef CONFIG_BINFMT_CONSTRUCTORS
  121. /* Execute C++ destructors */
  122. ret = exec_dtors(binp);
  123. if (ret < 0)
  124. {
  125. berr("exec_ctors() failed: %d\n", ret);
  126. return ret;
  127. }
  128. #endif
  129. /* Unmap mapped address spaces */
  130. if (binp->mapped)
  131. {
  132. binfo("Unmapping address space: %p\n", binp->mapped);
  133. file_munmap(binp->mapped, binp->mapsize);
  134. }
  135. /* Free allocated address spaces */
  136. for (i = 0; i < BINFMT_NALLOC; i++)
  137. {
  138. if (binp->alloc[i])
  139. {
  140. binfo("Freeing alloc[%d]: %p\n", i, binp->alloc[i]);
  141. #if defined(CONFIG_ARCH_USE_TEXT_HEAP)
  142. up_textheap_free((FAR void *)binp->alloc[i]);
  143. #else
  144. kumm_free((FAR void *)binp->alloc[i]);
  145. #endif
  146. }
  147. }
  148. /* Notice that the address environment is not destroyed. This should
  149. * happen automatically when the task exits.
  150. */
  151. }
  152. return OK;
  153. }
  154. #endif /* CONFIG_BINFMT_DISABLE */