aboutsummaryrefslogtreecommitdiff
path: root/dlfcn/dlerror.h
blob: 999b9f26ae2fe82b0df41448c48623c3e5e4ec1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Memory management for dlerror messages.
   Copyright (C) 2021-2023 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#ifndef _DLERROR_H
#define _DLERROR_H

#include <dlfcn.h>
#include <ldsodefs.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

/* Source of the errstring member in struct dl_action_result, for
   finding the right deallocation routine.  */
enum dl_action_result_errstring_source
  {
   dl_action_result_errstring_constant, /* String literal, no deallocation.  */
   dl_action_result_errstring_rtld, /* libc in the primary namespace.  */
   dl_action_result_errstring_local, /* libc in the current namespace.  */
  };

struct dl_action_result
{
  int errcode;
  char errstring_source;
  bool returned;
  const char *objname;
  char *errstring;
};

/* Used to free the errstring member of struct dl_action_result in the
   dl_action_result_errstring_rtld case.  */
static inline void
dl_error_free (void *ptr)
{
#ifdef SHARED
  /* In the shared case, ld.so may use a different malloc than this
     namespace.  */
  GLRO (dl_error_free (ptr));
#else
  /* Call the implementation directly.  It still has to check for
     pointers which cannot be freed, so do not call free directly
     here.  */
  _dl_error_free (ptr);
#endif
}

/* Deallocate RESULT->errstring, leaving *RESULT itself allocated.  */
static inline void
dl_action_result_errstring_free (struct dl_action_result *result)
{
  switch (result->errstring_source)
    {
    case dl_action_result_errstring_constant:
      break;
    case dl_action_result_errstring_rtld:
      dl_error_free (result->errstring);
      break;
    case dl_action_result_errstring_local:
      free (result->errstring);
      break;
    }
}

/* Stand-in for an error result object whose allocation failed.  No
   precise message can be reported for this, but an error must still
   be signaled.  */
static struct dl_action_result *const dl_action_result_malloc_failed
  __attribute__ ((unused)) = (struct dl_action_result *) (intptr_t) -1;

/* Thread-local variable for storing dlfcn failures for subsequent
   reporting via dlerror.  */
extern __thread struct dl_action_result *__libc_dlerror_result
  attribute_tls_model_ie;
void __libc_dlerror_result_free (void) attribute_hidden;

#endif /* _DLERROR_H */