Writing in .text section - Linux

This is a discussion on Writing in .text section - Linux ; Hello to everyone, I have to write a program that modifies its code in .text section at runtime. I know that this is not possible as long as that section is marked as executable / readable but not writable. I ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Writing in .text section

  1. Writing in .text section

    Hello to everyone,

    I have to write a program that modifies its code in .text section at
    runtime. I know that this is not possible as long as that section is
    marked as executable / readable but not writable. I tried to look at
    the ELF file format but i didn't find anything useful. So the question
    is: how i can do that?

    tia,
    ac


  2. Re: Writing in .text section

    On Jun 18, 11:36 pm, pippo wrote:
    > Hello to everyone,
    >
    > I have to write a program that modifies its code in .text section at
    > runtime. I know that this is not possible as long as that section is
    > marked as executable / readable but not writable. I tried to look at
    > the ELF file format but i didn't find anything useful. So the question
    > is: how i can do that?
    >

    I have no such experience, but I think mmap with parameter PROT_EXEC
    may help, after mmap you copy your code to this page.



  3. Re: Writing in .text section

    > I have to write a program that modifies its code in .text section at
    > runtime. I know that this is not possible as long as that section is
    > marked as executable / readable but not writable. I tried to look at
    > the ELF file format but i didn't find anything useful. So the question
    > is: how i can do that?


    First you must set PROT_WRITE on the memory page; see PF_W in ,
    and mprotect(). Then after storing to memory you must convince the
    hardware to remove or invalidate stale data in the instruction cache
    (and/or instruction prefetch buffer, etc.) This is depends on the
    hardware, and deliberately is not supported in any portable way.
    On uniprocessor x86 any backwards branch will do the trick,
    so it happens to work "by itself" in nearly all cases.

    --

  4. Re: Writing in .text section

    pippo writes:
    >
    > I have to write a program that modifies its code in .text section at
    > runtime. I know that this is not possible as long as that section is
    > marked as executable / readable but not writable. I tried to look at
    > the ELF file format but i didn't find anything useful. So the question
    > is: how i can do that?


    What an odd assignment. Regardless, I'm sure your instructor will
    have some valuable tips for you if you go to their office hours and
    ask.

  5. Re: Writing in .text section


    "Joe Pfeiffer" wrote in message
    news:1bsl8pxkjz.fsf@snowball.wb.comcast.net...
    > pippo writes:
    > >
    > > I have to write a program that modifies its code in .text section at
    > > runtime. I know that this is not possible as long as that section is
    > > marked as executable / readable but not writable. I tried to look at
    > > the ELF file format but i didn't find anything useful. So the question
    > > is: how i can do that?

    >
    > What an odd assignment. Regardless, I'm sure your instructor will
    > have some valuable tips for you if you go to their office hours and
    > ask.


    Smells like virus writting...




  6. Re: Writing in .text section


    why do not reserve an array byte and write code on it, then jump into

    regards

    claudio daffra



  7. Re: Writing in .text section

    On Jun 18, 8:36 am, pippo wrote:

    > I have to write a program that modifies its code in .text section at
    > runtime. I know that this is not possible as long as that section is
    > marked as executable / readable but not writable. I tried to look at
    > the ELF file format but i didn't find anything useful. So the question
    > is: how i can do that?


    You don't need to modify the file, you only need to modify the faulted-
    in pages. Most modern operating systems (including Linux) map them
    copy-on-write already. So all you need to do is call 'mprotect',
    modify the code, then do whatever is required on your hardware to
    safely run modified code.

    DS


+ Reply to Thread