xed icon indicating copy to clipboard operation
xed copied to clipboard

Can xed be used to generate code for JIT engine

Open dibyendumajumdar opened this issue 5 years ago • 8 comments

Hi I assume that xed can be used as an assembler for a JIT engine? Is there an example of how to do this? Thank you

dibyendumajumdar avatar Aug 16 '19 11:08 dibyendumajumdar

yes. XED was originally created to be the instruction encoder/decoder for Intel's Pin binary instrumentation system, which is a JIT at its core. XED is also used in Intel's ZSIM emulator. There are papers on both (links below), but they are closed source projects. You'd probably get more out of reading the papers than sifting through enormous code bases. Maybe some of the people who sometimes respond to issues posted here can point out other examples.

https://software.intel.com/en-us/articles/pin-a-binary-instrumentation-tool-papers https://pdfs.semanticscholar.org/d853/013b71946850d0570d6c493a930dfd20d4cf.pdf

markcharney avatar Aug 16 '19 13:08 markcharney

Thank you. So there is no example in the repo showing how to compile and execute a simple function?

dibyendumajumdar avatar Aug 16 '19 13:08 dibyendumajumdar

there are simple examples of how to use xed in the examples directory. But a JIT is significantly beyond a simple example.

markcharney avatar Aug 16 '19 13:08 markcharney

Perhaps my question wasn't clear - I am only looking for a sample that shows how to compile something like:

int f(int a) { return a; }

That is, what facilities if any XED provides to generate a compiled object. I couldn't find any example that does that.

dibyendumajumdar avatar Aug 16 '19 18:08 dibyendumajumdar

I think you may have the wrong project. This project is for encoding and decoding x86 instructions. The decoder decodes 1...15 bytes and produces a data structure describing the instruction. The encoder takes a data structure describing an instruction and produces an array of 1...15 bytes.

This project does not parse C code.

markcharney avatar Aug 16 '19 19:08 markcharney

Sorry let me try again. Of course I know what this project does. I only gave an example of a simple function. I want to be able to create an assembly function, and the C function is just to show an example of a simple function. I want to emit the assembly code and be able to invoke the function.

I have my own C front-end if that helps. I am looking for an assembler library in C.

I know I can encode instructions using XED. But can I generate equivalent of an object file in memory that can then be invoked.

dibyendumajumdar avatar Aug 16 '19 20:08 dibyendumajumdar

Well XED is not an assembler either, sorry. With XED, one can generate (encoded) x86 instructions to a buffer, make the buffer executable and jump to it. How one makes a buffer executable is O/S dependent (mprotect() on linux) and you'd probably need a little assembly stub to do an indirect jump to the code you generated. You'd have to keep track of instruction sizes and patch branch displacements as well.

markcharney avatar Aug 16 '19 20:08 markcharney

Okay that's what I was trying to understand. I was hoping for an example of that in the samples, but I couldn't find any. Anyway I can figure out what to do.

dibyendumajumdar avatar Aug 16 '19 20:08 dibyendumajumdar