Thursday, December 30, 2010

Adding Options to control LLVM CodeGen

This post shows how to add a command line option to clang which can be used to gate code in LLVM CodeGen. It took me a while, using gdb and grep, to figure out how this can be done.

The details : I wanted to add an option -random-stack-layout which would set a global option variable that I could read to turn on/off my stack layout randomization code in PrologEpilogInserter.cpp.

Before I talk about the changes, let me give you a quick introduction to how clang works. clang can work in the 'driver' mode and in the 'cc1' mode. The following command-line output, generated using the -v option, shows how this works.

km:~/projects/llvm-obj/Debug$ ./bin/clang -v ~/test/llvm/stack.c -o st
clang version 2.8 (tags/RELEASE_28)
Target: i386-pc-linux-gnu
Thread model: posix
"/home/km/projects/llvm-obj/Debug/bin/clang" -cc1 -triple i386-pc-linux-gnu -S -disable-free -disable-llvm-verifier -main-file-name stack.c -mrelocation-model static - mdisable-fp-elim -mconstructor-aliases -target-cpu pentium4 -target-linker-version 2.20.1 -v -resource-dir /home/km/projects/llvm-obj/Debug/lib/clang/2.8 -ferror-limit 19 -fmessage-length 237 -fgnu-runtime -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/cc-uTVPpH.s -x c /home/km/test/llvm/stack.c

clang -cc1 version 2.8 based upon llvm 2.8 hosted on i386-pc-linux-gnu
............
.............
"/usr/bin/as" --32 -o /tmp/cc-cFqgsc.o /tmp/cc-uTVPpH.s
"/usr/bin/gcc" -v -m32 -o st /tmp/cc-cFqgsc.o

when clang is invoked in the driver mode it invokes itself again with a list of arguments. The -cc1 is the argument that makes it behave like a compiler. This makes clang produce a .s file from the input .c file and then as, and gcc are invoked to produce the final executable (st in this example).

Note how the -v option is passed to gcc also. This is because -v is parsed and interpreted by the driver-mode clang. To avoid this behavior, the -Xclang switch can be used to send arguments to only cc1-mode clang.

The following changes illustrate the necessary changes to add an option that can be passed in with the -Xclang switch :

  • Edit /llvm/tools/clang/include/clang/Driver/CC1Options.td to add your option string under the appropriate section. In my case, I wanted to add a CodeGen option so I added the following line under the CodeGen options section.

def random_stack_layout : Flag<"-random-stack-layout">,
HelpText<"Enables stack layout randomization">;

The LLVM build infrastructure generates a clang/Driver/Options.inc out of this and includes it in /llvm/tools/clang/include/clang/Driver/Options.h. Here, the option we added gets translated to OPT_ random_stack_layout.

  • The option variable to set/unset is multicompiler::RandomStackLayout. It is an unsigned integer that is declared in/llvm/include/llvm/MultiCompiler/MultiCompilerOptions.h, and is defined in/llvm/lib/CodeGen/MultiCompilerOptions.cpp that gets compiled into the CodeGen library.

  • The OPT_ random_stack_layout flag can be checked in the ParseCodeGenArgs() method in /llvm/tools/clang/lib/Frontend/CompilerInvocation.cpp

#include "llvm/MultiCompiler/MultiCompilerOptions.h"
..........
...........
static void ParseCodeGenArgs( ...) {
.........
.........
multicompiler::RandomStackLayout = Args.hasArg(OPT_random_stack_layout);
..........
}

  • The RandomStackLayout variable can now be accessed in any CodeGen file by including the MultiCompilerOptions.h header file. The option can be specified in the command line as shown below

./bin/clang -Xclang -random-stack-layout ~/test/llvm/stack.c -o st


Friday, September 10, 2010

Visualizing Soot graphs

Notes on visualizing Soot CFGs:

Run Soot with appropriate dump options. e.g.

-pp -dump-cfg jap.sjenvanalyse -p jap.sjenvanalyse enabled:true -p tag.sjenvaggregator enabled:true -cp /home/karthik/projects/sjenv/bin test.basic.CFG

for the command line shown above, the generated .dot file can be found in the sub folders of sootOutput/test.basic.CFG

to generate a pdf out of the .dot file using graphviz , use

dot jap.sjenvanalyse-BriefUnitGraph-0.dot -T pdf -o whiledo.pdf

Thursday, December 27, 2007

Scroll wheel issue in Ubuntu guest on Vmware Fusion

The scroll wheel on my Logitech USB mouse was not working in the Ubuntu 7.10 guest running on VMware Fusion.

Fix: change the driver in the mouse section of /etc/X11/xorg.conf from ps/2 to imps/2

Ref : http://communities.vmware.com/message/780209

Wednesday, September 5, 2007

uid and gid

use the id command to find out the uid and the gid of a user.
example:

% id root
uid=0(root) gid=0(root) groups=0(root)

Friday, June 29, 2007

Printing code at a PC value in gdb

x /i $pc

Then you can

si

to continue stepping, along with a display of the instruction corresponding to the PC

Saturday, May 12, 2007

Problem in searching with Eclipse

Workspace/Project search for text, on my Eclipse CDT projects, returned "0 matches" in spite of the searched text being present.

The problem was that my default VM was set to the GNU Java VM.
Setting this to the Sun VM solved the problem.

http://ubuntuforums.org/showthread.php?t=201378 shows how to install the Sun VM on Ubutnu and make Eclipse use it.

Wednesday, May 9, 2007

Intel syntax in gdb

You can get gdb to use Intel syntax to display assembly code by using

set disassembly-flavor intel