0

I want to insert my code block at the prologue of each function in XNU kernel at compile time.

Writing an LLVM pass for IR transformation is probably the best choice for it, but I couldn't find any information about building XNU kernel with LLVM passes.

Is it possible to building XNU kernel with my own LLVM pass plugin? If so, could you please tell me how to do that or provide any link to it?

Is there any other method to instrument the XNU kernel with my code block at compile time?

This link describes how to build the XNU kernel.

Thanks.

compor
  • 2,239
  • 1
  • 19
  • 29
user10007
  • 1
  • 1

1 Answers1

1

I'm not sure this is going to be an adequate answer, but not enough space in the comments.

In general, if you want to pass anything via a custom LLVM optimization pass, you need to either 1) produce bitcode and use the pass then on it, or 2) have clang run that pass for you.

For 1), it means that you need to preprocess and compile the program with the same options and flags and do the same at the linking phase (when you're done processing the generated bitcode). AFAIK, the easiest and less invasive is to use the wllvm utility (especially since xnu uses make).

For 2), you need to register your plugin with clang's pass manager. According to the documentation, there are various registries for that, but the clang one is not mentioned. Looking at how LLVM does it in e.g. llvm/Transforms/IPO/PassManagerBuilder.h

#include "llvm/Transforms/IPO/PassManagerBuilder.h"
// using llvm::PassManagerBuilder
// using llvm::RegisterStandardPasses

static void registerHello(const llvm::PassManagerBuilder &Builder, 
                          llvm::legacy::PassManagerBase &PM) {
  PM.add(new HelloPass());

  return;
}

static llvm::RegisterStandardPasses RegisterHello(llvm::PassManagerBuilder::EP_EarlyAsPossible, registerHello);

and call it as

clang -Xclang -load -Xclang [path to plugin]/libHelloPass.so foo.c -o foo
compor
  • 2,239
  • 1
  • 19
  • 29
  • it would be helpful to address this issue. thanks for the response! – user10007 Dec 13 '17 at 01:05
  • assuming that I have a custom llvm pass, which part of files(makefile) in xnu kernel tree need to be modified to make the pass work on? – user10007 Dec 13 '17 at 01:39
  • or which options of make to use? CFLAGS? – user10007 Dec 13 '17 at 01:53
  • I'd guess so. That is something that you can try out on a toy/dummy project, right? You don't have to ask every single detail before even trying out stuff, please see [this](https://stackoverflow.com/help/how-to-ask) – compor Dec 13 '17 at 08:42