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