LWN: Comments on "An introduction to creating GCC plugins" https://lwn.net/Articles/457543/ This is a special feed containing comments posted to the individual LWN article titled "An introduction to creating GCC plugins". en-us Fri, 24 Oct 2025 11:36:12 +0000 Fri, 24 Oct 2025 11:36:12 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net An introduction to creating GCC plugins https://lwn.net/Articles/716261/ https://lwn.net/Articles/716261/ Patosga <div class="FormattedComment"> Hi,<br> <p> I'm interested on assessing memory accesses on a C program.<br> I want to generate my own code for all write operations to a specific variable, which is identified by the name.<br> <p> Do you have any idea how I could achieve that? Or where should I start?<br> <p> You said you can traverse the abstraction layers by getting an SSA_NAME instance and then a POINTER_TYPE, etc...<br> Is it possible to be at the lowest abstraction level (RTL I think) and identify write operations on a specific variable, just before the assembly code is generated?<br> <p> Thanks!<br> </div> Fri, 03 Mar 2017 22:38:36 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/677697/ https://lwn.net/Articles/677697/ milly_Z <div class="FormattedComment"> Thank you very much. I've been successful to build .so file, but something went wrong later.<br> $SPELLER=/home/yancai/Research/gcc-plugin/myplugin/speller.so<br> <p> If I use gcc-4.6, terminal showed as following:<br> <p> yancai@yancai-ThinkPad-W541:~/Research/gcc-plugin/myplugin$ gcc -fplugin=$SPELLER test.c<br> cc1: error: cannot load plugin /home/yancai/Research/gcc-plugin/myplugin/speller.so<br> /home/yancai/Research/gcc-plugin/myplugin/speller.so: undefined symbol: to_aspell_speller<br> <p> Well, I've found that symbol in aspell.h.<br> <p> Then I changed to gcc-4.7, and terminal showed as following:<br> <p> yancai@yancai-ThinkPad-W541:~/Research/gcc-plugin/myplugin$ gcc -fplugin=$SPELLER test.ccc1: 错误: 无法加载插件 /home/yancai/Research/gcc-plugin/myplugin/speller.so<br> /home/yancai/Research/gcc-plugin/myplugin/speller.so: undefined symbol: warning_at<br> <p> I followed the tips above, using g++ (mine is 4.8), and terminal showed as following:<br> <p> yancai@yancai-ThinkPad-W541:~/Research/gcc-plugin/myplugin$ g++ -I/usr/lib/gcc/i686-linux-gnu/4.8/plugin/include -c -fPIC -o speller.o speller.c<br> speller.c:165:5: error: expected primary-expression before ‘.’ token<br> .pass.type = GIMPLE_PASS,<br> ^<br> speller.c:166:5: error: expected primary-expression before ‘.’ token<br> .pass.name = "speller", /* For use in the dump file */<br> ^<br> speller.c:167:5: error: expected primary-expression before ‘.’ token<br> .pass.gate = speller_gate,<br> ^<br> speller.c:168:5: error: expected primary-expression before ‘.’ token<br> .pass.execute = speller_exec, /* Pass handler/callback */<br> ^<br> <p> Um...I really don't know what should I do next and I need some help.<br> Thank you very much.<br> <p> </div> Sat, 27 Feb 2016 02:23:47 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/677581/ https://lwn.net/Articles/677581/ cortana <div class="FormattedComment"> You need to install the aspell development files. Exactly how you do this depends on which distribution you're using. If you're using Debian (or some Debian-derived distribution such as Ubuntu) then you can use <a href="http://packages.debian.org/file:/usr/include/aspell.h">http://packages.debian.org/file:/usr/include/aspell.h</a> to see which package contains the missing file, and then install it with 'apt-get install libaspell-dev'.<br> </div> Fri, 26 Feb 2016 12:44:23 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/677579/ https://lwn.net/Articles/677579/ milly_Z <div class="FormattedComment"> Hi,<br> I am beginner in gcc plugins. I copied the source codes, but I failed to build the shared object. Terminal showed as following:<br> yancai@yancai-ThinkPad-W541:~/Research/gcc/myplugin$ gcc -I/usr/lib/gcc/i686-linux-gnu/4.6/plugin/include -c -fPIC -o speller.o speller.c<br> speller.c:22:20: fatal error: aspell.h: no such file or directory<br> compilation terminated.<br> <p> Could you please help me solve the problem?<br> <p> Thanks,<br> milly<br> </div> Fri, 26 Feb 2016 12:37:09 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/677574/ https://lwn.net/Articles/677574/ milly_Z <div class="FormattedComment"> Hi,<br> I am beginner in gcc plugins. I copied the source codes, but I failed to build the shared object. Terminal showed as following:<br> yancai@yancai-ThinkPad-W541:~/Research/gcc/myplugin$ gcc -I/usr/lib/gcc/i686-linux-gnu/4.6/plugin/include -c -fPIC -o speller.o speller.c<br> speller.c:22:20: fatal error: aspell.h: no such file or directory<br> compilation terminated.<br> <p> Could you please tell me how did you succeeded in that?<br> <p> Thanks,<br> milly<br> </div> Fri, 26 Feb 2016 12:13:11 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/554978/ https://lwn.net/Articles/554978/ PaXTeam <div class="FormattedComment"> gcc 4.7 introduced the ability to be built with a c++ compiler and most/all distros build gcc that way so you'll have to build your plugins with g++ instead of gcc as well.<br> </div> Mon, 17 Jun 2013 21:28:19 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/554968/ https://lwn.net/Articles/554968/ sandeepchaudhary <div class="FormattedComment"> Hi guys,<br> <p> I am posting this comment here for some help. I have followed the steps mentioned in this tutorial to create the plugin, and I am able to successfully build the plugin shared object. However, I can not run the plugin. It gives errors related to undefined symbols. Below is the terminal snippet. <br> <p> sandeep@ubuntu:~/myplugin$ /usr/bin/gcc -fplugin=/home/sandeep/myplugin/speller.so -c test.c<br> cc1: error: cannot load plugin /home/sandeep/myplugin/speller.so<br> /home/sandeep/myplugin/speller.so: undefined symbol: warning_at<br> <p> I can see that the mentioned symbol is indeed undefined using following<br> <p> sandeep@ubuntu:~/myplugin$ nm -D -C speller.so | grep warning_at<br> U warning_at<br> <p> And I see that <br> <p> sandeep@ubuntu:/usr/lib/gcc/x86_64-linux-gnu/4.7.2$ pwd<br> /usr/lib/gcc/x86_64-linux-gnu/4.7.2<br> sandeep@ubuntu:/usr/lib/gcc/x86_64-linux-gnu/4.7.2$ nm -D -C $(gcc -print-file-name=cc1) | grep "warning_"<br> 0000000000c1e5a0 T warning_at(unsigned int, int, char const*, ...)<br> <p> What am I missing? How should I link the libraries correctly?<br> <p> Thanks,<br> Sandeep.<br> <p> <p> <p> </div> Mon, 17 Jun 2013 21:04:06 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/461509/ https://lwn.net/Articles/461509/ MattDavis <div class="FormattedComment"> Thanks for the tip!<br> </div> Tue, 04 Oct 2011 08:44:38 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/461214/ https://lwn.net/Articles/461214/ PaXTeam <div class="FormattedComment"> i assume you're compiling all your code with -O0 then lest you fall victim of the infamous NULL check elimination as the kernel itself did not long ago, not to mention SSP that must be your enemy #1 then ;). more seriously, and assuming you're talking about the constification plugin, the truth is that the patch route was tried before and it just doesn't scale at both the producer (patch writer) and the consumer (lkml/kernel devs) side.<br> <p> to give you some numbers, an allmodconfig 2.6.39 i386 kernel loses over 7000 static (i.e., not runtime allocated) writable function pointers (a reduction of about 16%). creating an equivalent source patch would be thousands of lines of code and have virtually no chance to be accepted in any reasonable amount of time (not to mention the maintenance effort of being out-of-tree during the process; i've carried a small fraction of such a patch in PaX for years and it was a PITA even if the patch itself was 'only' 800k).<br> <p> also by virtue of enforcing const on these types one will quickly find out all the places where variables of a given type are modified (sometimes in direct contradiction to kernel policy) and one will be forced to document it in the code (for each such exception there's a writable type to be used, based on the no_const attribute introduced by the plugin). the patch for this is about 100k, but it also includes changes that actually eliminate the need to modify a given variable (vs. just changing its type to be writable).<br> <p> all in all, the cost/benefit ratio of the plugin approach is excellent and there's a lot more in the pipeline ;).<br> </div> Fri, 30 Sep 2011 21:10:25 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/461144/ https://lwn.net/Articles/461144/ vonbrand <p>I'd vote for a way to point out such oportunities to hackers, or even write source patches. Some magic mangling that gives object code that doesn't behave as the source says is deeply disturbing to my little mind.</p> Fri, 30 Sep 2011 13:38:00 +0000 An introduction to creating GCC plugins https://lwn.net/Articles/461048/ https://lwn.net/Articles/461048/ PaXTeam <div class="FormattedComment"> if anyone's interested in what gcc plugins can do in a kernel context, check out the few distributed in PaX (under tools/gcc). some examples: automatic ops structure constification, user/kernel function pointer separation, *alloc size statistics, etc.<br> </div> Thu, 29 Sep 2011 21:58:56 +0000 An introduction to creating GCC plugins: also GCC MELT or GCC Python https://lwn.net/Articles/461031/ https://lwn.net/Articles/461031/ dave_malcolm <div class="FormattedComment"> Thanks for mentioning the gcc Python plugin.<br> <p> I'm the author of the Python plugin, so naturally I'm fond of it.<br> <p> The lack of pattern matching within Python's syntax hasn't been a major issue to me in practice. Also, I like to think that it's become significantly more mature than when I first announced it :) <br> <p> I suspect that MELT will lead to faster-executing results, given that CPython isn't the fastest runtime in the world - though speed hasn't been an issue for me yet, either.<br> <p> Inspired by this LWN article, I had a go at implementing a spell-checking GCC pass in Python, using my plugin.<br> <p> You can see the results at:<br> <a href="http://readthedocs.org/docs/gcc-python-plugin/en/latest/working-with-c.html#spell-checking-string-constants-within-source-code">http://readthedocs.org/docs/gcc-python-plugin/en/latest/w...</a><br> <p> I believe my version is considerably simpler than doing it in C (about 35 lines of Python code, including comments), and, I hope, easier to read.<br> <p> The Python implementation of the spell checker is slightly different to the one in the article, in that I'm using gcc.Gimple.walk_tree() on each statement to (recursively) visit all tree nodes it references. I'm also using gcc.warning() and gcc.inform() to integrate with GCC's warnings subsystem, so that the error messages are in GCC's normal format (so that e.g. when I click on them in emacs it takes me to the source line containing the error).<br> <p> One of the nice things about using Python for this is all of the 3rd-party libraries that are available. In the Python implementation I'm using the "enchant" module that came out of AbiWord's spellchecker, and this makes it easy to offer spelling suggestions. There is a huge collection of Python libraries out there, so if you want to e.g. pause compilation part-way through and serve the internal-representation over HTTP, or dump it all into a database etc, there are lots of possibilities there.<br> <p> Hope this is helpful<br> Dave<br> </div> Thu, 29 Sep 2011 20:19:53 +0000 source code for example https://lwn.net/Articles/460952/ https://lwn.net/Articles/460952/ corbet <a rel="nofollow" href="https://lwn.net/Articles/458806/">Over here</a>. It was linked from the article, though, I must admit, not in an easy-to-notice way. Thu, 29 Sep 2011 13:27:28 +0000 source code for example https://lwn.net/Articles/460914/ https://lwn.net/Articles/460914/ mjw <div class="FormattedComment"> Is the source code for this example plugin available for those of us who are too lazy to copy/paste from the article?<br> </div> Thu, 29 Sep 2011 09:04:05 +0000 An introduction to creating GCC plugins: also GCC MELT or GCC Python https://lwn.net/Articles/460896/ https://lwn.net/Articles/460896/ bstarynk <p>There are also some simpler way to extend GCC: coding GCC plugins in C is tedious, and there are some alternatives: <ul> <li>First, use <a href='http://gcc-melt.org/'>GCC-MELT</a> <small>(disclaimer: I, Basile, am the main author of GCC MELT)</small>. <b>MELT</b> is a <i>high-level domain specific language to ease the development of GCC extensions</i>. The MELT language provides some high-level features: ability to do powerful pattern matching on GCC internal data (like Gimple): first-class dynamically typed garbage-collected values; ability to add new GCC passes in MELT; the MELT language is translated to C, and you can even add small C code chunks in your MELT code; MELT has a syntax similar to Lisp or Scheme; your MELT extensions can analyze or modify GCC internal representations, for various tasks like static analysis, coding rules validation, specific warnings, optimization, code refactoring, etc. MELT is available as a GCC [meta-] plugin.</li> <li>There is also a <a href='https://fedorahosted.org/gcc-python-plugin/'>Gcc Python Plugin</a> which enables you to code your GCC plugin-like extensions in Python. However, Python don't give you pattern matching on GCC internal representations (like MELT does), and the GCC Python plugin is less mature than MELT so gives you access to less internal GCC data.</li> </ul></p> <p>The main point is that people should <b>consider extending GCC for their own needs</b> and this is now possible with GCC plugin machinery, either in C, or in MELT or Python.</p> -- <a href='http://starynkevitch.net/Basile'>Basile Starynkevitch</a> Thu, 29 Sep 2011 06:42:54 +0000