plugin.c 26 KB

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