Debian Codegraph @

gcc/jit/jit-result.cc

raw
1 /* Internals of libgccjit: implementation of gcc_jit_result
2 Copyright (C) 2013-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <[email protected]>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_DLFCN_H
23 #include "system.h"
24 #include "coretypes.h"
25
26 #include "jit-common.h"
27 #include "jit-logging.h"
28 #include "jit-result.h"
29 #include "jit-tempdir.h"
30
31 #ifdef _WIN32
32 #include "jit-w32.h"
33 #endif
34
35 namespace gcc {
36 namespace jit {
37
38 /* Constructor for gcc::jit::result. */
39
40 result::
41 result(logger *logger, handle dso_handle, tempdir *tempdir_) :
42 log_user (logger),
43 m_dso_handle (dso_handle),
44 m_tempdir (tempdir_)
45 {
46 JIT_LOG_SCOPE (get_logger ());
47 }
48
49 /* gcc::jit::result's destructor.
50
51 Called implicitly by gcc_jit_result_release. */
52
53 result::~result()
54 {
55 JIT_LOG_SCOPE (get_logger ());
56
57 #ifdef _WIN32
58 FreeLibrary(m_dso_handle);
59 #else
60 dlclose (m_dso_handle);
61 #endif
62 /* Responsibility for cleaning up the tempdir (including "fake.so" within
63 the filesystem) might have been handed to us by the playback::context,
64 so that the cleanup can be delayed (see PR jit/64206).
65
66 If so, clean it up now. */
67 delete m_tempdir;
68 }
69
70 /* Attempt to locate the given function by name within the
71 playback::result, using dlsym.
72
73 Implements the post-error-checking part of
74 gcc_jit_result_get_code. */
75
76 void *
77 result::
78 get_code (const char *funcname)
79 {
80 JIT_LOG_SCOPE (get_logger ());
81
82 void *code;
83
84 #ifdef _WIN32
85 /* Clear any existing error. */
86 SetLastError(0);
87
88 code = (void *)GetProcAddress(m_dso_handle, funcname);
89 if (GetLastError() != 0) {
90 print_last_error ();
91 }
92 #else
93 const char *error;
94 /* Clear any existing error. */
95 dlerror ();
96
97 code = dlsym (m_dso_handle, funcname);
98
99 if ((error = dlerror()) != NULL) {
100 fprintf(stderr, "%s\n", error);
101 }
102 #endif
103
104 return code;
105 }
106
107 /* Attempt to locate the given global by name within the
108 playback::result, using dlsym.
109
110 Implements the post-error-checking part of
111 gcc_jit_result_get_global. */
112
113 void *
114 result::
115 get_global (const char *name)
116 {
117 JIT_LOG_SCOPE (get_logger ());
118
119 void *global;
120
121 #ifdef _WIN32
122 /* Clear any existing error. */
123 SetLastError(0);
124
125 global = (void *)GetProcAddress(m_dso_handle, name);
126 if (GetLastError() != 0) {
127 print_last_error ();
128 }
129 #else
130 const char *error;
131 /* Clear any existing error. */
132 dlerror ();
133
134 global = dlsym (m_dso_handle, name);
135
136 if ((error = dlerror()) != NULL) {
137 fprintf(stderr, "%s\n", error);
138 }
139 #endif
140
141 return global;
142 }
143
144 } // namespace gcc::jit
145
146 } // namespace gcc