Código auto-modificado sempre falha de segmentação no Linux

Encontrei um artigo sobre código de modificação automática e tentei fazer alguns exemplos, mas sempre tenho falhas de segmentação. Tanto quanto eu posso entender, há uma violação nas permissões de memória. O segmento de código é (r) ead / e (x) ecute e, portanto, a tentativa de gravar resulta nessa falha. Existe uma maneira de testar o programa alterando as permissões de memória em tempo de execução ou antes? Estou usando o linux e o exemplo está escrito no assembly GAS.

.extern memcpy
.section .data
string:
        .asciz  "whatever"
string_end:
.section .bss
        .lcomm buf, string_end-string
.section .text
.globl main
main:
        call changer
        mov $string, %edx
label:
        push string_end-string
        push $buf
        push $string
        call memcpy
changer:
        mov $offset_to_write, %esi
        mov $label, %edi
        mov $0xb, %ecx
loop1:
        lodsb
        stosb
        loop loop1
        ret
offset_to_write:
        push 0
        call exit
end:

Então, após a modificação sugerida pelo osgx, aqui está um código que funciona. (Na verdade, se você montar, vincular e executar, ele trava, mas se você observar o gdb, ele modifica o código!)

.extern memcpy
.section .data
string:
        .asciz  "Giorgos"
string_end:
.section .bss
        .lcomm buf, string_end-string
.section .text
.globl main
main:
        lea (main), %esi                # get the start of memory region to
                                        # change its permissions (smc-enabled)
        andl $0xFFFFF000, %esi          # align to start of a pagesize
        pushl   $7                      # permissions==r|w|x
        pushl   $4096                   # page size
        pushl   %esi                    # computed start address
        call    mprotect

        call    changer                 # function that does smc
        mov     $string, %edx
label:
        push    string_end-string       # this code will be overridden
        push    $buf                    # and never be executed!
        push    $string
        call    memcpy
changer:
        mov     $offset_to_write, %esi  # simple copy bytes algorithm
        mov     $label, %edi
        mov     $0xb, %ecx
loop1:
        lodsb
        stosb
        loop    loop1
        ret
offset_to_write:                        # these instructions will be
        push    $0                      # executed eventually
        call    exit
end:

questionAnswers(3)

yourAnswerToTheQuestion