The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: joemc on April 20, 2010, 03:29:47 AM

Title: make .CODE section able to be modified
Post by: joemc on April 20, 2010, 03:29:47 AM

  jmp OverData
    TheData dword 0
  OverData:
  mov TheData,eax 


Causes problems. that is just simplified version of what i am trying to accomplish. I am still searching but the words to describe the situation are very common with many topics that are not related.
Title: Re: make .CODE section able to be modified
Post by: hutch-- on April 20, 2010, 03:50:37 AM
Joe, writing data in the code section is OK, it is read only but just be careful of forward referencing, if its inserted in the code section after the location to call that data you may get errors.
Title: Re: make .CODE section able to be modified
Post by: dedndave on April 20, 2010, 04:19:40 AM
you can use the VirtualProtect API call to alter access rights to pages of the code section
what Hutch says is true, and can be very tricky with newer processors, as they cache quite a bit of code
if the "data" gets altered after it has been loaded into the prefetch queue, then accessed,
the processor may see the unaltered data from the queue - not the altered data as you might expect or require
Title: Re: make .CODE section able to be modified
Post by: donkey on April 20, 2010, 05:06:00 AM
Dave's hit the nail on the head here, the processor will sometimes not see modifications in the code section, you should consider using FlushInstructionCache (http://msdn.microsoft.com/en-us/library/ms679350%28VS.85%29.aspx) when you modify anything in the code section.
Title: Re: make .CODE section able to be modified
Post by: redskull on April 20, 2010, 11:54:10 AM
Code pages are mapped as read only by default, and will throw an access violation if you try to fuss with them.  You can override this in the linker through the use of the SECTION switch:

link /SUBSYSTEM:WINDOWS /SECTION:.text,W MyProg.obj

-r