1 1.1 christos /* Xtensa configuration settings loader. 2 1.1.1.2 christos Copyright (C) 2022-2025 Free Software Foundation, Inc. 3 1.1 christos 4 1.1 christos This file is part of BFD, the Binary File Descriptor library. 5 1.1 christos 6 1.1 christos GCC is free software; you can redistribute it and/or modify it under 7 1.1 christos the terms of the GNU General Public License as published by the Free 8 1.1 christos Software Foundation; either version 3, or (at your option) any later 9 1.1 christos version. 10 1.1 christos 11 1.1 christos GCC is distributed in the hope that it will be useful, but WITHOUT ANY 12 1.1 christos WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 1.1 christos FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 1.1 christos for more details. 15 1.1 christos 16 1.1 christos You should have received a copy of the GNU General Public License 17 1.1 christos along with GCC; see the file COPYING3. If not see 18 1.1 christos <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #include "sysdep.h" 21 1.1 christos #include "bfd.h" 22 1.1 christos #include "libbfd.h" 23 1.1 christos 24 1.1 christos #define XTENSA_CONFIG_DEFINITION 25 1.1 christos #include "xtensa-config.h" 26 1.1 christos #include "xtensa-dynconfig.h" 27 1.1 christos 28 1.1 christos #if defined (HAVE_DLFCN_H) 29 1.1 christos #include <dlfcn.h> 30 1.1 christos #elif defined (HAVE_WINDOWS_H) 31 1.1 christos #include <windows.h> 32 1.1 christos #endif 33 1.1 christos 34 1.1 christos #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) 35 1.1 christos 36 1.1 christos #define RTLD_LAZY 0 /* Dummy value. */ 37 1.1 christos 38 1.1 christos static void * 39 1.1 christos dlopen (const char *file, int mode ATTRIBUTE_UNUSED) 40 1.1 christos { 41 1.1 christos return LoadLibrary (file); 42 1.1 christos } 43 1.1 christos 44 1.1 christos static void * 45 1.1 christos dlsym (void *handle, const char *name) 46 1.1 christos { 47 1.1 christos return (void *) GetProcAddress ((HMODULE) handle, name); 48 1.1 christos } 49 1.1 christos 50 1.1 christos static int ATTRIBUTE_UNUSED 51 1.1 christos dlclose (void *handle) 52 1.1 christos { 53 1.1 christos FreeLibrary ((HMODULE) handle); 54 1.1 christos return 0; 55 1.1 christos } 56 1.1 christos 57 1.1 christos static const char * 58 1.1 christos dlerror (void) 59 1.1 christos { 60 1.1 christos return _("Unable to load DLL."); 61 1.1 christos } 62 1.1 christos 63 1.1 christos #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */ 64 1.1 christos 65 1.1 christos #define CONFIG_ENV_NAME "XTENSA_GNU_CONFIG" 66 1.1 christos 67 1.1 christos const void *xtensa_load_config (const char *name ATTRIBUTE_UNUSED, 68 1.1 christos const void *no_plugin_def, 69 1.1 christos const void *no_name_def ATTRIBUTE_UNUSED) 70 1.1 christos { 71 1.1 christos static int init; 72 1.1 christos #if BFD_SUPPORTS_PLUGINS 73 1.1 christos static void *handle; 74 1.1 christos void *p; 75 1.1 christos 76 1.1 christos if (!init) 77 1.1 christos { 78 1.1 christos const char *path = getenv (CONFIG_ENV_NAME); 79 1.1 christos 80 1.1 christos init = 1; 81 1.1 christos if (!path) 82 1.1 christos return no_plugin_def; 83 1.1 christos handle = dlopen (path, RTLD_LAZY); 84 1.1 christos if (!handle) 85 1.1 christos { 86 1.1 christos _bfd_error_handler (_("%s is defined but could not be loaded: %s"), 87 1.1 christos CONFIG_ENV_NAME, dlerror ()); 88 1.1 christos abort (); 89 1.1 christos } 90 1.1 christos } 91 1.1 christos else if (!handle) 92 1.1 christos { 93 1.1 christos return no_plugin_def; 94 1.1 christos } 95 1.1 christos 96 1.1 christos p = dlsym (handle, name); 97 1.1 christos if (!p) 98 1.1 christos { 99 1.1 christos if (no_name_def) 100 1.1 christos return no_name_def; 101 1.1 christos 102 1.1 christos _bfd_error_handler (_("%s is loaded but symbol \"%s\" is not found: %s"), 103 1.1 christos CONFIG_ENV_NAME, name, dlerror ()); 104 1.1 christos abort (); 105 1.1 christos } 106 1.1 christos return p; 107 1.1 christos #else 108 1.1 christos if (!init) 109 1.1 christos { 110 1.1 christos const char *path = getenv (CONFIG_ENV_NAME); 111 1.1 christos 112 1.1 christos init = 1; 113 1.1 christos if (path) 114 1.1 christos { 115 1.1 christos _bfd_error_handler (_("%s is defined but plugin support is disabled"), 116 1.1 christos CONFIG_ENV_NAME); 117 1.1 christos abort (); 118 1.1 christos } 119 1.1 christos } 120 1.1 christos return no_plugin_def; 121 1.1 christos #endif 122 1.1 christos } 123 1.1 christos 124 1.1 christos XTENSA_CONFIG_INSTANCE_LIST; 125 1.1 christos 126 1.1 christos const struct xtensa_config_v1 *xtensa_get_config_v1 (void) 127 1.1 christos { 128 1.1 christos static const struct xtensa_config_v1 *config; 129 1.1 christos 130 1.1 christos if (!config) 131 1.1 christos config = (const struct xtensa_config_v1 *) xtensa_load_config ("xtensa_config_v1", 132 1.1 christos &xtensa_config_v1, 133 1.1 christos NULL); 134 1.1 christos return config; 135 1.1 christos } 136 1.1 christos 137 1.1 christos const struct xtensa_config_v2 *xtensa_get_config_v2 (void) 138 1.1 christos { 139 1.1 christos static const struct xtensa_config_v2 *config; 140 1.1 christos static const struct xtensa_config_v2 def; 141 1.1 christos 142 1.1 christos if (!config) 143 1.1 christos config = (const struct xtensa_config_v2 *) xtensa_load_config ("xtensa_config_v2", 144 1.1 christos &xtensa_config_v2, 145 1.1 christos &def); 146 1.1 christos return config; 147 1.1 christos } 148