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",
67 /* Hash table for the plugin_name_args objects created during command-line
69 static htab_t plugin_name_args_tab
= NULL
;
71 /* List node for keeping track of plugin-registered callback. */
74 const char *plugin_name
; /* Name of plugin that registers the callback. */
75 plugin_callback_func func
; /* Callback to be called. */
76 void *user_data
; /* plugin-specified data. */
77 struct callback_info
*next
;
80 /* An array of lists of 'callback_info' objects indexed by the event id. */
81 static struct callback_info
*plugin_callbacks
[PLUGIN_EVENT_LAST
] = { NULL
};
85 /* Each plugin should define an initialization function with exactly
87 static const char *str_plugin_init_func_name
= "plugin_init";
89 /* Each plugin should define this symbol to assert that it is
90 distributed under a GPL-compatible license. */
91 static const char *str_license
= "plugin_is_GPL_compatible";
94 /* Helper function for the hash table that compares the base_name of the
95 existing entry (S1) with the given string (S2). */
98 htab_str_eq (const void *s1
, const void *s2
)
100 const struct plugin_name_args
*plugin
= (const struct plugin_name_args
*) s1
;
101 return !strcmp (plugin
->base_name
, (const char *) s2
);
105 /* Given a plugin's full-path name FULL_NAME, e.g. /pass/to/NAME.so,
109 get_plugin_base_name (const char *full_name
)
111 /* First get the base name part of the full-path name, i.e. NAME.so. */
112 char *base_name
= xstrdup (lbasename (full_name
));
114 /* Then get rid of '.so' part of the name. */
115 strip_off_ending (base_name
, strlen (base_name
));
121 /* Create a plugin_name_args object for the give plugin and insert it to
122 the hash table. This function is called when -fplugin=/path/to/NAME.so
123 option is processed. */
126 add_new_plugin (const char* plugin_name
)
128 struct plugin_name_args
*plugin
;
130 char *base_name
= get_plugin_base_name (plugin_name
);
132 /* If this is the first -fplugin= option we encounter, create
133 'plugin_name_args_tab' hash table. */
134 if (!plugin_name_args_tab
)
135 plugin_name_args_tab
= htab_create (10, htab_hash_string
, htab_str_eq
,
138 slot
= htab_find_slot (plugin_name_args_tab
, base_name
, INSERT
);
140 /* If the same plugin (name) has been specified earlier, either emit an
141 error or a warning message depending on if they have identical full
145 plugin
= (struct plugin_name_args
*) *slot
;
146 if (strcmp (plugin
->full_name
, plugin_name
))
147 error ("Plugin %s was specified with different paths:\n%s\n%s",
148 plugin
->base_name
, plugin
->full_name
, plugin_name
);
152 plugin
= XCNEW (struct plugin_name_args
);
153 plugin
->base_name
= base_name
;
154 plugin
->full_name
= plugin_name
;
160 /* Parse the -fplugin-arg-<name>-<key>[=<value>] option and create a
161 'plugin_argument' object for the parsed key-value pair. ARG is
162 the <name>-<key>[=<value>] part of the option. */
165 parse_plugin_arg_opt (const char *arg
)
167 size_t len
= 0, name_len
= 0, key_len
= 0, value_len
= 0;
168 const char *ptr
, *name_start
= arg
, *key_start
= NULL
, *value_start
= NULL
;
169 char *name
, *key
, *value
;
171 bool name_parsed
= false, key_parsed
= false;
173 /* Iterate over the ARG string and identify the starting character position
174 of 'name', 'key', and 'value' and their lengths. */
175 for (ptr
= arg
; *ptr
; ++ptr
)
177 /* Only the first '-' encountered is considered a separator between
178 'name' and 'key'. All the subsequent '-'s are considered part of
179 'key'. For example, given -fplugin-arg-foo-bar-primary-key=value,
180 the plugin name is 'foo' and the key is 'bar-primary-key'. */
181 if (*ptr
== '-' && !name_parsed
)
189 else if (*ptr
== '=')
193 error ("Malformed option -fplugin-arg-%s (multiple '=' signs)",
199 value_start
= ptr
+ 1;
209 error ("Malformed option -fplugin-arg-%s (missing -<key>[=<value>])",
214 /* If the option doesn't contain the 'value' part, LEN is the KEY_LEN.
215 Otherwise, it is the VALUE_LEN. */
221 name
= XNEWVEC (char, name_len
+ 1);
222 strncpy (name
, name_start
, name_len
);
223 name
[name_len
] = '\0';
225 /* Check if the named plugin has already been specified earlier in the
227 if (plugin_name_args_tab
228 && ((slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
))
231 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
233 key
= XNEWVEC (char, key_len
+ 1);
234 strncpy (key
, key_start
, key_len
);
238 value
= XNEWVEC (char, value_len
+ 1);
239 strncpy (value
, value_start
, value_len
);
240 value
[value_len
] = '\0';
245 /* Create a plugin_argument object for the parsed key-value pair.
246 If there are already arguments for this plugin, we will need to
247 adjust the argument array size by creating a new array and deleting
248 the old one. If the performance ever becomes an issue, we can
249 change the code by pre-allocating a larger array first. */
250 if (plugin
->argc
> 0)
252 struct plugin_argument
*args
= XNEWVEC (struct plugin_argument
,
254 memcpy (args
, plugin
->argv
,
255 sizeof (struct plugin_argument
) * plugin
->argc
);
256 XDELETEVEC (plugin
->argv
);
262 gcc_assert (plugin
->argv
== NULL
);
263 plugin
->argv
= XNEWVEC (struct plugin_argument
, 1);
267 plugin
->argv
[plugin
->argc
- 1].key
= key
;
268 plugin
->argv
[plugin
->argc
- 1].value
= value
;
271 error ("Plugin %s should be specified before -fplugin-arg-%s "
272 "in the command line", name
, arg
);
274 /* We don't need the plugin's name anymore. Just release it. */
278 /* Register additional plugin information. NAME is the name passed to
279 plugin_init. INFO is the information that should be registered. */
282 register_plugin_info (const char* name
, struct plugin_info
*info
)
284 void **slot
= htab_find_slot (plugin_name_args_tab
, name
, NO_INSERT
);
285 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
286 plugin
->version
= info
->version
;
287 plugin
->help
= info
->help
;
290 /* Called from the plugin's initialization code. Register a single callback.
291 This function can be called multiple times.
293 PLUGIN_NAME - display name for this plugin
294 EVENT - which event the callback is for
295 CALLBACK - the callback to be called at the event
296 USER_DATA - plugin-provided data */
299 register_callback (const char *plugin_name
,
300 enum plugin_event event
,
301 plugin_callback_func callback
,
306 case PLUGIN_PASS_MANAGER_SETUP
:
307 gcc_assert (!callback
);
308 register_pass ((struct register_pass_info
*) user_data
);
311 gcc_assert (!callback
);
312 register_plugin_info (plugin_name
, (struct plugin_info
*) user_data
);
314 case PLUGIN_REGISTER_GGC_ROOTS
:
315 gcc_assert (!callback
);
316 ggc_register_root_tab ((const struct ggc_root_tab
*) user_data
);
318 case PLUGIN_REGISTER_GGC_CACHES
:
319 gcc_assert (!callback
);
320 ggc_register_cache_tab ((const struct ggc_cache_tab
*) user_data
);
322 case PLUGIN_FINISH_TYPE
:
323 case PLUGIN_START_UNIT
:
324 case PLUGIN_FINISH_UNIT
:
325 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
326 case PLUGIN_GGC_START
:
327 case PLUGIN_GGC_MARKING
:
329 case PLUGIN_ATTRIBUTES
:
333 struct callback_info
*new_callback
;
336 error ("Plugin %s registered a null callback function "
337 "for event %s", plugin_name
, plugin_event_name
[event
]);
340 new_callback
= XNEW (struct callback_info
);
341 new_callback
->plugin_name
= plugin_name
;
342 new_callback
->func
= callback
;
343 new_callback
->user_data
= user_data
;
344 new_callback
->next
= plugin_callbacks
[event
];
345 plugin_callbacks
[event
] = new_callback
;
348 case PLUGIN_EVENT_LAST
:
350 error ("Unknown callback event registered by plugin %s",
356 /* Called from inside GCC. Invoke all plug-in callbacks registered with
359 EVENT - the event identifier
360 GCC_DATA - event-specific data provided by the compiler */
363 invoke_plugin_callbacks (enum plugin_event event
, void *gcc_data
)
365 timevar_push (TV_PLUGIN_RUN
);
369 case PLUGIN_FINISH_TYPE
:
370 case PLUGIN_START_UNIT
:
371 case PLUGIN_FINISH_UNIT
:
372 case PLUGIN_CXX_CP_PRE_GENERICIZE
:
373 case PLUGIN_ATTRIBUTES
:
376 case PLUGIN_GGC_START
:
377 case PLUGIN_GGC_MARKING
:
380 /* Iterate over every callback registered with this event and
382 struct callback_info
*callback
= plugin_callbacks
[event
];
383 for ( ; callback
; callback
= callback
->next
)
384 (*callback
->func
) (gcc_data
, callback
->user_data
);
388 case PLUGIN_PASS_MANAGER_SETUP
:
389 case PLUGIN_EVENT_LAST
:
390 case PLUGIN_REGISTER_GGC_ROOTS
:
391 case PLUGIN_REGISTER_GGC_CACHES
:
396 timevar_pop (TV_PLUGIN_RUN
);
400 /* We need a union to cast dlsym return value to a function pointer
401 as ISO C forbids assignment between function pointer and 'void *'.
402 Use explicit union instead of __extension__(<union_cast>) for
404 #define PTR_UNION_TYPE(TOTYPE) union { void *_q; TOTYPE _nq; }
405 #define PTR_UNION_AS_VOID_PTR(NAME) (NAME._q)
406 #define PTR_UNION_AS_CAST_PTR(NAME) (NAME._nq)
408 /* Try to initialize PLUGIN. Return true if successful. */
411 try_init_one_plugin (struct plugin_name_args
*plugin
)
414 plugin_init_func plugin_init
;
416 PTR_UNION_TYPE (plugin_init_func
) plugin_init_union
;
418 /* We use RTLD_NOW to accelerate binding and detect any mismatch
419 between the API expected by the plugin and the GCC API; we use
420 RTLD_GLOBAL which is useful to plugins which themselves call
422 dl_handle
= dlopen (plugin
->full_name
, RTLD_NOW
| RTLD_GLOBAL
);
425 error ("Cannot load plugin %s\n%s", plugin
->full_name
, dlerror ());
429 /* Clear any existing error. */
432 /* Check the plugin license. */
433 if (dlsym (dl_handle
, str_license
) == NULL
)
434 fatal_error ("plugin %s is not licensed under a GPL-compatible license\n"
435 "%s", plugin
->full_name
, dlerror ());
437 PTR_UNION_AS_VOID_PTR (plugin_init_union
) =
438 dlsym (dl_handle
, str_plugin_init_func_name
);
439 plugin_init
= PTR_UNION_AS_CAST_PTR (plugin_init_union
);
441 if ((err
= dlerror ()) != NULL
)
443 error ("Cannot find %s in plugin %s\n%s", str_plugin_init_func_name
,
444 plugin
->full_name
, err
);
448 /* Call the plugin-provided initialization routine with the arguments. */
449 if ((*plugin_init
) (plugin
, &gcc_version
))
451 error ("Fail to initialize plugin %s", plugin
->full_name
);
459 /* Routine to dlopen and initialize one plugin. This function is passed to
460 (and called by) the hash table traverse routine. Return 1 for the
461 htab_traverse to continue scan, 0 to stop.
463 SLOT - slot of the hash table element
464 INFO - auxiliary pointer handed to hash table traverse routine
465 (unused in this function) */
468 init_one_plugin (void **slot
, void * ARG_UNUSED (info
))
470 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
471 bool ok
= try_init_one_plugin (plugin
);
474 htab_remove_elt (plugin_name_args_tab
, plugin
->base_name
);
480 #endif /* ENABLE_PLUGIN */
482 /* Main plugin initialization function. Called from compile_file() in
486 initialize_plugins (void)
488 /* If no plugin was specified in the command-line, simply return. */
489 if (!plugin_name_args_tab
)
492 timevar_push (TV_PLUGIN_INIT
);
495 /* Traverse and initialize each plugin specified in the command-line. */
496 htab_traverse_noresize (plugin_name_args_tab
, init_one_plugin
, NULL
);
499 timevar_pop (TV_PLUGIN_INIT
);
502 /* Release memory used by one plugin. */
505 finalize_one_plugin (void **slot
, void * ARG_UNUSED (info
))
507 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
512 /* Free memory allocated by the plugin system. */
515 finalize_plugins (void)
517 if (!plugin_name_args_tab
)
520 /* We can now delete the plugin_name_args object as it will no longer
521 be used. Note that base_name and argv fields (both of which were also
522 dynamically allocated) are not freed as they could still be used by
525 htab_traverse_noresize (plugin_name_args_tab
, finalize_one_plugin
, NULL
);
527 /* PLUGIN_NAME_ARGS_TAB is no longer needed, just delete it. */
528 htab_delete (plugin_name_args_tab
);
529 plugin_name_args_tab
= NULL
;
532 /* Used to pass options to htab_traverse callbacks. */
540 /* Print the version of one plugin. */
543 print_version_one_plugin (void **slot
, void *data
)
545 struct print_options
*opt
= (struct print_options
*) data
;
546 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
547 const char *version
= plugin
->version
? plugin
->version
: "Unknown version.";
549 fprintf (opt
->file
, " %s%s: %s\n", opt
->indent
, plugin
->base_name
, version
);
553 /* Print the version of each plugin. */
556 print_plugins_versions (FILE *file
, const char *indent
)
558 struct print_options opt
;
561 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
564 fprintf (file
, "%sVersions of loaded plugins:\n", indent
);
565 htab_traverse_noresize (plugin_name_args_tab
, print_version_one_plugin
, &opt
);
568 /* Print help for one plugin. SLOT is the hash table slot. DATA is the
569 argument to htab_traverse_noresize. */
572 print_help_one_plugin (void **slot
, void *data
)
574 struct print_options
*opt
= (struct print_options
*) data
;
575 struct plugin_name_args
*plugin
= (struct plugin_name_args
*) *slot
;
576 const char *help
= plugin
->help
? plugin
->help
: "No help available .";
578 char *dup
= xstrdup (help
);
580 fprintf (opt
->file
, " %s%s:\n", opt
->indent
, plugin
->base_name
);
582 for (p
= nl
= dup
; nl
; p
= nl
)
584 nl
= strchr (nl
, '\n');
590 fprintf (opt
->file
, " %s %s\n", opt
->indent
, p
);
597 /* Print help for each plugin. The output goes to FILE and every line starts
601 print_plugins_help (FILE *file
, const char *indent
)
603 struct print_options opt
;
606 if (!plugin_name_args_tab
|| htab_elements (plugin_name_args_tab
) == 0)
609 fprintf (file
, "%sHelp for the loaded plugins:\n", indent
);
610 htab_traverse_noresize (plugin_name_args_tab
, print_help_one_plugin
, &opt
);
614 /* Return true if plugins have been loaded. */
617 plugins_active_p (void)
621 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< PLUGIN_EVENT_LAST
; event
++)
622 if (plugin_callbacks
[event
])
629 /* Dump to FILE the names and associated events for all the active
633 dump_active_plugins (FILE *file
)
637 if (!plugins_active_p ())
640 fprintf (stderr
, "Event\t\t\tPlugins\n");
641 for (event
= PLUGIN_PASS_MANAGER_SETUP
; event
< PLUGIN_EVENT_LAST
; event
++)
642 if (plugin_callbacks
[event
])
644 struct callback_info
*ci
;
646 fprintf (file
, "%s\t", plugin_event_name
[event
]);
648 for (ci
= plugin_callbacks
[event
]; ci
; ci
= ci
->next
)
649 fprintf (file
, "%s ", ci
->plugin_name
);
651 fprintf (file
, "\n");
656 /* Dump active plugins to stderr. */
659 debug_active_plugins (void)
661 dump_active_plugins (stderr
);
664 /* The default version check. Compares every field in VERSION. */
667 plugin_default_version_check (struct plugin_gcc_version
*gcc_version
,
668 struct plugin_gcc_version
*plugin_version
)
670 if (!gcc_version
|| !plugin_version
)
673 if (strcmp (gcc_version
->basever
, plugin_version
->basever
))
675 if (strcmp (gcc_version
->datestamp
, plugin_version
->datestamp
))
677 if (strcmp (gcc_version
->devphase
, plugin_version
->devphase
))
679 if (strcmp (gcc_version
->revision
, plugin_version
->revision
))
681 if (strcmp (gcc_version
->configuration_arguments
,
682 plugin_version
->configuration_arguments
))