0

gerard4143 explained 6 years ago how to store binary code in memory and then execute it. See https://www.daniweb.com/programming/software-development/threads/353077/store-binary-code-in-memory-then-execute-it#post1500738.

In the example, we create an array of unsigned char containing these values:
0x50,0x57,0x56,0x52,0xe8,
0x00,0x00,0x00,0x00,0x5e,
0x48,0x81,0xc6,0x24,0x00,
0x00,0x00,0x48,0xc7,0xc0,
0x01,0x00,0x00,0x00,0x48,
0xc7,0xc7,0x01,0x00,0x00,
0x00,0x48,0xc7,0xc2,0x0e,
0x00,0x00,0x00,0x0f,0x05,
0x5a,0x5e,0x5f,0x5a,0xc3,
0x48,0x65,0x6c,0x6c,0x6f,
0x2c,0x20,0x57,0x6f,0x72,
0x6c,0x64,0x21,0x0a,0x00

This works fine when I use the binary code provided with the example.

How does one generate the binary code that can be used in this manner?

I'm trying to generalize this "Hello, World!" example. The goal is to load binary code for a set of functions into memory, as an array of arrays of unsigned char, for instance. Set page attributes to permit execution. And execute the functions by treating pointers to memory locations as function pointers.

It seems eminently doable, but I don't know how to generate binary code suitable for loading into memory and executing from that. I thought maybe generate object code files and ignore the ELF header? Working with Linux on x86_64 systems.

2
Contributors
1
Reply
20
Views
2 Weeks
Discussion Span
Last Post by rproffitt
0

I'm going to answer this a different way. What you are essentially doing is Inline Assembler. There are priors on that at https://www.google.com/search?q=inline+assembler+linux but some basic knowledge is required here.

That is, you could put the assembler code in there or run it through the assmbler and pick over the bytes to copy into your app's inline assembler code.

Seasoned coders will not put byte code like that into an app so I don't expect a lot of support for this today.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.