LLVM as a native assembly instrumentation tool

Hi, there.

I am working on a project about control flow checking. For some reasons, I have to instrument native assembly code.

I have read about the MC layer of LLVM. I’m trying to use it for native assembly instrumentation, but have not found a way to do so. From the source code of the llvm-mc tool, I can see the source file is opened and added to the SourceMgr, which is in turn used to construct a MCContext object. At the end, I can see an AsmParser is constructed. However, it seems all work is actually done when the AsmParser is run. This appears to be very different from what I had in mind.

What I would like to do is:

  1. Parse a native assembly file into a MCContext, populating all internal data structures such as MCSection, MCFragment, MCInst, etc.

  2. Manipulate the data structures, adding instructions at points indicated by some labels.

  3. Write out the instrumented native assembly code to a text file.

Could you please tell me if the MC layer is a suitable tool for my work? If it is, would you give me some hints about how to use it?

Thank you!