Hello,
the solution is probably very simple, but I can't find it... the source code:
compile and execute:
"Speicherzugriffsfehler" means "segmentation fault", but why?
Using the ald debugger I have found the origin of the error: "MOV , AL". The error is caused by writing the content of AL into the memory, but the pointer (EBX) is initialized correctly at the line above: "MOV EBX, string"
I tried a couple of ideas with LEA instead of MOV, etc...
Anyone any idea?
Thanks for reading
loskornosdelsol
the solution is probably very simple, but I can't find it... the source code:
SECTION .DATA
hello: DB 'Hello world!',10
helloLen: EQU $-hello
SECTION .TEXT
GLOBAL _start
_start:
CALL printHW
XOR EAX, EAX
MOV EBX, string
MOV , AL
CALL printHW
CALL exit
string:
DB 0xff,0xff,0xff,0xff
;; write "hello world" to the screen
printHW:
PUSH EAX
PUSH EBX
PUSH ECX
PUSH EDX
MOV EAX, 4 ; system call "write"
MOV EBX, 1 ; file descriptor stdout
MOV ECX, hello ; string to write
MOV EDX, helloLen ; num of bytes to write
INT 80h ; call kernel interrupt
POP EDX
POP ECX
POP EBX
POP EAX
RET
;; terminate program
exit:
MOV EAX, 1 ; system call "exit"
MOV EBX, 0 ; exit code
INT 80h
compile and execute:
chk@subnetrouter ~/nasm $ nasm -f elf foo.asm
chk@subnetrouter ~/nasm $ ld foo.o -o foo
chk@subnetrouter ~/nasm $ ./foo
Hello world!
Speicherzugriffsfehler
"Speicherzugriffsfehler" means "segmentation fault", but why?
Using the ald debugger I have found the origin of the error: "MOV , AL". The error is caused by writing the content of AL into the memory, but the pointer (EBX) is initialized correctly at the line above: "MOV EBX, string"
I tried a couple of ideas with LEA instead of MOV, etc...
Anyone any idea?
Thanks for reading
loskornosdelsol
The problem is that you are writing to the .TEXT section which has the permissions of Read/Execute. So when you try to write to it, you get an error because you don't have permission to write to that section of memory. Put string in the .DATA or .BSS sections (or set the permissions of .TEXT to Read/Write/Execute). Try using:
Regards,
Bryant Keller
SECTION .TEXT EXEC WRITE ALIGN=16
Regards,
Bryant Keller
Aaaah, ok. First: Thanks a lot! I didn't know, that such "section flags" exist. Replacing my section definition with yours solved the problem. Ok, it's a small security hole, but I know occurs.
Second: I found this website: http://www.tortall.net/projects/yasm/wiki/ElfObject - it told me (like you), that .DATA would be writeable per default. But, before posting my problem, I tried this too. When I move "string: DB 0xff,0xff,0xff,0xff" into the .DATA section, the problem is still present.
Mhmm, when changing to "SECTION .DATA WRITE ALIGN=16" the problem is really solved, but why is the data section not writable per default. Is this set by the nasm configuration, where can I change the defaults?
I will read the nasm doc....
thanks for help!!!
loskornosdelsol
edit: I can't find information about the section access configuration in the nasm documentation at http://nasm.sourceforge.net/doc/html/nasmdoc0.html
Second: I found this website: http://www.tortall.net/projects/yasm/wiki/ElfObject - it told me (like you), that .DATA would be writeable per default. But, before posting my problem, I tried this too. When I move "string: DB 0xff,0xff,0xff,0xff" into the .DATA section, the problem is still present.
Mhmm, when changing to "SECTION .DATA WRITE ALIGN=16" the problem is really solved, but why is the data section not writable per default. Is this set by the nasm configuration, where can I change the defaults?
I will read the nasm doc....
thanks for help!!!
loskornosdelsol
edit: I can't find information about the section access configuration in the nasm documentation at http://nasm.sourceforge.net/doc/html/nasmdoc0.html
edit: I can't find information about the section access configuration in the nasm documentation at http://nasm.sourceforge.net/doc/html/nasmdoc0.html
Read chapters 5 and 6 of the NASM docs.
Aaaaah! The section ".data" is writable per default, but ".DATA" is treated as "other", and "other" is not writable :-)