Coverage Report

Created: 2026-03-10 08:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/xtensa-dynconfig.c
Line
Count
Source
1
/* Xtensa configuration settings loader.
2
   Copyright (C) 2022-2026 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
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 "sysdep.h"
21
#include "bfd.h"
22
#include "libbfd.h"
23
24
#define XTENSA_CONFIG_DEFINITION
25
#include "xtensa-config.h"
26
#include "xtensa-dynconfig.h"
27
28
#if defined (HAVE_DLFCN_H)
29
#include <dlfcn.h>
30
#elif defined (HAVE_WINDOWS_H)
31
#include <windows.h>
32
#endif
33
34
#if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
35
36
#define RTLD_LAZY 0      /* Dummy value.  */
37
38
static void *
39
dlopen (const char *file, int mode ATTRIBUTE_UNUSED)
40
{
41
  return LoadLibrary (file);
42
}
43
44
static void *
45
dlsym (void *handle, const char *name)
46
{
47
  return (void *) GetProcAddress ((HMODULE) handle, name);
48
}
49
50
static int ATTRIBUTE_UNUSED
51
dlclose (void *handle)
52
{
53
  FreeLibrary ((HMODULE) handle);
54
  return 0;
55
}
56
57
static const char *
58
dlerror (void)
59
{
60
  return _("Unable to load DLL.");
61
}
62
63
#endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)  */
64
65
3
#define CONFIG_ENV_NAME "XTENSA_GNU_CONFIG"
66
67
const void *xtensa_load_config (const char *name ATTRIBUTE_UNUSED,
68
        const void *no_plugin_def,
69
        const void *no_name_def ATTRIBUTE_UNUSED)
70
3
{
71
3
  static int init;
72
3
  if (bfd_plugin_enabled ())
73
3
    {
74
3
      static void *handle;
75
3
      void *p;
76
77
3
      if (!init)
78
3
  {
79
3
    const char *path = getenv (CONFIG_ENV_NAME);
80
81
3
    init = 1;
82
3
    if (!path)
83
3
      return no_plugin_def;
84
0
    handle = dlopen (path, RTLD_LAZY);
85
0
    if (!handle)
86
0
      {
87
0
        _bfd_error_handler (_("%s is defined but could not be loaded: %s"),
88
0
          CONFIG_ENV_NAME, dlerror ());
89
0
        abort ();
90
0
      }
91
0
  }
92
0
      else if (!handle)
93
0
  {
94
0
    return no_plugin_def;
95
0
  }
96
97
0
      p = dlsym (handle, name);
98
0
      if (!p)
99
0
  {
100
0
    if (no_name_def)
101
0
      return no_name_def;
102
103
0
    _bfd_error_handler (_("%s is loaded but symbol \"%s\" is not found: %s"),
104
0
            CONFIG_ENV_NAME, name, dlerror ());
105
0
    abort ();
106
0
  }
107
0
      return p;
108
0
    }
109
0
  else if (!init)
110
0
    {
111
0
      const char *path = getenv (CONFIG_ENV_NAME);
112
113
0
      init = 1;
114
0
      if (path)
115
0
  {
116
0
    _bfd_error_handler (_("%s is defined but plugin support is disabled"),
117
0
            CONFIG_ENV_NAME);
118
0
    abort ();
119
0
  }
120
0
    }
121
0
  return no_plugin_def;
122
3
}
123
124
XTENSA_CONFIG_INSTANCE_LIST;
125
126
const struct xtensa_config_v1 *xtensa_get_config_v1 (void)
127
0
{
128
0
  static const struct xtensa_config_v1 *config;
129
130
0
  if (!config)
131
0
    config = (const struct xtensa_config_v1 *) xtensa_load_config ("xtensa_config_v1",
132
0
                   &xtensa_config_v1,
133
0
                   NULL);
134
0
  return config;
135
0
}
136
137
const struct xtensa_config_v2 *xtensa_get_config_v2 (void)
138
0
{
139
0
  static const struct xtensa_config_v2 *config;
140
0
  static const struct xtensa_config_v2 def;
141
142
0
  if (!config)
143
0
    config = (const struct xtensa_config_v2 *) xtensa_load_config ("xtensa_config_v2",
144
0
                   &xtensa_config_v2,
145
0
                   &def);
146
0
  return config;
147
0
}