flat assembler
Message board for the users of flat assembler.
Index
> Linux > Can a '.text' section of an ELF binary be 'rwx'? |
| Author |
|
|
Jessé 13 Jun 2026, 06:00
Hello,
I did some trials with 'elf64.inc' original file from fasm2 headers, to figure out what appears to be the main reason it does not even try to set 'writeable executable' flags under a section: trying to do this under a '.text' section, which will be the main executable section of your dynamic address ELF, simply doesn't work, apparently. I tried many things, but the resulting executable was always 'r-x' flags for the '.text' section. Has anyone tried something else to achieve a 'rwx' '.text' section? My workaround is as follows, but it is done at runtime, not compile time, so the section starts as compiled with 'r-x' flags set: Code: ; Self modifying code section example: it kind of replaces the missing '_code rwx' functionality, ; apparently unavailable to dynamic ELF '.text' section. ; After succeeded mprotect() call, this '.text' section behaves the same as '_code rwx'. format ELF64 include 'fastcall.inc' include 'stdmacros.inc' include 'stdio.inc' _code Start entry: lea rdi, [$] mov r10, 0_FFFF_FFFF_FFFF_F000h and rdi, r10 mprotect(rdi, 4096, (PROT_READ or PROT_WRITE or PROT_EXEC)); test eax, eax jz @f fprintf(*stderr, &errfmt, "failed: "); perror(NULL); exit(1); @@ fprintf(*stderr, &errfmt, "succeeded!"\n); signal(SIGINT, &.break); @@ inc [count] usleep(500'000); test [flags], 1 jnz .end fprintf(*stdout, <13,"Code section counter value: %lu",0>, *count); fflush(*stdout); jmp @b .end: fprintf(*stdout, "%s"\n "Finished."\n, <8,8," ",0>); exit(0); .break: or [flags], 1 ret errfmt xb 'Change memory protection %s', 0 count xq 0 flags xb 1111_1110b ; To compile: ; ; > ./build.sh code.rwx-dyn ; ; And then, run it: ; ; > ./code.rwx-dyn ; ; A good (and tested) way to see the change, is to look using edb-debugger under 'View->Memory Regions' ; before and after that mprotect() call, looking at the page address of that 'Start' entry point. To ease understanding, that whole '_code' line macro from this example translates to: Code: section '.text' executable align 16 public Start Start: lea rdi, [$] Thanks in advance if anyone can help, so I can apply an improvement (not too useful, but an improvement anyways) on this beauty. |
|||
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.