| 1 |
/* Xtensa configuration settings loader. |
| 2 |
Copyright (C) 2022-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 3, or (at your option) any later |
| 9 |
version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#include "system.h" |
| 22 |
#include "coretypes.h" |
| 23 |
#include "diagnostic.h" |
| 24 |
#include "intl.h" |
| 25 |
#define XTENSA_CONFIG_DEFINITION |
| 26 |
#include "xtensa-config.h" |
| 27 |
#include "xtensa-dynconfig.h" |
| 28 |
|
| 29 |
#if defined (HAVE_DLFCN_H) |
| 30 |
#include <dlfcn.h> |
| 31 |
#elif defined (_WIN32) |
| 32 |
#define WIN32_LEAN_AND_MEAN |
| 33 |
#include <windows.h> |
| 34 |
#define ENABLE_PLUGIN |
| 35 |
#endif |
| 36 |
|
| 37 |
#if !defined (HAVE_DLFCN_H) && defined (_WIN32) |
| 38 |
|
| 39 |
#define RTLD_LAZY 0 /* Dummy value. */ |
| 40 |
|
| 41 |
static void * |
| 42 |
dlopen (const char *file, int mode ATTRIBUTE_UNUSED) |
| 43 |
{ |
| 44 |
return LoadLibrary (file); |
| 45 |
} |
| 46 |
|
| 47 |
static void * |
| 48 |
dlsym (void *handle, const char *name) |
| 49 |
{ |
| 50 |
return (void *) GetProcAddress ((HMODULE) handle, name); |
| 51 |
} |
| 52 |
|
| 53 |
static int ATTRIBUTE_UNUSED |
| 54 |
dlclose (void *handle) |
| 55 |
{ |
| 56 |
FreeLibrary ((HMODULE) handle); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
static const char * |
| 61 |
dlerror (void) |
| 62 |
{ |
| 63 |
return _("Unable to load DLL."); |
| 64 |
} |
| 65 |
|
| 66 |
#endif /* !defined (HAVE_DLFCN_H) && defined (_WIN32) */ |
| 67 |
|
| 68 |
#define CONFIG_ENV_NAME "XTENSA_GNU_CONFIG" |
| 69 |
|
| 70 |
const void *xtensa_load_config (const char *name ATTRIBUTE_UNUSED, |
| 71 |
const void *no_plugin_def, |
| 72 |
const void *no_name_def ATTRIBUTE_UNUSED) |
| 73 |
{ |
| 74 |
static int init; |
| 75 |
#ifdef ENABLE_PLUGIN |
| 76 |
static void *handle; |
| 77 |
void *p; |
| 78 |
|
| 79 |
if (!init) |
| 80 |
{ |
| 81 |
const char *path = getenv (CONFIG_ENV_NAME); |
| 82 |
|
| 83 |
init = 1; |
| 84 |
if (!path) |
| 85 |
return no_plugin_def; |
| 86 |
handle = dlopen (path, RTLD_LAZY); |
| 87 |
if (!handle) |
| 88 |
{ |
| 89 |
fatal_error (input_location, |
| 90 |
"%qs is defined but could not be loaded: %s", |
| 91 |
CONFIG_ENV_NAME, dlerror ()); |
| 92 |
exit (FATAL_EXIT_CODE); |
| 93 |
} |
| 94 |
if (dlsym (handle, "plugin_is_GPL_compatible") == NULL) |
| 95 |
{ |
| 96 |
fatal_error (input_location, |
| 97 |
"%qs plugin is not licensed under a GPL-compatible license", |
| 98 |
CONFIG_ENV_NAME); |
| 99 |
exit (FATAL_EXIT_CODE); |
| 100 |
} |
| 101 |
} |
| 102 |
else if (!handle) |
| 103 |
{ |
| 104 |
return no_plugin_def; |
| 105 |
} |
| 106 |
|
| 107 |
p = dlsym (handle, name); |
| 108 |
if (!p) |
| 109 |
{ |
| 110 |
if (no_name_def) |
| 111 |
return no_name_def; |
| 112 |
|
| 113 |
fatal_error (input_location, |
| 114 |
"%qs is loaded but symbol %qs is not found: %s", |
| 115 |
CONFIG_ENV_NAME, name, dlerror ()); |
| 116 |
exit (FATAL_EXIT_CODE); |
| 117 |
} |
| 118 |
return p; |
| 119 |
#else |
| 120 |
if (!init) |
| 121 |
{ |
| 122 |
const char *path = getenv (CONFIG_ENV_NAME); |
| 123 |
|
| 124 |
init = 1; |
| 125 |
if (path) |
| 126 |
{ |
| 127 |
fatal_error (input_location, |
| 128 |
"%qs is defined but plugin support is disabled", |
| 129 |
CONFIG_ENV_NAME); |
| 130 |
exit (FATAL_EXIT_CODE); |
| 131 |
} |
| 132 |
} |
| 133 |
return no_plugin_def; |
| 134 |
#endif |
| 135 |
} |
| 136 |
|
| 137 |
XTENSA_CONFIG_INSTANCE_LIST; |
| 138 |
|
| 139 |
#define _STRINGIFY(a) #a |
| 140 |
#define STRINGIFY(a) _STRINGIFY(a) |
| 141 |
|
| 142 |
#undef XTENSA_CONFIG_ENTRY |
| 143 |
#define XTENSA_CONFIG_ENTRY(a) "__" #a "=" STRINGIFY(a) |
| 144 |
|
| 145 |
static const char * const xtensa_config_strings[] = { |
| 146 |
XTENSA_CONFIG_ENTRY_LIST, |
| 147 |
NULL, |
| 148 |
}; |
| 149 |
|
| 150 |
const struct xtensa_config_v1 *xtensa_get_config_v1 (void) |
| 151 |
{ |
| 152 |
static const struct xtensa_config_v1 *config; |
| 153 |
|
| 154 |
if (!config) |
| 155 |
config = (const struct xtensa_config_v1 *) xtensa_load_config ("xtensa_config_v1", |
| 156 |
&xtensa_config_v1, |
| 157 |
NULL); |
| 158 |
return config; |
| 159 |
} |
| 160 |
|
| 161 |
const struct xtensa_config_v2 *xtensa_get_config_v2 (void) |
| 162 |
{ |
| 163 |
static const struct xtensa_config_v2 *config; |
| 164 |
static struct xtensa_config_v2 def; |
| 165 |
|
| 166 |
if (!config) |
| 167 |
config = (const struct xtensa_config_v2 *) xtensa_load_config ("xtensa_config_v2", |
| 168 |
&xtensa_config_v2, |
| 169 |
&def); |
| 170 |
return config; |
| 171 |
} |
| 172 |
|
| 173 |
const struct xtensa_config_v3 *xtensa_get_config_v3 (void) |
| 174 |
{ |
| 175 |
static const struct xtensa_config_v3 *config; |
| 176 |
static struct xtensa_config_v3 def; |
| 177 |
|
| 178 |
if (!config) |
| 179 |
config = (const struct xtensa_config_v3 *) xtensa_load_config ("xtensa_config_v3", |
| 180 |
&xtensa_config_v3, |
| 181 |
&def); |
| 182 |
return config; |
| 183 |
} |
| 184 |
|
| 185 |
const struct xtensa_config_v4 *xtensa_get_config_v4 (void) |
| 186 |
{ |
| 187 |
static const struct xtensa_config_v4 *config; |
| 188 |
static const struct xtensa_config_v4 def = { |
| 189 |
16, /* xchal_data_width */ |
| 190 |
1, /* xchal_unaligned_load_exception */ |
| 191 |
1, /* xchal_unaligned_store_exception */ |
| 192 |
0, /* xchal_unaligned_load_hw */ |
| 193 |
0, /* xchal_unaligned_store_hw */ |
| 194 |
}; |
| 195 |
|
| 196 |
if (!config) |
| 197 |
config = (const struct xtensa_config_v4 *) xtensa_load_config ("xtensa_config_v4", |
| 198 |
&xtensa_config_v4, |
| 199 |
&def); |
| 200 |
return config; |
| 201 |
} |
| 202 |
|
| 203 |
const char * const *xtensa_get_config_strings (void) |
| 204 |
{ |
| 205 |
static const char * const *config_strings; |
| 206 |
|
| 207 |
if (!config_strings) |
| 208 |
config_strings = (const char * const *) xtensa_load_config ("xtensa_config_strings", |
| 209 |
&xtensa_config_strings, |
| 210 |
NULL); |
| 211 |
|
| 212 |
return config_strings; |
| 213 |
} |