पर विभाजन दोष मैं स्वयं संशोधित कोड के बारे में एक लेख मिला और कुछ उदाहरण करने की कोशिश की है, लेकिन मैं हमेशा विभाजन दोष मिलता है। जैसा कि मैं समझ सकता हूं, स्मृति अनुमतियों में उल्लंघन है। कोड सेगमेंट (आर) ईएडी/ई (एक्स) ecute है और इसलिए इस गलती के परिणाम लिखने का प्रयास है। क्या रनटाइम पर या पहले मेमोरी अनुमतियों को बदलकर प्रोग्राम का परीक्षण करने का कोई तरीका है? मैं लिनक्स का उपयोग कर रहा हूं और उदाहरण जीएएस असेंबली में लिखा गया है।स्व संशोधित कोड हमेशा लिनक्स
.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:
इसलिए यहाँ osgx ने सुझाव दिया संशोधन के बाद एक काम कर कोड (वास्तव में अगर आप & लिंक & रन यह दुर्घटनाओं को इकट्ठा लेकिन आप gdb यह करता है का उपयोग करके देखें, तो अपने कोड को संशोधित करता है!) है।
.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:
सुनिश्चित नहीं हैं कि यह कैसे लिनक्स पर किया जाता है, लेकिन आप या तो .text अनुभाग के लिए कस्टम झंडे का उपयोग करने के अपने लिंकर हिदायत सकता है, या तो उन्हें स्रोत में निर्दिष्ट करने के लिए सक्षम होने के लिए (.data के लिए डिफ़ॉल्ट आरडब्ल्यू, .text के लिए RX है) – ruslik
@ruslik क्या मैं स्रोत से स्रोत (टेक्स्ट) सेगमेंट को आरडब्ल्यूएक्स का उपयोग करके निर्दिष्ट कर सकता हूं? –