dlerror.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Memory management for dlerror messages.
  2. Copyright (C) 2021-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _DLERROR_H
  16. #define _DLERROR_H
  17. #include <dlfcn.h>
  18. #include <ldsodefs.h>
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. /* Source of the errstring member in struct dl_action_result, for
  23. finding the right deallocation routine. */
  24. enum dl_action_result_errstring_source
  25. {
  26. dl_action_result_errstring_constant, /* String literal, no deallocation. */
  27. dl_action_result_errstring_rtld, /* libc in the primary namespace. */
  28. dl_action_result_errstring_local, /* libc in the current namespace. */
  29. };
  30. struct dl_action_result
  31. {
  32. int errcode;
  33. char errstring_source;
  34. bool returned;
  35. const char *objname;
  36. char *errstring;
  37. };
  38. /* Used to free the errstring member of struct dl_action_result in the
  39. dl_action_result_errstring_rtld case. */
  40. static inline void
  41. dl_error_free (void *ptr)
  42. {
  43. #ifdef SHARED
  44. /* In the shared case, ld.so may use a different malloc than this
  45. namespace. */
  46. GLRO (dl_error_free (ptr));
  47. #else
  48. /* Call the implementation directly. It still has to check for
  49. pointers which cannot be freed, so do not call free directly
  50. here. */
  51. _dl_error_free (ptr);
  52. #endif
  53. }
  54. /* Deallocate RESULT->errstring, leaving *RESULT itself allocated. */
  55. static inline void
  56. dl_action_result_errstring_free (struct dl_action_result *result)
  57. {
  58. switch (result->errstring_source)
  59. {
  60. case dl_action_result_errstring_constant:
  61. break;
  62. case dl_action_result_errstring_rtld:
  63. dl_error_free (result->errstring);
  64. break;
  65. case dl_action_result_errstring_local:
  66. free (result->errstring);
  67. break;
  68. }
  69. }
  70. /* Stand-in for an error result object whose allocation failed. No
  71. precise message can be reported for this, but an error must still
  72. be signaled. */
  73. static struct dl_action_result *const dl_action_result_malloc_failed
  74. __attribute__ ((unused)) = (struct dl_action_result *) (intptr_t) -1;
  75. /* Thread-local variable for storing dlfcn failures for subsequent
  76. reporting via dlerror. */
  77. extern __thread struct dl_action_result *__libc_dlerror_result
  78. attribute_tls_model_ie;
  79. #endif /* _DLERROR_H */