aboutsummaryrefslogtreecommitdiff
path: root/gcc/jit/jit-result.cc
blob: e00f4d861d861f73ce15a2adfcd663dc3a7d4b79 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Internals of libgccjit: implementation of gcc_jit_result
   Copyright (C) 2013-2023 Free Software Foundation, Inc.
   Contributed by David Malcolm <dmalcolm@redhat.com>.

This file is part of GCC.

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

GCC 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"

#include "jit-common.h"
#include "jit-logging.h"
#include "jit-result.h"
#include "jit-tempdir.h"

#ifdef _WIN32
#include "jit-w32.h"
#endif

namespace gcc {
namespace jit {

/* Constructor for gcc::jit::result.  */

result::
result(logger *logger, handle dso_handle, tempdir *tempdir_) :
  log_user (logger),
  m_dso_handle (dso_handle),
  m_tempdir (tempdir_)
{
  JIT_LOG_SCOPE (get_logger ());
}

/* gcc::jit::result's destructor.

   Called implicitly by gcc_jit_result_release.  */

result::~result()
{
  JIT_LOG_SCOPE (get_logger ());

#ifdef _WIN32
  FreeLibrary(m_dso_handle);
#else
  dlclose (m_dso_handle);
#endif
  /* Responsibility for cleaning up the tempdir (including "fake.so" within
     the filesystem) might have been handed to us by the playback::context,
     so that the cleanup can be delayed (see PR jit/64206).

     If so, clean it up now.  */
  delete m_tempdir;
}

/* Attempt to locate the given function by name within the
   playback::result, using dlsym.

   Implements the post-error-checking part of
   gcc_jit_result_get_code.  */

void *
result::
get_code (const char *funcname)
{
  JIT_LOG_SCOPE (get_logger ());

  void *code;

#ifdef _WIN32
  /* Clear any existing error.  */
  SetLastError(0);

  code = (void *)GetProcAddress(m_dso_handle, funcname);
  if (GetLastError() != 0)  {
    print_last_error ();
  }
#else
  const char *error;
  /* Clear any existing error.  */
  dlerror ();

  code = dlsym (m_dso_handle, funcname);

  if ((error = dlerror()) != NULL)  {
    fprintf(stderr, "%s\n", error);
  }
#endif

  return code;
}

/* Attempt to locate the given global by name within the
   playback::result, using dlsym.

   Implements the post-error-checking part of
   gcc_jit_result_get_global.  */

void *
result::
get_global (const char *name)
{
  JIT_LOG_SCOPE (get_logger ());

  void *global;

#ifdef _WIN32
  /* Clear any existing error.  */
  SetLastError(0);

  global = (void *)GetProcAddress(m_dso_handle, name);
  if (GetLastError() != 0)  {
    print_last_error ();
  }
#else
  const char *error;
  /* Clear any existing error.  */
  dlerror ();

  global = dlsym (m_dso_handle, name);

  if ((error = dlerror()) != NULL)  {
    fprintf(stderr, "%s\n", error);
  }
#endif

  return global;
}

} // namespace gcc::jit

} // namespace gcc