1 /* Support for GCC plugin mechanism.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
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/>. */
20 /* This file contains the support for GCC plugin mechanism based on the
21 APIs described in doc/plugin.texi. */
26 /* If plugin support is not enabled, do not try to execute any code
27 that may reference libdl. The generic code is still compiled in to
28 avoid including too many conditional compilation paths in the rest
34 #include "coretypes.h"
37 #include "tree-pass.h"
44 #include "plugin-version.h"
47 /* Event names as strings. Keep in sync with enum plugin_event. */
48 const char *plugin_event_name
[] =
50 "PLUGIN_PASS_MANAGER_SETUP",
53 "PLUGIN_CXX_CP_PRE_GENERICIZE",
59 "PLUGIN_REGISTER_GGC_ROOTS",
60 "PLUGIN_REGISTER_GGC_CACHES",
65 /* Hash table for the plugin_name_args objects created during command-line
67 static htab_t plugin_name_args_tab
= NULL
;
69 /* List node for keeping track of plugin-registered callback. */
72 const char *plugin_name
; /* Name of plugin that registers the callback. */
73 plugin_callback_func func
; /* Callback to be called. */
74 void *user_data
; /* plugin-specified data. */
75 struct callback_info
*next
;
78 /* An array of lists of 'callback_info' objects indexed by the event id. */
79 static struct callback_info
*plugin_callbacks
[PLUGIN_EVENT_LAST
] = { NULL
};
83 /* Each plugin should define an initialization function with exactly
85 static const char *str_plugin_init_func_name
= "plugin_init";
87 /* Each plugin should define this symbol to assert that it is
88 distributed under a GPL-compatible license. */
89 static const char *str_license
= "plugin_is_GPL_compatible";
92 /* Helper function for the hash table that compares the base_name of the
93 existing entry (S1) with the given string (S2). */
96 htab_str_eq (const void *s1
, const void *s2
)
98 const struct plugin_name_args
*plugin
= (const struct plugin_name_args
*) s1
;
99 return !strcmp (plugin
->base_name
, (const char *) s2
);
103 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
107 get_plugin_base_name (const char *full_name
)
109 /* First get the base name part of the full-path name, i.e. NAME.so. */
110 char *base_name
= xstrdup (lbasename (full_name
));
112 /* Then get rid of '.so' part of the name. */
113 strip_off_ending (base_name
, strlen (base_name
));
119 /* Create a plugin_name_args object for the give plugin and insert it to
120 the hash table. This function is called when -fplugin=/path/to/NAME.so
121 option is processed. */
124 add_new_plugin (const char* plugin_name
)
126 struct plugin_name_args
*plugin
;
128 char *base_name
= get_plugin_base_name (plugin_name
);
130 /* If this is the first -fplugin= option we encounter, create
131 'plugin_name_args_tab' hash table. */
132 if (!plugin_name_args_tab
)
133 plugin_name_args_tab
= htab_create (10, htab_hash_string
, htab_str_eq
,
136 slot
= htab_find_slot (plugin_name_args_tab
, base_name
, INSERT
);
138 /* If the same plugin (name) has been specified earlier, either emit an
139 error or a warning message depending on if they have identical full
143 plugin
= (struct plugin_name_args
*) *slot
;
144 if (strcmp (plugin
->full_name
, plugin_name
))
145 error ("Plugin %s was specified with different paths:\n%s\n%s",
146 plugin
->base_name
, plugin
->full_name
, plugin_name
);
150 plugin
= XCNEW (struct plugin_name_args
);
151 plugin
->base_name
= base_name
;
152 plugin
->full_name
= plugin_name
;
158 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
159 'plugin_argument' object for the parsed key-value pair. ARG is
160 the <name>-<key>[=<value>] part of the option. */
163 parse_plugin_arg_opt (const char *arg
)
165 size_t len
= 0, name_len
= 0, key_len
= 0, value_len
= 0;
166 const char *ptr
, *name_start
= arg
, *key_start
= NULL
, *value_start
= NULL
;
167 char *name
, *key
, *value
;
169 bool name_parsed
= false, key_parsed
= false;
171 /* Iterate over the ARG string and identify the starting character position
172 of 'name', 'key', and 'value' and their lengths. */
173 for (ptr
= arg
; *ptr
; ++ptr
)
175 /* Only the first '-' encountered is considered a separator between
176 'name' and 'key'. All the subsequent '-'s are considered part of
177 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
178 the plugin name is 'foo' and the key is 'bar-primary-key'. */
179 if (*ptr
== '-' && !name_parsed
)
187 else if (*ptr
== '=')
191 error ("Malformed option -fplugin-arg-%s (multiple '=' signs)",
197 value_start
= ptr
+ 1;
207 error ("Malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
212 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
213 Otherwise, it is the VALUE_LEN. */
219 name
= XNEWVEC (char, name_len
+ 1);
220 strncpy (name
, name_start
, name_len
);
221 name
[name_len
] = '\0';
223 /* Check if the named plugin has already been specified earlier in the
225 if (plugin_name_args_tab
226 && ((slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
))
229 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
231 key
= XNEWVEC (char, key_len
+ 1);
232 strncpy (key
, key_start
, key_len
);
236 value
= XNEWVEC (char, value_len
+ 1);
237 strncpy (value
, value_start
, value_len
);
238 value
[value_len
] = '\0';
243 /* Create a plugin_argument object for the parsed key-value pair.
244 If there are already arguments for this plugin, we will need to
245 adjust the argument array size by creating a new array and deleting
246 the old one. If the performance ever becomes an issue, we can
247 change the code by pre-allocating a larger array first. */
248 if (plugin
->argc
> 0)
250 struct plugin_argument
*args
= XNEWVEC (struct plugin_argument
,
252 memcpy (args
, plugin
->argv
,
253 sizeof (struct plugin_argument
) * plugin
->argc
);
254 XDELETEVEC (plugin
->argv
);
260 gcc_assert (plugin
->argv
== NULL
);
261 plugin
->argv
= XNEWVEC (struct plugin_argument
, 1);
265 plugin
->argv
[plugin
->argc
- 1].key
= key
;
266 plugin
->argv
[plugin
->argc
- 1].value
= value
;
269 error ("Plugin %s should be specified before -fplugin-arg-%s "
270 "in the command line", name
, arg
);
272 /* We don't need the plugin's name anymore. Just release it. */
276 /* Register additional plugin information. NAME is the name passed to
277 plugin_init. INFO is the information that should be registered. */
280 register_plugin_info (const char* name
, struct plugin_info
*info
)
282 void **slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
);
283 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
284 plugin
->version
= info
->version
;
285 plugin
->help
= info
->help
;
288 /* Called from the plugin's initialization code. Register a single callback.
289 This function can be called multiple times.
291 PLUGIN_NAME - display name for this plugin
292 EVENT - which event the callback is for
293 CALLBACK - the callback to be called at the event
294 USER_DATA - plugin-provided data */
297 register_callback (const char *plugin_name
,
298 enum plugin_event event
,
299 plugin_callback_func callback
,
304 case PLUGIN_PASS_MANAGER_SETUP
:
305 gcc_assert (!callback
);
306 register_pass ((struct register_pass_info
*) user_data
);
309 gcc_assert (!callback
);
310 register_plugin_info (plugin_name
, (struct plugin_info
*) user_data
);
312 case PLUGIN_REGISTER_GGC_ROOTS
:
313 gcc_assert (!callback
);
314 ggc_register_root_tab ((const struct ggc_root_tab
*) user_data
);
316 case PLUGIN_REGISTER_GGC_CACHES
:
317 gcc_assert (!callback
);
318 ggc_register_cache_tab ((const struct ggc_cache_tab
*) user_data
);
320 case PLUGIN_FINISH_TYPE
:
321 case PLUGIN_START_UNIT
:
322 case PLUGIN_FINISH_UNIT
:
323 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
324 case PLUGIN_GGC_START
:
325 case PLUGIN_GGC_MARKING
:
327 case PLUGIN_ATTRIBUTES
:
330 struct callback_info
*new_callback
;
333 error ("Plugin %s registered a null callback function "
334 "for event %s", plugin_name
, plugin_event_name
[event
]);
337 new_callback
= XNEW (struct callback_info
);
338 new_callback
->plugin_name
= plugin_name
;
339 new_callback
->func
= callback
;
340 new_callback
->user_data
= user_data
;
341 new_callback
->next
= plugin_callbacks
[event
];
342 plugin_callbacks
[event
] = new_callback
;
345 case PLUGIN_EVENT_LAST
:
347 error ("Unkown callback event registered by plugin %s",
353 /* Called from inside GCC. Invoke all plug-in callbacks registered with
356 EVENT - the event identifier
357 GCC_DATA - event-specific data provided by the compiler */
360 invoke_plugin_callbacks (enum plugin_event event
, void *gcc_data
)
362 timevar_push (TV_PLUGIN_RUN
);
366 case PLUGIN_FINISH_TYPE
:
367 case PLUGIN_START_UNIT
:
368 case PLUGIN_FINISH_UNIT
:
369 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
370 case PLUGIN_ATTRIBUTES
:
372 case PLUGIN_GGC_START
:
373 case PLUGIN_GGC_MARKING
:
376 /* Iterate over every callback registered with this event and
378 struct callback_info
*callback
= plugin_callbacks
[event
];
379 for ( ; callback
; callback
= callback
->next
)
380 (*callback
->func
) (gcc_data
, callback
->user_data
);
384 case PLUGIN_PASS_MANAGER_SETUP
:
385 case PLUGIN_EVENT_LAST
:
386 case PLUGIN_REGISTER_GGC_ROOTS
:
387 case PLUGIN_REGISTER_GGC_CACHES
:
392 timevar_pop (TV_PLUGIN_RUN
);
396 /* We need a union to cast dlsym return value to a function pointer
397 as ISO C forbids assignment between function pointer and 'void *'.
398 Use explicit union instead of __extension__(<union_cast>) for
400 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
401 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
402 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
404 /* Try to initialize PLUGIN. Return true if successful. */
407 try_init_one_plugin (struct plugin_name_args
*plugin
)
410 plugin_init_func plugin_init
;
412 PTR_UNION_TYPE (plugin_init_func
) plugin_init_union
;
414 /* We use RTLD_NOW to accelerate binding and detect any mismatch
415 between the API expected by the plugin and the GCC API; we use
416 RTLD_GLOBAL which is useful to plugins which themselves call
418 dl_handle
= dlopen (plugin
->full_name
, RTLD_NOW
| RTLD_GLOBAL
);
421 error ("Cannot load plugin %s\n%s", plugin
->full_name
, dlerror ());
425 /* Clear any existing error. */
428 /* Check the plugin license. */
429 if (dlsym (dl_handle
, str_license
) == NULL
)
430 fatal_error ("plugin %s is not licensed under a GPL-compatible license\n"
431 "%s", plugin
->full_name
, dlerror ());
433 PTR_UNION_AS_VOID_PTR (plugin_init_union
) =
434 dlsym (dl_handle
, str_plugin_init_func_name
);
435 plugin_init
= PTR_UNION_AS_CAST_PTR (plugin_init_union
);
437 if ((err
= dlerror ()) != NULL
)
439 error ("Cannot find %s in plugin %s\n%s", str_plugin_init_func_name
,
440 plugin
->full_name
, err
);
444 /* Call the plugin-provided initialization routine with the arguments. */
445 if ((*plugin_init
) (plugin
, &gcc_version
))
447 error ("Fail to initialize plugin %s", plugin
->full_name
);
455 /* Routine to dlopen and initialize one plugin. This function is passed to
456 (and called by) the hash table traverse routine. Return 1 for the
457 htab_traverse to continue scan, 0 to stop.
459 SLOT - slot of the hash table element
460 INFO - auxiliary pointer handed to hash table traverse routine
461 (unused in this function) */
464 init_one_plugin (void **slot
, void * ARG_UNUSED (info
))
466 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
467 bool ok
= try_init_one_plugin (plugin
);
470 htab_remove_elt (plugin_name_args_tab
, plugin
->base_name
);
476 #endif /* ENABLE_PLUGIN */
478 /* Main plugin initialization function. Called from compile_file() in
482 initialize_plugins (void)
484 /* If no plugin was specified in the command-line, simply return. */
485 if (!plugin_name_args_tab
)
488 timevar_push (TV_PLUGIN_INIT
);
491 /* Traverse and initialize each plugin specified in the command-line. */
492 htab_traverse_noresize (plugin_name_args_tab
, init_one_plugin
, NULL
);
495 timevar_pop (TV_PLUGIN_INIT
);
498 /* Release memory used by one plugin. */
501 finalize_one_plugin (void **slot
, void * ARG_UNUSED (info
))
503 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
508 /* Free memory allocated by the plugin system. */
511 finalize_plugins (void)
513 if (!plugin_name_args_tab
)
516 /* We can now delete the plugin_name_args object as it will no longer
517 be used. Note that base_name and argv fields (both of which were also
518 dynamically allocated) are not freed as they could still be used by
521 htab_traverse_noresize (plugin_name_args_tab
, finalize_one_plugin
, NULL
);
523 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
524 htab_delete (plugin_name_args_tab
);
525 plugin_name_args_tab
= NULL
;
528 /* Used to pass options to htab_traverse callbacks. */
536 /* Print the version of one plugin. */
539 print_version_one_plugin (void **slot
, void *data
)
541 struct print_options
*opt
= (struct print_options
*) data
;
542 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
543 const char *version
= plugin
->version
? plugin
->version
: "Unknown version.";
545 fprintf (opt
->file
, " %s%s: %s\n", opt
->indent
, plugin
->base_name
, version
);
549 /* Print the version of each plugin. */
552 print_plugins_versions (FILE *file
, const char *indent
)
554 struct print_options opt
;
557 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
560 fprintf (file
, "%sVersions of loaded plugins:\n", indent
);
561 htab_traverse_noresize (plugin_name_args_tab
, print_version_one_plugin
, &opt
);
564 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
565 argument to htab_traverse_noresize. */
568 print_help_one_plugin (void **slot
, void *data
)
570 struct print_options
*opt
= (struct print_options
*) data
;
571 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
572 const char *help
= plugin
->help
? plugin
->help
: "No help available .";
574 char *dup
= xstrdup (help
);
576 fprintf (opt
->file
, " %s%s:\n", opt
->indent
, plugin
->base_name
);
578 for (p
= nl
= dup
; nl
; p
= nl
)
580 nl
= strchr (nl
, '\n');
586 fprintf (opt
->file
, " %s %s\n", opt
->indent
, p
);
593 /* Print help for each plugin. The output goes to FILE and every line starts
597 print_plugins_help (FILE *file
, const char *indent
)
599 struct print_options opt
;
602 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
605 fprintf (file
, "%sHelp for the loaded plugins:\n", indent
);
606 htab_traverse_noresize (plugin_name_args_tab
, print_help_one_plugin
, &opt
);
610 /* Return true if plugins have been loaded. */
613 plugins_active_p (void)
617 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< PLUGIN_EVENT_LAST
; event
++)
618 if (plugin_callbacks
[event
])
625 /* Dump to FILE the names and associated events for all the active
629 dump_active_plugins (FILE *file
)
633 if (!plugins_active_p ())
636 fprintf (stderr
, "Event\t\t\tPlugins\n");
637 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< PLUGIN_EVENT_LAST
; event
++)
638 if (plugin_callbacks
[event
])
640 struct callback_info
*ci
;
642 fprintf (file
, "%s\t", plugin_event_name
[event
]);
644 for (ci
= plugin_callbacks
[event
]; ci
; ci
= ci
->next
)
645 fprintf (file
, "%s ", ci
->plugin_name
);
647 fprintf (file
, "\n");
652 /* Dump active plugins to stderr. */
655 debug_active_plugins (void)
657 dump_active_plugins (stderr
);
660 /* The default version check. Compares every field in VERSION. */
663 plugin_default_version_check (struct plugin_gcc_version
*gcc_version
,
664 struct plugin_gcc_version
*plugin_version
)
666 if (!gcc_version
|| !plugin_version
)
669 if (strcmp (gcc_version
->basever
, plugin_version
->basever
))
671 if (strcmp (gcc_version
->datestamp
, plugin_version
->datestamp
))
673 if (strcmp (gcc_version
->devphase
, plugin_version
->devphase
))
675 if (strcmp (gcc_version
->revision
, plugin_version
->revision
))
677 if (strcmp (gcc_version
->configuration_arguments
,
678 plugin_version
->configuration_arguments
))