dynamic code segments
has anyone had any experience using dynamic code segments. as in dynamically creating code segments, filling in the code then jumping to the address. i think the function supposed to be used is VirtualAlloc and VirtualFree.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
November 23, 2000 01:52 PM
Okay, its fairly simple...so long as you can deal with machine code As you''ve stated, you should use VirtualAlloc to allocate memory for your dynamic segment - you should do it something like this:
BYTE *pSegment=VirtualAlloc(NULL,dwSegmentLength,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
you can write to the segment by de-referencing pSegment, using a pointer into the segment etc. if you''re just copying (or decrypting) code into the segment, you should find that most of the jumps are relative, and won''t need relocs to be applied - but some jumps (and memory references) because they are absolute will need this!!! you will need to know where they are !!! if this is dynamically generated code, it shouldn''t provide too much of a problem - in static (copied) code you will need a reloc table.
Jumping to the entry-point, you just need to know where the entry point is relative to the start of the segment...then add the segment address to it and jmp to it...personally I prefer to use a call to the code as you can easily ret back to where you diverged from the pre-compiled code, without needing to work out an address. eg (in MSVC):
hope that helps a little bit...
oh, yeah - VirtualFree is what you use to free the memory when you''re finished
eg:
VirtualFree(pSegment,dwSegmentLength,MEM_DECOMMIT);
BYTE *pSegment=VirtualAlloc(NULL,dwSegmentLength,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
you can write to the segment by de-referencing pSegment, using a pointer into the segment etc. if you''re just copying (or decrypting) code into the segment, you should find that most of the jumps are relative, and won''t need relocs to be applied - but some jumps (and memory references) because they are absolute will need this!!! you will need to know where they are !!! if this is dynamically generated code, it shouldn''t provide too much of a problem - in static (copied) code you will need a reloc table.
Jumping to the entry-point, you just need to know where the entry point is relative to the start of the segment...then add the segment address to it and jmp to it...personally I prefer to use a call to the code as you can easily ret back to where you diverged from the pre-compiled code, without needing to work out an address. eg (in MSVC):
__asm { mov eax, pSegment add eax, EntryPointRVA call eax mov nReturnValue, eax}/* where: pSegment = pointer to start of dynamic segment EntryPointRVA = distance from start of segment to the routine entry point nReturnValue (optional really) = will contain whatever gets returned by the function*/
hope that helps a little bit...
oh, yeah - VirtualFree is what you use to free the memory when you''re finished
eg:
VirtualFree(pSegment,dwSegmentLength,MEM_DECOMMIT);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement