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 #define GCC_PLUGIN_STRINGIFY0(X) #X
48 #define GCC_PLUGIN_STRINGIFY1(X) GCC_PLUGIN_STRINGIFY0 (X)
50 /* Event names as strings. Keep in sync with enum plugin_event. */
51 static const char *plugin_event_name_init
[] =
53 # define DEFEVENT(NAME) GCC_PLUGIN_STRINGIFY1 (NAME),
54 # include "plugin.def"
58 /* A printf format large enough for the largest event above. */
59 #define FMT_FOR_PLUGIN_EVENT "%-32s"
61 const char **plugin_event_name
= plugin_event_name_init
;
63 /* A hash table to map event names to the position of the names in the
64 plugin_event_name table. */
65 static htab_t event_tab
;
67 /* Keep track of the limit of allocated events and space ready for
69 static int event_last
= PLUGIN_EVENT_FIRST_DYNAMIC
;
70 static int event_horizon
= PLUGIN_EVENT_FIRST_DYNAMIC
;
72 /* Hash table for the plugin_name_args objects created during command-line
74 static htab_t plugin_name_args_tab
= NULL
;
76 /* List node for keeping track of plugin-registered callback. */
79 const char *plugin_name
; /* Name of plugin that registers the callback. */
80 plugin_callback_func func
; /* Callback to be called. */
81 void *user_data
; /* plugin-specified data. */
82 struct callback_info
*next
;
85 /* An array of lists of 'callback_info' objects indexed by the event id. */
86 static struct callback_info
*plugin_callbacks_init
[PLUGIN_EVENT_FIRST_DYNAMIC
];
87 static struct callback_info
**plugin_callbacks
= plugin_callbacks_init
;
91 /* Each plugin should define an initialization function with exactly
93 static const char *str_plugin_init_func_name
= "plugin_init";
95 /* Each plugin should define this symbol to assert that it is
96 distributed under a GPL-compatible license. */
97 static const char *str_license
= "plugin_is_GPL_compatible";
100 /* Helper function for the hash table that compares the base_name of the
101 existing entry (S1) with the given string (S2). */
104 htab_str_eq (const void *s1
, const void *s2
)
106 const struct plugin_name_args
*plugin
= (const struct plugin_name_args
*) s1
;
107 return !strcmp (plugin
->base_name
, (const char *) s2
);
111 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
115 get_plugin_base_name (const char *full_name
)
117 /* First get the base name part of the full-path name, i.e. NAME.so. */
118 char *base_name
= xstrdup (lbasename (full_name
));
120 /* Then get rid of '.so' part of the name. */
121 strip_off_ending (base_name
, strlen (base_name
));
127 /* Create a plugin_name_args object for the give plugin and insert it to
128 the hash table. This function is called when -fplugin=/path/to/NAME.so
129 option is processed. */
132 add_new_plugin (const char* plugin_name
)
134 struct plugin_name_args
*plugin
;
136 char *base_name
= get_plugin_base_name (plugin_name
);
138 /* If this is the first -fplugin= option we encounter, create
139 'plugin_name_args_tab' hash table. */
140 if (!plugin_name_args_tab
)
141 plugin_name_args_tab
= htab_create (10, htab_hash_string
, htab_str_eq
,
144 slot
= htab_find_slot (plugin_name_args_tab
, base_name
, INSERT
);
146 /* If the same plugin (name) has been specified earlier, either emit an
147 error or a warning message depending on if they have identical full
151 plugin
= (struct plugin_name_args
*) *slot
;
152 if (strcmp (plugin
->full_name
, plugin_name
))
153 error ("Plugin %s was specified with different paths:\n%s\n%s",
154 plugin
->base_name
, plugin
->full_name
, plugin_name
);
158 plugin
= XCNEW (struct plugin_name_args
);
159 plugin
->base_name
= base_name
;
160 plugin
->full_name
= plugin_name
;
166 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
167 'plugin_argument' object for the parsed key-value pair. ARG is
168 the <name>-<key>[=<value>] part of the option. */
171 parse_plugin_arg_opt (const char *arg
)
173 size_t len
= 0, name_len
= 0, key_len
= 0, value_len
= 0;
174 const char *ptr
, *name_start
= arg
, *key_start
= NULL
, *value_start
= NULL
;
175 char *name
, *key
, *value
;
177 bool name_parsed
= false, key_parsed
= false;
179 /* Iterate over the ARG string and identify the starting character position
180 of 'name', 'key', and 'value' and their lengths. */
181 for (ptr
= arg
; *ptr
; ++ptr
)
183 /* Only the first '-' encountered is considered a separator between
184 'name' and 'key'. All the subsequent '-'s are considered part of
185 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
186 the plugin name is 'foo' and the key is 'bar-primary-key'. */
187 if (*ptr
== '-' && !name_parsed
)
195 else if (*ptr
== '=')
199 error ("Malformed option -fplugin-arg-%s (multiple '=' signs)",
205 value_start
= ptr
+ 1;
215 error ("Malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
220 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
221 Otherwise, it is the VALUE_LEN. */
227 name
= XNEWVEC (char, name_len
+ 1);
228 strncpy (name
, name_start
, name_len
);
229 name
[name_len
] = '\0';
231 /* Check if the named plugin has already been specified earlier in the
233 if (plugin_name_args_tab
234 && ((slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
))
237 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
239 key
= XNEWVEC (char, key_len
+ 1);
240 strncpy (key
, key_start
, key_len
);
244 value
= XNEWVEC (char, value_len
+ 1);
245 strncpy (value
, value_start
, value_len
);
246 value
[value_len
] = '\0';
251 /* Create a plugin_argument object for the parsed key-value pair.
252 If there are already arguments for this plugin, we will need to
253 adjust the argument array size by creating a new array and deleting
254 the old one. If the performance ever becomes an issue, we can
255 change the code by pre-allocating a larger array first. */
256 if (plugin
->argc
> 0)
258 struct plugin_argument
*args
= XNEWVEC (struct plugin_argument
,
260 memcpy (args
, plugin
->argv
,
261 sizeof (struct plugin_argument
) * plugin
->argc
);
262 XDELETEVEC (plugin
->argv
);
268 gcc_assert (plugin
->argv
== NULL
);
269 plugin
->argv
= XNEWVEC (struct plugin_argument
, 1);
273 plugin
->argv
[plugin
->argc
- 1].key
= key
;
274 plugin
->argv
[plugin
->argc
- 1].value
= value
;
277 error ("Plugin %s should be specified before -fplugin-arg-%s "
278 "in the command line", name
, arg
);
280 /* We don't need the plugin's name anymore. Just release it. */
284 /* Register additional plugin information. NAME is the name passed to
285 plugin_init. INFO is the information that should be registered. */
288 register_plugin_info (const char* name
, struct plugin_info
*info
)
290 void **slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
);
291 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
292 plugin
->version
= info
->version
;
293 plugin
->help
= info
->help
;
296 /* Helper function for the event hash table that compares the name of an
297 existing entry (E1) with the given string (S2). */
300 htab_event_eq (const void *e1
, const void *s2
)
302 const char *s1
= *(const char * const *) e1
;
303 return !strcmp (s1
, (const char *) s2
);
306 /* Look up the event id for NAME. If the name is not found, return -1
307 if INSERT is NO_INSERT. */
310 get_named_event_id (const char *name
, enum insert_option insert
)
318 event_tab
= htab_create (150, htab_hash_string
, htab_event_eq
, NULL
);
319 for (i
= 0; i
< PLUGIN_EVENT_FIRST_DYNAMIC
; i
++)
321 slot
= htab_find_slot (event_tab
, plugin_event_name
[i
], INSERT
);
322 gcc_assert (*slot
== HTAB_EMPTY_ENTRY
);
323 *slot
= &plugin_event_name
[i
];
326 slot
= htab_find_slot (event_tab
, name
, insert
);
329 if (*slot
!= HTAB_EMPTY_ENTRY
)
330 return (const char **) *slot
- &plugin_event_name
[0];
332 if (event_last
>= event_horizon
)
334 event_horizon
= event_last
* 2;
335 if (plugin_event_name
== plugin_event_name_init
)
337 plugin_event_name
= XNEWVEC (const char *, event_horizon
);
338 memcpy (plugin_event_name
, plugin_event_name_init
,
339 sizeof plugin_event_name_init
);
340 plugin_callbacks
= XNEWVEC (struct callback_info
*, event_horizon
);
341 memcpy (plugin_callbacks
, plugin_callbacks_init
,
342 sizeof plugin_callbacks_init
);
347 = XRESIZEVEC (const char *, plugin_event_name
, event_horizon
);
348 plugin_callbacks
= XRESIZEVEC (struct callback_info
*,
349 plugin_callbacks
, event_horizon
);
351 /* All the pointers in the hash table will need to be updated. */
352 htab_delete (event_tab
);
356 *slot
= &plugin_event_name
[event_last
];
357 plugin_event_name
[event_last
] = name
;
361 /* Called from the plugin's initialization code. Register a single callback.
362 This function can be called multiple times.
364 PLUGIN_NAME - display name for this plugin
365 EVENT - which event the callback is for
366 CALLBACK - the callback to be called at the event
367 USER_DATA - plugin-provided data */
370 register_callback (const char *plugin_name
,
372 plugin_callback_func callback
,
377 case PLUGIN_PASS_MANAGER_SETUP
:
378 gcc_assert (!callback
);
379 register_pass ((struct register_pass_info
*) user_data
);
382 gcc_assert (!callback
);
383 register_plugin_info (plugin_name
, (struct plugin_info
*) user_data
);
385 case PLUGIN_REGISTER_GGC_ROOTS
:
386 gcc_assert (!callback
);
387 ggc_register_root_tab ((const struct ggc_root_tab
*) user_data
);
389 case PLUGIN_REGISTER_GGC_CACHES
:
390 gcc_assert (!callback
);
391 ggc_register_cache_tab ((const struct ggc_cache_tab
*) user_data
);
393 case PLUGIN_EVENT_FIRST_DYNAMIC
:
395 if (event
< PLUGIN_EVENT_FIRST_DYNAMIC
|| event
>= event_last
)
397 error ("Unknown callback event registered by plugin %s",
402 case PLUGIN_FINISH_TYPE
:
403 case PLUGIN_START_UNIT
:
404 case PLUGIN_FINISH_UNIT
:
405 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
406 case PLUGIN_GGC_START
:
407 case PLUGIN_GGC_MARKING
:
409 case PLUGIN_ATTRIBUTES
:
412 case PLUGIN_ALL_PASSES_START
:
413 case PLUGIN_ALL_PASSES_END
:
414 case PLUGIN_ALL_IPA_PASSES_START
:
415 case PLUGIN_ALL_IPA_PASSES_END
:
416 case PLUGIN_OVERRIDE_GATE
:
417 case PLUGIN_PASS_EXECUTION
:
418 case PLUGIN_EARLY_GIMPLE_PASSES_START
:
419 case PLUGIN_EARLY_GIMPLE_PASSES_END
:
420 case PLUGIN_NEW_PASS
:
422 struct callback_info
*new_callback
;
425 error ("Plugin %s registered a null callback function "
426 "for event %s", plugin_name
, plugin_event_name
[event
]);
429 new_callback
= XNEW (struct callback_info
);
430 new_callback
->plugin_name
= plugin_name
;
431 new_callback
->func
= callback
;
432 new_callback
->user_data
= user_data
;
433 new_callback
->next
= plugin_callbacks
[event
];
434 plugin_callbacks
[event
] = new_callback
;
440 /* Remove a callback for EVENT which has been registered with for a plugin
441 PLUGIN_NAME. Return PLUGEVT_SUCCESS if a matching callback was
442 found & removed, PLUGEVT_NO_CALLBACK if the event does not have a matching
443 callback, and PLUGEVT_NO_SUCH_EVENT if EVENT is invalid. */
445 unregister_callback (const char *plugin_name
, int event
)
447 struct callback_info
*callback
, **cbp
;
449 if (event
>= event_last
)
450 return PLUGEVT_NO_SUCH_EVENT
;
452 for (cbp
= &plugin_callbacks
[event
]; (callback
= *cbp
); cbp
= &callback
->next
)
453 if (strcmp (callback
->plugin_name
, plugin_name
) == 0)
455 *cbp
= callback
->next
;
456 return PLUGEVT_SUCCESS
;
458 return PLUGEVT_NO_CALLBACK
;
461 /* Called from inside GCC. Invoke all plug-in callbacks registered with
463 Return PLUGEVT_SUCCESS if at least one callback was called,
464 PLUGEVT_NO_CALLBACK if there was no callback.
466 EVENT - the event identifier
467 GCC_DATA - event-specific data provided by the compiler */
470 invoke_plugin_callbacks (int event
, void *gcc_data
)
472 int retval
= PLUGEVT_SUCCESS
;
474 timevar_push (TV_PLUGIN_RUN
);
478 case PLUGIN_EVENT_FIRST_DYNAMIC
:
480 gcc_assert (event
>= PLUGIN_EVENT_FIRST_DYNAMIC
);
481 gcc_assert (event
< event_last
);
483 case PLUGIN_FINISH_TYPE
:
484 case PLUGIN_START_UNIT
:
485 case PLUGIN_FINISH_UNIT
:
486 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
487 case PLUGIN_ATTRIBUTES
:
490 case PLUGIN_GGC_START
:
491 case PLUGIN_GGC_MARKING
:
493 case PLUGIN_ALL_PASSES_START
:
494 case PLUGIN_ALL_PASSES_END
:
495 case PLUGIN_ALL_IPA_PASSES_START
:
496 case PLUGIN_ALL_IPA_PASSES_END
:
497 case PLUGIN_OVERRIDE_GATE
:
498 case PLUGIN_PASS_EXECUTION
:
499 case PLUGIN_EARLY_GIMPLE_PASSES_START
:
500 case PLUGIN_EARLY_GIMPLE_PASSES_END
:
501 case PLUGIN_NEW_PASS
:
503 /* Iterate over every callback registered with this event and
505 struct callback_info
*callback
= plugin_callbacks
[event
];
508 retval
= PLUGEVT_NO_CALLBACK
;
509 for ( ; callback
; callback
= callback
->next
)
510 (*callback
->func
) (gcc_data
, callback
->user_data
);
514 case PLUGIN_PASS_MANAGER_SETUP
:
515 case PLUGIN_REGISTER_GGC_ROOTS
:
516 case PLUGIN_REGISTER_GGC_CACHES
:
520 timevar_pop (TV_PLUGIN_RUN
);
525 /* We need a union to cast dlsym return value to a function pointer
526 as ISO C forbids assignment between function pointer and 'void *'.
527 Use explicit union instead of __extension__(<union_cast>) for
529 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
530 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
531 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
533 /* Try to initialize PLUGIN. Return true if successful. */
536 try_init_one_plugin (struct plugin_name_args
*plugin
)
539 plugin_init_func plugin_init
;
541 PTR_UNION_TYPE (plugin_init_func
) plugin_init_union
;
543 /* We use RTLD_NOW to accelerate binding and detect any mismatch
544 between the API expected by the plugin and the GCC API; we use
545 RTLD_GLOBAL which is useful to plugins which themselves call
547 dl_handle
= dlopen (plugin
->full_name
, RTLD_NOW
| RTLD_GLOBAL
);
550 error ("Cannot load plugin %s\n%s", plugin
->full_name
, dlerror ());
554 /* Clear any existing error. */
557 /* Check the plugin license. */
558 if (dlsym (dl_handle
, str_license
) == NULL
)
559 fatal_error ("plugin %s is not licensed under a GPL-compatible license\n"
560 "%s", plugin
->full_name
, dlerror ());
562 PTR_UNION_AS_VOID_PTR (plugin_init_union
) =
563 dlsym (dl_handle
, str_plugin_init_func_name
);
564 plugin_init
= PTR_UNION_AS_CAST_PTR (plugin_init_union
);
566 if ((err
= dlerror ()) != NULL
)
568 error ("Cannot find %s in plugin %s\n%s", str_plugin_init_func_name
,
569 plugin
->full_name
, err
);
573 /* Call the plugin-provided initialization routine with the arguments. */
574 if ((*plugin_init
) (plugin
, &gcc_version
))
576 error ("Fail to initialize plugin %s", plugin
->full_name
);
584 /* Routine to dlopen and initialize one plugin. This function is passed to
585 (and called by) the hash table traverse routine. Return 1 for the
586 htab_traverse to continue scan, 0 to stop.
588 SLOT - slot of the hash table element
589 INFO - auxiliary pointer handed to hash table traverse routine
590 (unused in this function) */
593 init_one_plugin (void **slot
, void * ARG_UNUSED (info
))
595 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
596 bool ok
= try_init_one_plugin (plugin
);
599 htab_remove_elt (plugin_name_args_tab
, plugin
->base_name
);
605 #endif /* ENABLE_PLUGIN */
607 /* Main plugin initialization function. Called from compile_file() in
611 initialize_plugins (void)
613 /* If no plugin was specified in the command-line, simply return. */
614 if (!plugin_name_args_tab
)
617 timevar_push (TV_PLUGIN_INIT
);
620 /* Traverse and initialize each plugin specified in the command-line. */
621 htab_traverse_noresize (plugin_name_args_tab
, init_one_plugin
, NULL
);
624 timevar_pop (TV_PLUGIN_INIT
);
627 /* Release memory used by one plugin. */
630 finalize_one_plugin (void **slot
, void * ARG_UNUSED (info
))
632 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
637 /* Free memory allocated by the plugin system. */
640 finalize_plugins (void)
642 if (!plugin_name_args_tab
)
645 /* We can now delete the plugin_name_args object as it will no longer
646 be used. Note that base_name and argv fields (both of which were also
647 dynamically allocated) are not freed as they could still be used by
650 htab_traverse_noresize (plugin_name_args_tab
, finalize_one_plugin
, NULL
);
652 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
653 htab_delete (plugin_name_args_tab
);
654 plugin_name_args_tab
= NULL
;
657 /* Used to pass options to htab_traverse callbacks. */
665 /* Print the version of one plugin. */
668 print_version_one_plugin (void **slot
, void *data
)
670 struct print_options
*opt
= (struct print_options
*) data
;
671 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
672 const char *version
= plugin
->version
? plugin
->version
: "Unknown version.";
674 fprintf (opt
->file
, " %s%s: %s\n", opt
->indent
, plugin
->base_name
, version
);
678 /* Print the version of each plugin. */
681 print_plugins_versions (FILE *file
, const char *indent
)
683 struct print_options opt
;
686 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
689 fprintf (file
, "%sVersions of loaded plugins:\n", indent
);
690 htab_traverse_noresize (plugin_name_args_tab
, print_version_one_plugin
, &opt
);
693 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
694 argument to htab_traverse_noresize. */
697 print_help_one_plugin (void **slot
, void *data
)
699 struct print_options
*opt
= (struct print_options
*) data
;
700 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
701 const char *help
= plugin
->help
? plugin
->help
: "No help available .";
703 char *dup
= xstrdup (help
);
705 fprintf (opt
->file
, " %s%s:\n", opt
->indent
, plugin
->base_name
);
707 for (p
= nl
= dup
; nl
; p
= nl
)
709 nl
= strchr (nl
, '\n');
715 fprintf (opt
->file
, " %s %s\n", opt
->indent
, p
);
722 /* Print help for each plugin. The output goes to FILE and every line starts
726 print_plugins_help (FILE *file
, const char *indent
)
728 struct print_options opt
;
731 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
734 fprintf (file
, "%sHelp for the loaded plugins:\n", indent
);
735 htab_traverse_noresize (plugin_name_args_tab
, print_help_one_plugin
, &opt
);
739 /* Return true if plugins have been loaded. */
742 plugins_active_p (void)
746 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< event_last
; event
++)
747 if (plugin_callbacks
[event
])
754 /* Dump to FILE the names and associated events for all the active
758 dump_active_plugins (FILE *file
)
762 if (!plugins_active_p ())
765 fprintf (file
, FMT_FOR_PLUGIN_EVENT
" | %s\n", _("Event"), _("Plugins"));
766 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< event_last
; event
++)
767 if (plugin_callbacks
[event
])
769 struct callback_info
*ci
;
771 fprintf (file
, FMT_FOR_PLUGIN_EVENT
" |", plugin_event_name
[event
]);
773 for (ci
= plugin_callbacks
[event
]; ci
; ci
= ci
->next
)
774 fprintf (file
, " %s", ci
->plugin_name
);
781 /* Dump active plugins to stderr. */
784 debug_active_plugins (void)
786 dump_active_plugins (stderr
);
789 /* The default version check. Compares every field in VERSION. */
792 plugin_default_version_check (struct plugin_gcc_version
*gcc_version
,
793 struct plugin_gcc_version
*plugin_version
)
795 if (!gcc_version
|| !plugin_version
)
798 if (strcmp (gcc_version
->basever
, plugin_version
->basever
))
800 if (strcmp (gcc_version
->datestamp
, plugin_version
->datestamp
))
802 if (strcmp (gcc_version
->devphase
, plugin_version
->devphase
))
804 if (strcmp (gcc_version
->revision
, plugin_version
->revision
))
806 if (strcmp (gcc_version
->configuration_arguments
,
807 plugin_version
->configuration_arguments
))
812 /* Return the current value of event_last, so that plugins which provide
813 additional functionality for events for the benefit of high-level plugins
814 know how many valid entries plugin_event_name holds. */
817 get_event_last (void)