1*38fd1498Szrj /* Support for GCC plugin mechanism.
2*38fd1498Szrj Copyright (C) 2009-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify
7*38fd1498Szrj it under the terms of the GNU General Public License as published by
8*38fd1498Szrj the Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj any later version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful,
12*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3. If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>. */
19*38fd1498Szrj
20*38fd1498Szrj /* This file contains the support for GCC plugin mechanism based on the
21*38fd1498Szrj APIs described in doc/plugin.texi. */
22*38fd1498Szrj
23*38fd1498Szrj #include "config.h"
24*38fd1498Szrj #include "system.h"
25*38fd1498Szrj #include "coretypes.h"
26*38fd1498Szrj #include "options.h"
27*38fd1498Szrj #include "tree-pass.h"
28*38fd1498Szrj #include "diagnostic-core.h"
29*38fd1498Szrj #include "flags.h"
30*38fd1498Szrj #include "intl.h"
31*38fd1498Szrj #include "plugin.h"
32*38fd1498Szrj
33*38fd1498Szrj #ifdef ENABLE_PLUGIN
34*38fd1498Szrj #include "plugin-version.h"
35*38fd1498Szrj #endif
36*38fd1498Szrj
37*38fd1498Szrj #ifdef __MINGW32__
38*38fd1498Szrj #ifndef WIN32_LEAN_AND_MEAN
39*38fd1498Szrj #define WIN32_LEAN_AND_MEAN
40*38fd1498Szrj #endif
41*38fd1498Szrj #ifndef NOMINMAX
42*38fd1498Szrj #define NOMINMAX
43*38fd1498Szrj #endif
44*38fd1498Szrj #include <windows.h>
45*38fd1498Szrj #endif
46*38fd1498Szrj
47*38fd1498Szrj #define GCC_PLUGIN_STRINGIFY0(X) #X
48*38fd1498Szrj #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
49*38fd1498Szrj
50*38fd1498Szrj /* Event names as strings. Keep in sync with enum plugin_event. */
51*38fd1498Szrj static const char *plugin_event_name_init[] =
52*38fd1498Szrj {
53*38fd1498Szrj # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
54*38fd1498Szrj # include "plugin.def"
55*38fd1498Szrj # undef DEFEVENT
56*38fd1498Szrj };
57*38fd1498Szrj
58*38fd1498Szrj /* A printf format large enough for the largest event above. */
59*38fd1498Szrj #define FMT_FOR_PLUGIN_EVENT "%-32s"
60*38fd1498Szrj
61*38fd1498Szrj const char **plugin_event_name = plugin_event_name_init;
62*38fd1498Szrj
63*38fd1498Szrj /* Event hashtable helpers. */
64*38fd1498Szrj
65*38fd1498Szrj struct event_hasher : nofree_ptr_hash <const char *>
66*38fd1498Szrj {
67*38fd1498Szrj static inline hashval_t hash (const char **);
68*38fd1498Szrj static inline bool equal (const char **, const char **);
69*38fd1498Szrj };
70*38fd1498Szrj
71*38fd1498Szrj /* Helper function for the event hash table that hashes the entry V. */
72*38fd1498Szrj
73*38fd1498Szrj inline hashval_t
hash(const char ** v)74*38fd1498Szrj event_hasher::hash (const char **v)
75*38fd1498Szrj {
76*38fd1498Szrj return htab_hash_string (*v);
77*38fd1498Szrj }
78*38fd1498Szrj
79*38fd1498Szrj /* Helper function for the event hash table that compares the name of an
80*38fd1498Szrj existing entry (S1) with the given string (S2). */
81*38fd1498Szrj
82*38fd1498Szrj inline bool
equal(const char ** s1,const char ** s2)83*38fd1498Szrj event_hasher::equal (const char **s1, const char **s2)
84*38fd1498Szrj {
85*38fd1498Szrj return !strcmp (*s1, *s2);
86*38fd1498Szrj }
87*38fd1498Szrj
88*38fd1498Szrj /* A hash table to map event names to the position of the names in the
89*38fd1498Szrj plugin_event_name table. */
90*38fd1498Szrj static hash_table<event_hasher> *event_tab;
91*38fd1498Szrj
92*38fd1498Szrj /* Keep track of the limit of allocated events and space ready for
93*38fd1498Szrj allocating events. */
94*38fd1498Szrj static int event_last = PLUGIN_EVENT_FIRST_DYNAMIC;
95*38fd1498Szrj static int event_horizon = PLUGIN_EVENT_FIRST_DYNAMIC;
96*38fd1498Szrj
97*38fd1498Szrj /* Hash table for the plugin_name_args objects created during command-line
98*38fd1498Szrj parsing. */
99*38fd1498Szrj static htab_t plugin_name_args_tab = NULL;
100*38fd1498Szrj
101*38fd1498Szrj /* List node for keeping track of plugin-registered callback. */
102*38fd1498Szrj struct callback_info
103*38fd1498Szrj {
104*38fd1498Szrj const char *plugin_name; /* Name of plugin that registers the callback. */
105*38fd1498Szrj plugin_callback_func func; /* Callback to be called. */
106*38fd1498Szrj void *user_data; /* plugin-specified data. */
107*38fd1498Szrj struct callback_info *next;
108*38fd1498Szrj };
109*38fd1498Szrj
110*38fd1498Szrj /* An array of lists of 'callback_info' objects indexed by the event id. */
111*38fd1498Szrj static struct callback_info *plugin_callbacks_init[PLUGIN_EVENT_FIRST_DYNAMIC];
112*38fd1498Szrj static struct callback_info **plugin_callbacks = plugin_callbacks_init;
113*38fd1498Szrj
114*38fd1498Szrj /* For invoke_plugin_callbacks(), see plugin.h. */
115*38fd1498Szrj bool flag_plugin_added = false;
116*38fd1498Szrj
117*38fd1498Szrj #ifdef ENABLE_PLUGIN
118*38fd1498Szrj /* Each plugin should define an initialization function with exactly
119*38fd1498Szrj this name. */
120*38fd1498Szrj static const char *str_plugin_init_func_name = "plugin_init";
121*38fd1498Szrj
122*38fd1498Szrj /* Each plugin should define this symbol to assert that it is
123*38fd1498Szrj distributed under a GPL-compatible license. */
124*38fd1498Szrj static const char *str_license = "plugin_is_GPL_compatible";
125*38fd1498Szrj #endif
126*38fd1498Szrj
127*38fd1498Szrj /* Helper function for hashing the base_name of the plugin_name_args
128*38fd1498Szrj structure to be inserted into the hash table. */
129*38fd1498Szrj
130*38fd1498Szrj static hashval_t
htab_hash_plugin(const PTR p)131*38fd1498Szrj htab_hash_plugin (const PTR p)
132*38fd1498Szrj {
133*38fd1498Szrj const struct plugin_name_args *plugin = (const struct plugin_name_args *) p;
134*38fd1498Szrj return htab_hash_string (plugin->base_name);
135*38fd1498Szrj }
136*38fd1498Szrj
137*38fd1498Szrj /* Helper function for the hash table that compares the base_name of the
138*38fd1498Szrj existing entry (S1) with the given string (S2). */
139*38fd1498Szrj
140*38fd1498Szrj static int
htab_str_eq(const void * s1,const void * s2)141*38fd1498Szrj htab_str_eq (const void *s1, const void *s2)
142*38fd1498Szrj {
143*38fd1498Szrj const struct plugin_name_args *plugin = (const struct plugin_name_args *) s1;
144*38fd1498Szrj return !strcmp (plugin->base_name, (const char *) s2);
145*38fd1498Szrj }
146*38fd1498Szrj
147*38fd1498Szrj
148*38fd1498Szrj /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
149*38fd1498Szrj return NAME. */
150*38fd1498Szrj
151*38fd1498Szrj static char *
get_plugin_base_name(const char * full_name)152*38fd1498Szrj get_plugin_base_name (const char *full_name)
153*38fd1498Szrj {
154*38fd1498Szrj /* First get the base name part of the full-path name, i.e. NAME.so. */
155*38fd1498Szrj char *base_name = xstrdup (lbasename (full_name));
156*38fd1498Szrj
157*38fd1498Szrj /* Then get rid of the extension in the name, e.g., .so. */
158*38fd1498Szrj strip_off_ending (base_name, strlen (base_name));
159*38fd1498Szrj
160*38fd1498Szrj return base_name;
161*38fd1498Szrj }
162*38fd1498Szrj
163*38fd1498Szrj
164*38fd1498Szrj /* Create a plugin_name_args object for the given plugin and insert it
165*38fd1498Szrj to the hash table. This function is called when
166*38fd1498Szrj -fplugin=/path/to/NAME.so or -fplugin=NAME option is processed. */
167*38fd1498Szrj
168*38fd1498Szrj void
add_new_plugin(const char * plugin_name)169*38fd1498Szrj add_new_plugin (const char* plugin_name)
170*38fd1498Szrj {
171*38fd1498Szrj struct plugin_name_args *plugin;
172*38fd1498Szrj void **slot;
173*38fd1498Szrj char *base_name;
174*38fd1498Szrj bool name_is_short;
175*38fd1498Szrj const char *pc;
176*38fd1498Szrj
177*38fd1498Szrj flag_plugin_added = true;
178*38fd1498Szrj
179*38fd1498Szrj /* Replace short names by their full path when relevant. */
180*38fd1498Szrj name_is_short = !IS_ABSOLUTE_PATH (plugin_name);
181*38fd1498Szrj for (pc = plugin_name; name_is_short && *pc; pc++)
182*38fd1498Szrj if (*pc == '.' || IS_DIR_SEPARATOR (*pc))
183*38fd1498Szrj name_is_short = false;
184*38fd1498Szrj
185*38fd1498Szrj if (name_is_short)
186*38fd1498Szrj {
187*38fd1498Szrj base_name = CONST_CAST (char*, plugin_name);
188*38fd1498Szrj
189*38fd1498Szrj #if defined(__MINGW32__)
190*38fd1498Szrj static const char plugin_ext[] = ".dll";
191*38fd1498Szrj #elif defined(__APPLE__)
192*38fd1498Szrj /* Mac OS has two types of libraries: dynamic libraries (.dylib) and
193*38fd1498Szrj plugins (.bundle). Both can be used with dlopen()/dlsym() but the
194*38fd1498Szrj former cannot be linked at build time (i.e., with the -lfoo linker
195*38fd1498Szrj option). A GCC plugin is therefore probably a Mac OS plugin but their
196*38fd1498Szrj use seems to be quite rare and the .bundle extension is more of a
197*38fd1498Szrj recommendation rather than the rule. This raises the questions of how
198*38fd1498Szrj well they are supported by tools (e.g., libtool). So to avoid
199*38fd1498Szrj complications let's use the .dylib extension for now. In the future,
200*38fd1498Szrj if this proves to be an issue, we can always check for both
201*38fd1498Szrj extensions. */
202*38fd1498Szrj static const char plugin_ext[] = ".dylib";
203*38fd1498Szrj #else
204*38fd1498Szrj static const char plugin_ext[] = ".so";
205*38fd1498Szrj #endif
206*38fd1498Szrj
207*38fd1498Szrj plugin_name = concat (default_plugin_dir_name (), "/",
208*38fd1498Szrj plugin_name, plugin_ext, NULL);
209*38fd1498Szrj if (access (plugin_name, R_OK))
210*38fd1498Szrj fatal_error
211*38fd1498Szrj (input_location,
212*38fd1498Szrj "inaccessible plugin file %s expanded from short plugin name %s: %m",
213*38fd1498Szrj plugin_name, base_name);
214*38fd1498Szrj }
215*38fd1498Szrj else
216*38fd1498Szrj base_name = get_plugin_base_name (plugin_name);
217*38fd1498Szrj
218*38fd1498Szrj /* If this is the first -fplugin= option we encounter, create
219*38fd1498Szrj 'plugin_name_args_tab' hash table. */
220*38fd1498Szrj if (!plugin_name_args_tab)
221*38fd1498Szrj plugin_name_args_tab = htab_create (10, htab_hash_plugin, htab_str_eq,
222*38fd1498Szrj NULL);
223*38fd1498Szrj
224*38fd1498Szrj slot = htab_find_slot_with_hash (plugin_name_args_tab, base_name,
225*38fd1498Szrj htab_hash_string (base_name), INSERT);
226*38fd1498Szrj
227*38fd1498Szrj /* If the same plugin (name) has been specified earlier, either emit an
228*38fd1498Szrj error or a warning message depending on if they have identical full
229*38fd1498Szrj (path) names. */
230*38fd1498Szrj if (*slot)
231*38fd1498Szrj {
232*38fd1498Szrj plugin = (struct plugin_name_args *) *slot;
233*38fd1498Szrj if (strcmp (plugin->full_name, plugin_name))
234*38fd1498Szrj error ("plugin %s was specified with different paths:\n%s\n%s",
235*38fd1498Szrj plugin->base_name, plugin->full_name, plugin_name);
236*38fd1498Szrj return;
237*38fd1498Szrj }
238*38fd1498Szrj
239*38fd1498Szrj plugin = XCNEW (struct plugin_name_args);
240*38fd1498Szrj plugin->base_name = base_name;
241*38fd1498Szrj plugin->full_name = plugin_name;
242*38fd1498Szrj
243*38fd1498Szrj *slot = plugin;
244*38fd1498Szrj }
245*38fd1498Szrj
246*38fd1498Szrj
247*38fd1498Szrj /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
248*38fd1498Szrj 'plugin_argument' object for the parsed key-value pair. ARG is
249*38fd1498Szrj the <name>-<key>[=<value>] part of the option. */
250*38fd1498Szrj
251*38fd1498Szrj void
parse_plugin_arg_opt(const char * arg)252*38fd1498Szrj parse_plugin_arg_opt (const char *arg)
253*38fd1498Szrj {
254*38fd1498Szrj size_t len = 0, name_len = 0, key_len = 0, value_len = 0;
255*38fd1498Szrj const char *ptr, *name_start = arg, *key_start = NULL, *value_start = NULL;
256*38fd1498Szrj char *name, *key, *value;
257*38fd1498Szrj void **slot;
258*38fd1498Szrj bool name_parsed = false, key_parsed = false;
259*38fd1498Szrj
260*38fd1498Szrj /* Iterate over the ARG string and identify the starting character position
261*38fd1498Szrj of 'name', 'key', and 'value' and their lengths. */
262*38fd1498Szrj for (ptr = arg; *ptr; ++ptr)
263*38fd1498Szrj {
264*38fd1498Szrj /* Only the first '-' encountered is considered a separator between
265*38fd1498Szrj 'name' and 'key'. All the subsequent '-'s are considered part of
266*38fd1498Szrj 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
267*38fd1498Szrj the plugin name is 'foo' and the key is 'bar-primary-key'. */
268*38fd1498Szrj if (*ptr == '-' && !name_parsed)
269*38fd1498Szrj {
270*38fd1498Szrj name_len = len;
271*38fd1498Szrj len = 0;
272*38fd1498Szrj key_start = ptr + 1;
273*38fd1498Szrj name_parsed = true;
274*38fd1498Szrj continue;
275*38fd1498Szrj }
276*38fd1498Szrj else if (*ptr == '=')
277*38fd1498Szrj {
278*38fd1498Szrj if (!key_parsed)
279*38fd1498Szrj {
280*38fd1498Szrj key_len = len;
281*38fd1498Szrj len = 0;
282*38fd1498Szrj value_start = ptr + 1;
283*38fd1498Szrj key_parsed = true;
284*38fd1498Szrj }
285*38fd1498Szrj continue;
286*38fd1498Szrj }
287*38fd1498Szrj else
288*38fd1498Szrj ++len;
289*38fd1498Szrj }
290*38fd1498Szrj
291*38fd1498Szrj if (!key_start)
292*38fd1498Szrj {
293*38fd1498Szrj error ("malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
294*38fd1498Szrj arg);
295*38fd1498Szrj return;
296*38fd1498Szrj }
297*38fd1498Szrj
298*38fd1498Szrj /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
299*38fd1498Szrj Otherwise, it is the VALUE_LEN. */
300*38fd1498Szrj if (!value_start)
301*38fd1498Szrj key_len = len;
302*38fd1498Szrj else
303*38fd1498Szrj value_len = len;
304*38fd1498Szrj
305*38fd1498Szrj name = XNEWVEC (char, name_len + 1);
306*38fd1498Szrj strncpy (name, name_start, name_len);
307*38fd1498Szrj name[name_len] = '\0';
308*38fd1498Szrj
309*38fd1498Szrj /* Check if the named plugin has already been specified earlier in the
310*38fd1498Szrj command-line. */
311*38fd1498Szrj if (plugin_name_args_tab
312*38fd1498Szrj && ((slot = htab_find_slot_with_hash (plugin_name_args_tab, name,
313*38fd1498Szrj htab_hash_string (name), NO_INSERT))
314*38fd1498Szrj != NULL))
315*38fd1498Szrj {
316*38fd1498Szrj struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
317*38fd1498Szrj
318*38fd1498Szrj key = XNEWVEC (char, key_len + 1);
319*38fd1498Szrj strncpy (key, key_start, key_len);
320*38fd1498Szrj key[key_len] = '\0';
321*38fd1498Szrj if (value_start)
322*38fd1498Szrj {
323*38fd1498Szrj value = XNEWVEC (char, value_len + 1);
324*38fd1498Szrj strncpy (value, value_start, value_len);
325*38fd1498Szrj value[value_len] = '\0';
326*38fd1498Szrj }
327*38fd1498Szrj else
328*38fd1498Szrj value = NULL;
329*38fd1498Szrj
330*38fd1498Szrj /* Create a plugin_argument object for the parsed key-value pair.
331*38fd1498Szrj If there are already arguments for this plugin, we will need to
332*38fd1498Szrj adjust the argument array size by creating a new array and deleting
333*38fd1498Szrj the old one. If the performance ever becomes an issue, we can
334*38fd1498Szrj change the code by pre-allocating a larger array first. */
335*38fd1498Szrj if (plugin->argc > 0)
336*38fd1498Szrj {
337*38fd1498Szrj struct plugin_argument *args = XNEWVEC (struct plugin_argument,
338*38fd1498Szrj plugin->argc + 1);
339*38fd1498Szrj memcpy (args, plugin->argv,
340*38fd1498Szrj sizeof (struct plugin_argument) * plugin->argc);
341*38fd1498Szrj XDELETEVEC (plugin->argv);
342*38fd1498Szrj plugin->argv = args;
343*38fd1498Szrj ++plugin->argc;
344*38fd1498Szrj }
345*38fd1498Szrj else
346*38fd1498Szrj {
347*38fd1498Szrj gcc_assert (plugin->argv == NULL);
348*38fd1498Szrj plugin->argv = XNEWVEC (struct plugin_argument, 1);
349*38fd1498Szrj plugin->argc = 1;
350*38fd1498Szrj }
351*38fd1498Szrj
352*38fd1498Szrj plugin->argv[plugin->argc - 1].key = key;
353*38fd1498Szrj plugin->argv[plugin->argc - 1].value = value;
354*38fd1498Szrj }
355*38fd1498Szrj else
356*38fd1498Szrj error ("plugin %s should be specified before -fplugin-arg-%s "
357*38fd1498Szrj "in the command line", name, arg);
358*38fd1498Szrj
359*38fd1498Szrj /* We don't need the plugin's name anymore. Just release it. */
360*38fd1498Szrj XDELETEVEC (name);
361*38fd1498Szrj }
362*38fd1498Szrj
363*38fd1498Szrj /* Register additional plugin information. NAME is the name passed to
364*38fd1498Szrj plugin_init. INFO is the information that should be registered. */
365*38fd1498Szrj
366*38fd1498Szrj static void
register_plugin_info(const char * name,struct plugin_info * info)367*38fd1498Szrj register_plugin_info (const char* name, struct plugin_info *info)
368*38fd1498Szrj {
369*38fd1498Szrj void **slot = htab_find_slot_with_hash (plugin_name_args_tab, name,
370*38fd1498Szrj htab_hash_string (name), NO_INSERT);
371*38fd1498Szrj struct plugin_name_args *plugin;
372*38fd1498Szrj
373*38fd1498Szrj if (slot == NULL)
374*38fd1498Szrj {
375*38fd1498Szrj error ("unable to register info for plugin %qs - plugin name not found",
376*38fd1498Szrj name);
377*38fd1498Szrj return;
378*38fd1498Szrj }
379*38fd1498Szrj plugin = (struct plugin_name_args *) *slot;
380*38fd1498Szrj plugin->version = info->version;
381*38fd1498Szrj plugin->help = info->help;
382*38fd1498Szrj }
383*38fd1498Szrj
384*38fd1498Szrj /* Look up the event id for NAME. If the name is not found, return -1
385*38fd1498Szrj if INSERT is NO_INSERT. */
386*38fd1498Szrj
387*38fd1498Szrj int
get_named_event_id(const char * name,enum insert_option insert)388*38fd1498Szrj get_named_event_id (const char *name, enum insert_option insert)
389*38fd1498Szrj {
390*38fd1498Szrj const char ***slot;
391*38fd1498Szrj
392*38fd1498Szrj if (!event_tab)
393*38fd1498Szrj {
394*38fd1498Szrj int i;
395*38fd1498Szrj
396*38fd1498Szrj event_tab = new hash_table<event_hasher> (150);
397*38fd1498Szrj for (i = 0; i < event_last; i++)
398*38fd1498Szrj {
399*38fd1498Szrj slot = event_tab->find_slot (&plugin_event_name[i], INSERT);
400*38fd1498Szrj gcc_assert (*slot == HTAB_EMPTY_ENTRY);
401*38fd1498Szrj *slot = &plugin_event_name[i];
402*38fd1498Szrj }
403*38fd1498Szrj }
404*38fd1498Szrj slot = event_tab->find_slot (&name, insert);
405*38fd1498Szrj if (slot == NULL)
406*38fd1498Szrj return -1;
407*38fd1498Szrj if (*slot != HTAB_EMPTY_ENTRY)
408*38fd1498Szrj return *slot - &plugin_event_name[0];
409*38fd1498Szrj
410*38fd1498Szrj if (event_last >= event_horizon)
411*38fd1498Szrj {
412*38fd1498Szrj event_horizon = event_last * 2;
413*38fd1498Szrj if (plugin_event_name == plugin_event_name_init)
414*38fd1498Szrj {
415*38fd1498Szrj plugin_event_name = XNEWVEC (const char *, event_horizon);
416*38fd1498Szrj memcpy (plugin_event_name, plugin_event_name_init,
417*38fd1498Szrj sizeof plugin_event_name_init);
418*38fd1498Szrj plugin_callbacks = XNEWVEC (struct callback_info *, event_horizon);
419*38fd1498Szrj memcpy (plugin_callbacks, plugin_callbacks_init,
420*38fd1498Szrj sizeof plugin_callbacks_init);
421*38fd1498Szrj }
422*38fd1498Szrj else
423*38fd1498Szrj {
424*38fd1498Szrj plugin_event_name
425*38fd1498Szrj = XRESIZEVEC (const char *, plugin_event_name, event_horizon);
426*38fd1498Szrj plugin_callbacks = XRESIZEVEC (struct callback_info *,
427*38fd1498Szrj plugin_callbacks, event_horizon);
428*38fd1498Szrj }
429*38fd1498Szrj /* All the pointers in the hash table will need to be updated. */
430*38fd1498Szrj delete event_tab;
431*38fd1498Szrj event_tab = NULL;
432*38fd1498Szrj }
433*38fd1498Szrj else
434*38fd1498Szrj *slot = &plugin_event_name[event_last];
435*38fd1498Szrj plugin_event_name[event_last] = name;
436*38fd1498Szrj return event_last++;
437*38fd1498Szrj }
438*38fd1498Szrj
439*38fd1498Szrj /* Called from the plugin's initialization code. Register a single callback.
440*38fd1498Szrj This function can be called multiple times.
441*38fd1498Szrj
442*38fd1498Szrj PLUGIN_NAME - display name for this plugin
443*38fd1498Szrj EVENT - which event the callback is for
444*38fd1498Szrj CALLBACK - the callback to be called at the event
445*38fd1498Szrj USER_DATA - plugin-provided data */
446*38fd1498Szrj
447*38fd1498Szrj void
register_callback(const char * plugin_name,int event,plugin_callback_func callback,void * user_data)448*38fd1498Szrj register_callback (const char *plugin_name,
449*38fd1498Szrj int event,
450*38fd1498Szrj plugin_callback_func callback,
451*38fd1498Szrj void *user_data)
452*38fd1498Szrj {
453*38fd1498Szrj switch (event)
454*38fd1498Szrj {
455*38fd1498Szrj case PLUGIN_PASS_MANAGER_SETUP:
456*38fd1498Szrj gcc_assert (!callback);
457*38fd1498Szrj register_pass ((struct register_pass_info *) user_data);
458*38fd1498Szrj break;
459*38fd1498Szrj case PLUGIN_INFO:
460*38fd1498Szrj gcc_assert (!callback);
461*38fd1498Szrj register_plugin_info (plugin_name, (struct plugin_info *) user_data);
462*38fd1498Szrj break;
463*38fd1498Szrj case PLUGIN_REGISTER_GGC_ROOTS:
464*38fd1498Szrj gcc_assert (!callback);
465*38fd1498Szrj ggc_register_root_tab ((const struct ggc_root_tab*) user_data);
466*38fd1498Szrj break;
467*38fd1498Szrj case PLUGIN_EVENT_FIRST_DYNAMIC:
468*38fd1498Szrj default:
469*38fd1498Szrj if (event < PLUGIN_EVENT_FIRST_DYNAMIC || event >= event_last)
470*38fd1498Szrj {
471*38fd1498Szrj error ("unknown callback event registered by plugin %s",
472*38fd1498Szrj plugin_name);
473*38fd1498Szrj return;
474*38fd1498Szrj }
475*38fd1498Szrj /* Fall through. */
476*38fd1498Szrj case PLUGIN_START_PARSE_FUNCTION:
477*38fd1498Szrj case PLUGIN_FINISH_PARSE_FUNCTION:
478*38fd1498Szrj case PLUGIN_FINISH_TYPE:
479*38fd1498Szrj case PLUGIN_FINISH_DECL:
480*38fd1498Szrj case PLUGIN_START_UNIT:
481*38fd1498Szrj case PLUGIN_FINISH_UNIT:
482*38fd1498Szrj case PLUGIN_PRE_GENERICIZE:
483*38fd1498Szrj case PLUGIN_GGC_START:
484*38fd1498Szrj case PLUGIN_GGC_MARKING:
485*38fd1498Szrj case PLUGIN_GGC_END:
486*38fd1498Szrj case PLUGIN_ATTRIBUTES:
487*38fd1498Szrj case PLUGIN_PRAGMAS:
488*38fd1498Szrj case PLUGIN_FINISH:
489*38fd1498Szrj case PLUGIN_ALL_PASSES_START:
490*38fd1498Szrj case PLUGIN_ALL_PASSES_END:
491*38fd1498Szrj case PLUGIN_ALL_IPA_PASSES_START:
492*38fd1498Szrj case PLUGIN_ALL_IPA_PASSES_END:
493*38fd1498Szrj case PLUGIN_OVERRIDE_GATE:
494*38fd1498Szrj case PLUGIN_PASS_EXECUTION:
495*38fd1498Szrj case PLUGIN_EARLY_GIMPLE_PASSES_START:
496*38fd1498Szrj case PLUGIN_EARLY_GIMPLE_PASSES_END:
497*38fd1498Szrj case PLUGIN_NEW_PASS:
498*38fd1498Szrj case PLUGIN_INCLUDE_FILE:
499*38fd1498Szrj {
500*38fd1498Szrj struct callback_info *new_callback;
501*38fd1498Szrj if (!callback)
502*38fd1498Szrj {
503*38fd1498Szrj error ("plugin %s registered a null callback function "
504*38fd1498Szrj "for event %s", plugin_name, plugin_event_name[event]);
505*38fd1498Szrj return;
506*38fd1498Szrj }
507*38fd1498Szrj new_callback = XNEW (struct callback_info);
508*38fd1498Szrj new_callback->plugin_name = plugin_name;
509*38fd1498Szrj new_callback->func = callback;
510*38fd1498Szrj new_callback->user_data = user_data;
511*38fd1498Szrj new_callback->next = plugin_callbacks[event];
512*38fd1498Szrj plugin_callbacks[event] = new_callback;
513*38fd1498Szrj }
514*38fd1498Szrj break;
515*38fd1498Szrj }
516*38fd1498Szrj }
517*38fd1498Szrj
518*38fd1498Szrj /* Remove a callback for EVENT which has been registered with for a plugin
519*38fd1498Szrj PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
520*38fd1498Szrj found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
521*38fd1498Szrj callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
522*38fd1498Szrj int
unregister_callback(const char * plugin_name,int event)523*38fd1498Szrj unregister_callback (const char *plugin_name, int event)
524*38fd1498Szrj {
525*38fd1498Szrj struct callback_info *callback, **cbp;
526*38fd1498Szrj
527*38fd1498Szrj if (event >= event_last)
528*38fd1498Szrj return PLUGEVT_NO_SUCH_EVENT;
529*38fd1498Szrj
530*38fd1498Szrj for (cbp = &plugin_callbacks[event]; (callback = *cbp); cbp = &callback->next)
531*38fd1498Szrj if (strcmp (callback->plugin_name, plugin_name) == 0)
532*38fd1498Szrj {
533*38fd1498Szrj *cbp = callback->next;
534*38fd1498Szrj return PLUGEVT_SUCCESS;
535*38fd1498Szrj }
536*38fd1498Szrj return PLUGEVT_NO_CALLBACK;
537*38fd1498Szrj }
538*38fd1498Szrj
539*38fd1498Szrj /* Invoke all plugin callbacks registered with the specified event,
540*38fd1498Szrj called from invoke_plugin_callbacks(). */
541*38fd1498Szrj
542*38fd1498Szrj int
invoke_plugin_callbacks_full(int event,void * gcc_data)543*38fd1498Szrj invoke_plugin_callbacks_full (int event, void *gcc_data)
544*38fd1498Szrj {
545*38fd1498Szrj int retval = PLUGEVT_SUCCESS;
546*38fd1498Szrj
547*38fd1498Szrj timevar_push (TV_PLUGIN_RUN);
548*38fd1498Szrj
549*38fd1498Szrj switch (event)
550*38fd1498Szrj {
551*38fd1498Szrj case PLUGIN_EVENT_FIRST_DYNAMIC:
552*38fd1498Szrj default:
553*38fd1498Szrj gcc_assert (event >= PLUGIN_EVENT_FIRST_DYNAMIC);
554*38fd1498Szrj gcc_assert (event < event_last);
555*38fd1498Szrj /* Fall through. */
556*38fd1498Szrj case PLUGIN_START_PARSE_FUNCTION:
557*38fd1498Szrj case PLUGIN_FINISH_PARSE_FUNCTION:
558*38fd1498Szrj case PLUGIN_FINISH_TYPE:
559*38fd1498Szrj case PLUGIN_FINISH_DECL:
560*38fd1498Szrj case PLUGIN_START_UNIT:
561*38fd1498Szrj case PLUGIN_FINISH_UNIT:
562*38fd1498Szrj case PLUGIN_PRE_GENERICIZE:
563*38fd1498Szrj case PLUGIN_ATTRIBUTES:
564*38fd1498Szrj case PLUGIN_PRAGMAS:
565*38fd1498Szrj case PLUGIN_FINISH:
566*38fd1498Szrj case PLUGIN_GGC_START:
567*38fd1498Szrj case PLUGIN_GGC_MARKING:
568*38fd1498Szrj case PLUGIN_GGC_END:
569*38fd1498Szrj case PLUGIN_ALL_PASSES_START:
570*38fd1498Szrj case PLUGIN_ALL_PASSES_END:
571*38fd1498Szrj case PLUGIN_ALL_IPA_PASSES_START:
572*38fd1498Szrj case PLUGIN_ALL_IPA_PASSES_END:
573*38fd1498Szrj case PLUGIN_OVERRIDE_GATE:
574*38fd1498Szrj case PLUGIN_PASS_EXECUTION:
575*38fd1498Szrj case PLUGIN_EARLY_GIMPLE_PASSES_START:
576*38fd1498Szrj case PLUGIN_EARLY_GIMPLE_PASSES_END:
577*38fd1498Szrj case PLUGIN_NEW_PASS:
578*38fd1498Szrj case PLUGIN_INCLUDE_FILE:
579*38fd1498Szrj {
580*38fd1498Szrj /* Iterate over every callback registered with this event and
581*38fd1498Szrj call it. */
582*38fd1498Szrj struct callback_info *callback = plugin_callbacks[event];
583*38fd1498Szrj
584*38fd1498Szrj if (!callback)
585*38fd1498Szrj retval = PLUGEVT_NO_CALLBACK;
586*38fd1498Szrj for ( ; callback; callback = callback->next)
587*38fd1498Szrj (*callback->func) (gcc_data, callback->user_data);
588*38fd1498Szrj }
589*38fd1498Szrj break;
590*38fd1498Szrj
591*38fd1498Szrj case PLUGIN_PASS_MANAGER_SETUP:
592*38fd1498Szrj case PLUGIN_REGISTER_GGC_ROOTS:
593*38fd1498Szrj gcc_assert (false);
594*38fd1498Szrj }
595*38fd1498Szrj
596*38fd1498Szrj timevar_pop (TV_PLUGIN_RUN);
597*38fd1498Szrj return retval;
598*38fd1498Szrj }
599*38fd1498Szrj
600*38fd1498Szrj #ifdef ENABLE_PLUGIN
601*38fd1498Szrj
602*38fd1498Szrj /* Try to initialize PLUGIN. Return true if successful. */
603*38fd1498Szrj
604*38fd1498Szrj #ifdef __MINGW32__
605*38fd1498Szrj
606*38fd1498Szrj // Return a message string for last error or NULL if unknown. Must be freed
607*38fd1498Szrj // with LocalFree().
608*38fd1498Szrj static inline char *
win32_error_msg()609*38fd1498Szrj win32_error_msg ()
610*38fd1498Szrj {
611*38fd1498Szrj char *msg;
612*38fd1498Szrj return FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
613*38fd1498Szrj FORMAT_MESSAGE_FROM_SYSTEM |
614*38fd1498Szrj FORMAT_MESSAGE_IGNORE_INSERTS |
615*38fd1498Szrj FORMAT_MESSAGE_MAX_WIDTH_MASK,
616*38fd1498Szrj 0,
617*38fd1498Szrj GetLastError (),
618*38fd1498Szrj MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
619*38fd1498Szrj (char*)&msg,
620*38fd1498Szrj 0,
621*38fd1498Szrj 0)
622*38fd1498Szrj ? msg
623*38fd1498Szrj : NULL;
624*38fd1498Szrj }
625*38fd1498Szrj
626*38fd1498Szrj static bool
try_init_one_plugin(struct plugin_name_args * plugin)627*38fd1498Szrj try_init_one_plugin (struct plugin_name_args *plugin)
628*38fd1498Szrj {
629*38fd1498Szrj HMODULE dl_handle;
630*38fd1498Szrj plugin_init_func plugin_init;
631*38fd1498Szrj
632*38fd1498Szrj dl_handle = LoadLibrary (plugin->full_name);
633*38fd1498Szrj if (!dl_handle)
634*38fd1498Szrj {
635*38fd1498Szrj char *err = win32_error_msg ();
636*38fd1498Szrj error ("cannot load plugin %s\n%s", plugin->full_name, err);
637*38fd1498Szrj LocalFree (err);
638*38fd1498Szrj return false;
639*38fd1498Szrj }
640*38fd1498Szrj
641*38fd1498Szrj /* Check the plugin license. Unlike the name suggests, GetProcAddress()
642*38fd1498Szrj can be used for both functions and variables. */
643*38fd1498Szrj if (GetProcAddress (dl_handle, str_license) == NULL)
644*38fd1498Szrj {
645*38fd1498Szrj char *err = win32_error_msg ();
646*38fd1498Szrj fatal_error (input_location,
647*38fd1498Szrj "plugin %s is not licensed under a GPL-compatible license\n"
648*38fd1498Szrj "%s", plugin->full_name, err);
649*38fd1498Szrj }
650*38fd1498Szrj
651*38fd1498Szrj /* Unlike dlsym(), GetProcAddress() returns a pointer to a function so we
652*38fd1498Szrj can cast directly without union tricks. */
653*38fd1498Szrj plugin_init = (plugin_init_func)
654*38fd1498Szrj GetProcAddress (dl_handle, str_plugin_init_func_name);
655*38fd1498Szrj
656*38fd1498Szrj if (plugin_init == NULL)
657*38fd1498Szrj {
658*38fd1498Szrj char *err = win32_error_msg ();
659*38fd1498Szrj FreeLibrary (dl_handle);
660*38fd1498Szrj error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
661*38fd1498Szrj plugin->full_name, err);
662*38fd1498Szrj LocalFree (err);
663*38fd1498Szrj return false;
664*38fd1498Szrj }
665*38fd1498Szrj
666*38fd1498Szrj /* Call the plugin-provided initialization routine with the arguments. */
667*38fd1498Szrj if ((*plugin_init) (plugin, &gcc_version))
668*38fd1498Szrj {
669*38fd1498Szrj FreeLibrary (dl_handle);
670*38fd1498Szrj error ("fail to initialize plugin %s", plugin->full_name);
671*38fd1498Szrj return false;
672*38fd1498Szrj }
673*38fd1498Szrj /* Leak dl_handle on purpose to ensure the plugin is loaded for the
674*38fd1498Szrj entire run of the compiler. */
675*38fd1498Szrj return true;
676*38fd1498Szrj }
677*38fd1498Szrj
678*38fd1498Szrj #else // POSIX-like with dlopen()/dlsym().
679*38fd1498Szrj
680*38fd1498Szrj /* We need a union to cast dlsym return value to a function pointer
681*38fd1498Szrj as ISO C forbids assignment between function pointer and 'void *'.
682*38fd1498Szrj Use explicit union instead of __extension__(<union_cast>) for
683*38fd1498Szrj portability. */
684*38fd1498Szrj #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
685*38fd1498Szrj #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
686*38fd1498Szrj #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
687*38fd1498Szrj
688*38fd1498Szrj static bool
try_init_one_plugin(struct plugin_name_args * plugin)689*38fd1498Szrj try_init_one_plugin (struct plugin_name_args *plugin)
690*38fd1498Szrj {
691*38fd1498Szrj void *dl_handle;
692*38fd1498Szrj plugin_init_func plugin_init;
693*38fd1498Szrj const char *err;
694*38fd1498Szrj PTR_UNION_TYPE (plugin_init_func) plugin_init_union;
695*38fd1498Szrj
696*38fd1498Szrj /* We use RTLD_NOW to accelerate binding and detect any mismatch
697*38fd1498Szrj between the API expected by the plugin and the GCC API; we use
698*38fd1498Szrj RTLD_GLOBAL which is useful to plugins which themselves call
699*38fd1498Szrj dlopen. */
700*38fd1498Szrj dl_handle = dlopen (plugin->full_name, RTLD_NOW | RTLD_GLOBAL);
701*38fd1498Szrj if (!dl_handle)
702*38fd1498Szrj {
703*38fd1498Szrj error ("cannot load plugin %s\n%s", plugin->full_name, dlerror ());
704*38fd1498Szrj return false;
705*38fd1498Szrj }
706*38fd1498Szrj
707*38fd1498Szrj /* Clear any existing error. */
708*38fd1498Szrj dlerror ();
709*38fd1498Szrj
710*38fd1498Szrj /* Check the plugin license. */
711*38fd1498Szrj if (dlsym (dl_handle, str_license) == NULL)
712*38fd1498Szrj fatal_error (input_location,
713*38fd1498Szrj "plugin %s is not licensed under a GPL-compatible license\n"
714*38fd1498Szrj "%s", plugin->full_name, dlerror ());
715*38fd1498Szrj
716*38fd1498Szrj PTR_UNION_AS_VOID_PTR (plugin_init_union) =
717*38fd1498Szrj dlsym (dl_handle, str_plugin_init_func_name);
718*38fd1498Szrj plugin_init = PTR_UNION_AS_CAST_PTR (plugin_init_union);
719*38fd1498Szrj
720*38fd1498Szrj if ((err = dlerror ()) != NULL)
721*38fd1498Szrj {
722*38fd1498Szrj dlclose(dl_handle);
723*38fd1498Szrj error ("cannot find %s in plugin %s\n%s", str_plugin_init_func_name,
724*38fd1498Szrj plugin->full_name, err);
725*38fd1498Szrj return false;
726*38fd1498Szrj }
727*38fd1498Szrj
728*38fd1498Szrj /* Call the plugin-provided initialization routine with the arguments. */
729*38fd1498Szrj if ((*plugin_init) (plugin, &gcc_version))
730*38fd1498Szrj {
731*38fd1498Szrj dlclose(dl_handle);
732*38fd1498Szrj error ("fail to initialize plugin %s", plugin->full_name);
733*38fd1498Szrj return false;
734*38fd1498Szrj }
735*38fd1498Szrj /* leak dl_handle on purpose to ensure the plugin is loaded for the
736*38fd1498Szrj entire run of the compiler. */
737*38fd1498Szrj return true;
738*38fd1498Szrj }
739*38fd1498Szrj #endif
740*38fd1498Szrj
741*38fd1498Szrj /* Routine to dlopen and initialize one plugin. This function is passed to
742*38fd1498Szrj (and called by) the hash table traverse routine. Return 1 for the
743*38fd1498Szrj htab_traverse to continue scan, 0 to stop.
744*38fd1498Szrj
745*38fd1498Szrj SLOT - slot of the hash table element
746*38fd1498Szrj INFO - auxiliary pointer handed to hash table traverse routine
747*38fd1498Szrj (unused in this function) */
748*38fd1498Szrj
749*38fd1498Szrj static int
init_one_plugin(void ** slot,void * ARG_UNUSED (info))750*38fd1498Szrj init_one_plugin (void **slot, void * ARG_UNUSED (info))
751*38fd1498Szrj {
752*38fd1498Szrj struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
753*38fd1498Szrj bool ok = try_init_one_plugin (plugin);
754*38fd1498Szrj if (!ok)
755*38fd1498Szrj {
756*38fd1498Szrj htab_remove_elt_with_hash (plugin_name_args_tab, plugin->base_name,
757*38fd1498Szrj htab_hash_string (plugin->base_name));
758*38fd1498Szrj XDELETE (plugin);
759*38fd1498Szrj }
760*38fd1498Szrj return 1;
761*38fd1498Szrj }
762*38fd1498Szrj
763*38fd1498Szrj #endif /* ENABLE_PLUGIN */
764*38fd1498Szrj
765*38fd1498Szrj /* Main plugin initialization function. Called from compile_file() in
766*38fd1498Szrj toplev.c. */
767*38fd1498Szrj
768*38fd1498Szrj void
initialize_plugins(void)769*38fd1498Szrj initialize_plugins (void)
770*38fd1498Szrj {
771*38fd1498Szrj /* If no plugin was specified in the command-line, simply return. */
772*38fd1498Szrj if (!plugin_name_args_tab)
773*38fd1498Szrj return;
774*38fd1498Szrj
775*38fd1498Szrj timevar_push (TV_PLUGIN_INIT);
776*38fd1498Szrj
777*38fd1498Szrj #ifdef ENABLE_PLUGIN
778*38fd1498Szrj /* Traverse and initialize each plugin specified in the command-line. */
779*38fd1498Szrj htab_traverse_noresize (plugin_name_args_tab, init_one_plugin, NULL);
780*38fd1498Szrj #endif
781*38fd1498Szrj
782*38fd1498Szrj timevar_pop (TV_PLUGIN_INIT);
783*38fd1498Szrj }
784*38fd1498Szrj
785*38fd1498Szrj /* Release memory used by one plugin. */
786*38fd1498Szrj
787*38fd1498Szrj static int
finalize_one_plugin(void ** slot,void * ARG_UNUSED (info))788*38fd1498Szrj finalize_one_plugin (void **slot, void * ARG_UNUSED (info))
789*38fd1498Szrj {
790*38fd1498Szrj struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
791*38fd1498Szrj XDELETE (plugin);
792*38fd1498Szrj return 1;
793*38fd1498Szrj }
794*38fd1498Szrj
795*38fd1498Szrj /* Free memory allocated by the plugin system. */
796*38fd1498Szrj
797*38fd1498Szrj void
finalize_plugins(void)798*38fd1498Szrj finalize_plugins (void)
799*38fd1498Szrj {
800*38fd1498Szrj if (!plugin_name_args_tab)
801*38fd1498Szrj return;
802*38fd1498Szrj
803*38fd1498Szrj /* We can now delete the plugin_name_args object as it will no longer
804*38fd1498Szrj be used. Note that base_name and argv fields (both of which were also
805*38fd1498Szrj dynamically allocated) are not freed as they could still be used by
806*38fd1498Szrj the plugin code. */
807*38fd1498Szrj
808*38fd1498Szrj htab_traverse_noresize (plugin_name_args_tab, finalize_one_plugin, NULL);
809*38fd1498Szrj
810*38fd1498Szrj /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
811*38fd1498Szrj htab_delete (plugin_name_args_tab);
812*38fd1498Szrj plugin_name_args_tab = NULL;
813*38fd1498Szrj }
814*38fd1498Szrj
815*38fd1498Szrj /* Used to pass options to htab_traverse callbacks. */
816*38fd1498Szrj
817*38fd1498Szrj struct print_options
818*38fd1498Szrj {
819*38fd1498Szrj FILE *file;
820*38fd1498Szrj const char *indent;
821*38fd1498Szrj };
822*38fd1498Szrj
823*38fd1498Szrj /* Print the version of one plugin. */
824*38fd1498Szrj
825*38fd1498Szrj static int
print_version_one_plugin(void ** slot,void * data)826*38fd1498Szrj print_version_one_plugin (void **slot, void *data)
827*38fd1498Szrj {
828*38fd1498Szrj struct print_options *opt = (struct print_options *) data;
829*38fd1498Szrj struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
830*38fd1498Szrj const char *version = plugin->version ? plugin->version : "Unknown version.";
831*38fd1498Szrj
832*38fd1498Szrj fprintf (opt->file, " %s%s: %s\n", opt->indent, plugin->base_name, version);
833*38fd1498Szrj return 1;
834*38fd1498Szrj }
835*38fd1498Szrj
836*38fd1498Szrj /* Print the version of each plugin. */
837*38fd1498Szrj
838*38fd1498Szrj void
print_plugins_versions(FILE * file,const char * indent)839*38fd1498Szrj print_plugins_versions (FILE *file, const char *indent)
840*38fd1498Szrj {
841*38fd1498Szrj struct print_options opt;
842*38fd1498Szrj opt.file = file;
843*38fd1498Szrj opt.indent = indent;
844*38fd1498Szrj if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
845*38fd1498Szrj return;
846*38fd1498Szrj
847*38fd1498Szrj fprintf (file, "%sVersions of loaded plugins:\n", indent);
848*38fd1498Szrj htab_traverse_noresize (plugin_name_args_tab, print_version_one_plugin, &opt);
849*38fd1498Szrj }
850*38fd1498Szrj
851*38fd1498Szrj /* Print help for one plugin. SLOT is the hash table slot. DATA is the
852*38fd1498Szrj argument to htab_traverse_noresize. */
853*38fd1498Szrj
854*38fd1498Szrj static int
print_help_one_plugin(void ** slot,void * data)855*38fd1498Szrj print_help_one_plugin (void **slot, void *data)
856*38fd1498Szrj {
857*38fd1498Szrj struct print_options *opt = (struct print_options *) data;
858*38fd1498Szrj struct plugin_name_args *plugin = (struct plugin_name_args *) *slot;
859*38fd1498Szrj const char *help = plugin->help ? plugin->help : "No help available .";
860*38fd1498Szrj
861*38fd1498Szrj char *dup = xstrdup (help);
862*38fd1498Szrj char *p, *nl;
863*38fd1498Szrj fprintf (opt->file, " %s%s:\n", opt->indent, plugin->base_name);
864*38fd1498Szrj
865*38fd1498Szrj for (p = nl = dup; nl; p = nl)
866*38fd1498Szrj {
867*38fd1498Szrj nl = strchr (nl, '\n');
868*38fd1498Szrj if (nl)
869*38fd1498Szrj {
870*38fd1498Szrj *nl = '\0';
871*38fd1498Szrj nl++;
872*38fd1498Szrj }
873*38fd1498Szrj fprintf (opt->file, " %s %s\n", opt->indent, p);
874*38fd1498Szrj }
875*38fd1498Szrj
876*38fd1498Szrj free (dup);
877*38fd1498Szrj return 1;
878*38fd1498Szrj }
879*38fd1498Szrj
880*38fd1498Szrj /* Print help for each plugin. The output goes to FILE and every line starts
881*38fd1498Szrj with INDENT. */
882*38fd1498Szrj
883*38fd1498Szrj void
print_plugins_help(FILE * file,const char * indent)884*38fd1498Szrj print_plugins_help (FILE *file, const char *indent)
885*38fd1498Szrj {
886*38fd1498Szrj struct print_options opt;
887*38fd1498Szrj opt.file = file;
888*38fd1498Szrj opt.indent = indent;
889*38fd1498Szrj if (!plugin_name_args_tab || htab_elements (plugin_name_args_tab) == 0)
890*38fd1498Szrj return;
891*38fd1498Szrj
892*38fd1498Szrj fprintf (file, "%sHelp for the loaded plugins:\n", indent);
893*38fd1498Szrj htab_traverse_noresize (plugin_name_args_tab, print_help_one_plugin, &opt);
894*38fd1498Szrj }
895*38fd1498Szrj
896*38fd1498Szrj
897*38fd1498Szrj /* Return true if plugins have been loaded. */
898*38fd1498Szrj
899*38fd1498Szrj bool
plugins_active_p(void)900*38fd1498Szrj plugins_active_p (void)
901*38fd1498Szrj {
902*38fd1498Szrj int event;
903*38fd1498Szrj
904*38fd1498Szrj for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
905*38fd1498Szrj if (plugin_callbacks[event])
906*38fd1498Szrj return true;
907*38fd1498Szrj
908*38fd1498Szrj return false;
909*38fd1498Szrj }
910*38fd1498Szrj
911*38fd1498Szrj
912*38fd1498Szrj /* Dump to FILE the names and associated events for all the active
913*38fd1498Szrj plugins. */
914*38fd1498Szrj
915*38fd1498Szrj DEBUG_FUNCTION void
dump_active_plugins(FILE * file)916*38fd1498Szrj dump_active_plugins (FILE *file)
917*38fd1498Szrj {
918*38fd1498Szrj int event;
919*38fd1498Szrj
920*38fd1498Szrj if (!plugins_active_p ())
921*38fd1498Szrj return;
922*38fd1498Szrj
923*38fd1498Szrj fprintf (file, FMT_FOR_PLUGIN_EVENT " | %s\n", _("Event"), _("Plugins"));
924*38fd1498Szrj for (event = PLUGIN_PASS_MANAGER_SETUP; event < event_last; event++)
925*38fd1498Szrj if (plugin_callbacks[event])
926*38fd1498Szrj {
927*38fd1498Szrj struct callback_info *ci;
928*38fd1498Szrj
929*38fd1498Szrj fprintf (file, FMT_FOR_PLUGIN_EVENT " |", plugin_event_name[event]);
930*38fd1498Szrj
931*38fd1498Szrj for (ci = plugin_callbacks[event]; ci; ci = ci->next)
932*38fd1498Szrj fprintf (file, " %s", ci->plugin_name);
933*38fd1498Szrj
934*38fd1498Szrj putc ('\n', file);
935*38fd1498Szrj }
936*38fd1498Szrj }
937*38fd1498Szrj
938*38fd1498Szrj
939*38fd1498Szrj /* Dump active plugins to stderr. */
940*38fd1498Szrj
941*38fd1498Szrj DEBUG_FUNCTION void
debug_active_plugins(void)942*38fd1498Szrj debug_active_plugins (void)
943*38fd1498Szrj {
944*38fd1498Szrj dump_active_plugins (stderr);
945*38fd1498Szrj }
946*38fd1498Szrj
947*38fd1498Szrj /* Give a warning if plugins are present, before an ICE message asking
948*38fd1498Szrj to submit a bug report. */
949*38fd1498Szrj
950*38fd1498Szrj void
warn_if_plugins(void)951*38fd1498Szrj warn_if_plugins (void)
952*38fd1498Szrj {
953*38fd1498Szrj if (plugins_active_p ())
954*38fd1498Szrj {
955*38fd1498Szrj fnotice (stderr, "*** WARNING *** there are active plugins, do not report"
956*38fd1498Szrj " this as a bug unless you can reproduce it without enabling"
957*38fd1498Szrj " any plugins.\n");
958*38fd1498Szrj dump_active_plugins (stderr);
959*38fd1498Szrj }
960*38fd1498Szrj
961*38fd1498Szrj }
962*38fd1498Szrj
963*38fd1498Szrj /* The default version check. Compares every field in VERSION. */
964*38fd1498Szrj
965*38fd1498Szrj bool
plugin_default_version_check(struct plugin_gcc_version * gcc_version,struct plugin_gcc_version * plugin_version)966*38fd1498Szrj plugin_default_version_check (struct plugin_gcc_version *gcc_version,
967*38fd1498Szrj struct plugin_gcc_version *plugin_version)
968*38fd1498Szrj {
969*38fd1498Szrj if (!gcc_version || !plugin_version)
970*38fd1498Szrj return false;
971*38fd1498Szrj
972*38fd1498Szrj if (strcmp (gcc_version->basever, plugin_version->basever))
973*38fd1498Szrj return false;
974*38fd1498Szrj if (strcmp (gcc_version->datestamp, plugin_version->datestamp))
975*38fd1498Szrj return false;
976*38fd1498Szrj if (strcmp (gcc_version->devphase, plugin_version->devphase))
977*38fd1498Szrj return false;
978*38fd1498Szrj if (strcmp (gcc_version->revision, plugin_version->revision))
979*38fd1498Szrj return false;
980*38fd1498Szrj if (strcmp (gcc_version->configuration_arguments,
981*38fd1498Szrj plugin_version->configuration_arguments))
982*38fd1498Szrj return false;
983*38fd1498Szrj return true;
984*38fd1498Szrj }
985*38fd1498Szrj
986*38fd1498Szrj
987*38fd1498Szrj /* Return the current value of event_last, so that plugins which provide
988*38fd1498Szrj additional functionality for events for the benefit of high-level plugins
989*38fd1498Szrj know how many valid entries plugin_event_name holds. */
990*38fd1498Szrj
991*38fd1498Szrj int
get_event_last(void)992*38fd1498Szrj get_event_last (void)
993*38fd1498Szrj {
994*38fd1498Szrj return event_last;
995*38fd1498Szrj }
996*38fd1498Szrj
997*38fd1498Szrj
998*38fd1498Szrj /* Retrieve the default plugin directory. The gcc driver should have passed
999*38fd1498Szrj it as -iplugindir <dir> to the cc1 program, and it is queriable through the
1000*38fd1498Szrj -print-file-name=plugin option to gcc. */
1001*38fd1498Szrj const char*
default_plugin_dir_name(void)1002*38fd1498Szrj default_plugin_dir_name (void)
1003*38fd1498Szrj {
1004*38fd1498Szrj if (!plugindir_string)
1005*38fd1498Szrj fatal_error (input_location,
1006*38fd1498Szrj "-iplugindir <dir> option not passed from the gcc driver");
1007*38fd1498Szrj return plugindir_string;
1008*38fd1498Szrj }
1009