LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Issues with open() (asm) (https://www.linuxquestions.org/questions/programming-9/issues-with-open-asm-178298/)

vexer 05-05-2004 04:31 PM

Issues with open() (asm)
 
I wrote a small program that is supposed to (keyword: supposed) create a file and put the word BORING! inside. The problem I have is that it wont take O_RDONLY (or anything else for that matter)

I copied a program using this exact method from Programming from the Ground up and it worked. So... here is the code.

Code:

.section .data

.equ    OPEN,  5
.equ    WRITE,  4
.equ    CLOSE,  6

crack:
        .ascii  "bored!"
        len = . - crack

.section .text
.global _start

_start:

create_file:
        movl    $OPEN, %eax
        movl    $crack,%ebx
        movl    $O_RDONLY, %ecx
        movl    $0666, %edx
        int    $0x80

copy_descriptor:
        push    %eax

write_to_file:
        movl    $4, %eax
        movl    4(%ebp), %ebx
        movl    $crack, %ecx
        movl    $len, %ecx
        int    $0x80

close_file:
        movl    $6,%eax
        movl    4(%ebp), %ebx
        pop    %eax


vexer 05-05-2004 04:32 PM

PS: if you download the pdf of Programming from the ground up, check page 89.

infamous41md 05-05-2004 05:59 PM

1) i dont see where u have defined the constant O_RDONLY, 2) if you are writing to afile, why would u use O_RDONLY, and 3) the args to open are supposed to be:
int open(const char *pathname, int flags, mode_t mode);
so ebx should have the name of the file, 'crack' seems to be what you want to write to the file?? and if u want to create the file for writing, here is constant i used from a project i did:
OUTFILE_FLAGS = 0x241 ;//O_WRONLY | O_CREAT | O_TRUNC

infamous41md 05-05-2004 06:00 PM

just look in the header files from man open to see the other constant values.

vexer 05-05-2004 06:32 PM

Ok, so I fixed some of the errors. Code still doesn't work of course.


Code:

.section .data

.equ    OPEN,  5
.equ    WRITE,  4
.equ    CLOSE,  6
.equ    O_WRONLY, 1

filename:
        .ascii  "file.tickle"

crack:
        .ascii  "bored!"
        len = . - crack

.section .text
.global _start

_start:

create_file:
        movl    $OPEN, %eax
        movl    $filename,%ebx
        movl    $O_WRONLY, %ecx
        movl    $0666, %edx
        int    $0x80

copy_descriptor:
        push    %eax

write_to_file:
        movl    $WRITE, %eax
        movl    4(%ebp), %ebx
        movl    $crack, %ecx
        movl    $len, %edx
        int    $0x80

close_file:
        movl    $6,%eax
        movl    4(%ebp), %ebx
        pop    %eax


vexer 05-05-2004 06:54 PM

when I strace it, I get the following, which hints that there is something wrong with my messages :

Code:

execve("./harhar", ["./harhar"], [/* 46 vars */]) = 0
open("file.ticklebored!", O_WRONLY)      = -1 ENOENT (No such file or directory)
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++


vexer 05-05-2004 07:24 PM

I've done some work on the code. In the past say, 20 minutes. (probably less). Here is the strace once more:
Code:

execve("./harhar", ["./harhar"], [/* 47 vars */]) = 0
open("file.PNX", O_WRONLY|O_CREAT, 0666) = 3
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++

here is the code:

Code:

.section .data

.equ    OPEN,  5
.equ    WRITE,  4
.equ    CLOSE,  6
.equ    OPEN_FLAGS, 0x41 ## Set O_CREAT and O_WRONLY. Changed 0101 to hex


filename:
        .ascii  "file.PNX\0"

crack:
        .ascii  "bored!\0"
        len = . - crack

.section .text
.global _start

_start:

create_file:
        movl    $OPEN, %eax
        movl    $filename,%ebx
        movl    $OPEN_FLAGS, %ecx
        movl    $0666, %edx
        int    $0x80

copy_descriptor:
        push    %eax

write_to_file:
        movl    $WRITE, %eax
        #pop    %ebx
        movl    4(%ebp), %ebx
        movl    $crack, %ecx
        movl    $len, %edx
        int    $0x80

close_file:
        movl    $CLOSE,%eax
        pop    %ebx
        #movl  4(%esp), %ebx
        int    $0x80


infamous41md 05-05-2004 07:38 PM

Code:

(gdb) r
Starting program: /tmp/a.out

Program received signal SIGSEGV, Segmentation fault.
0x08048090 in write_to_file ()
(gdb) disas write_to_file
Dump of assembler code for function write_to_file:
0x804808b <write_to_file>:      mov    $0x4,%eax
0x8048090 <write_to_file+5>:    mov    0x4(%ebp),%ebx
0x8048093 <write_to_file+8>:    mov    $0x80490b1,%ecx
0x8048098 <write_to_file+13>:  mov    $0x7,%edx
0x804809d <write_to_file+18>:  int    $0x80
End of assembler dump.
(gdb) i r ebp
ebp            0x0      0x0
(gdb) q
The program is running.  Exit anyway? (y or n) y

you can't reference ebp with out properly settin up the stack frame, ie:
Code:

.section .data

.equ    STACK_FRAME_SZ, 100
.equ    OPEN,  5
.equ    EXIT, 1
.equ    WRITE,  4
.equ    CLOSE,  6
.equ    OPEN_FLAGS, 0x41 ## Set O_CREAT and O_WRONLY. Changed 0101 to hex


filename:
        .ascii  "file.PNX\0"

crack:
        .ascii  "bored!\0"
        len = . - crack

.section .text
.global _start

_start:

create_file:
        enter  $STACK_FRAME_SZ, $0
        movl    $OPEN, %eax
        movl    $filename,%ebx
        movl    $OPEN_FLAGS, %ecx
        movl    $0666, %edx
        int    $0x80

copy_descriptor:
        push    %eax

write_to_file:
        movl    $WRITE, %eax
        #pop    %ebx
        movl    4(%ebp), %ebx
        movl    $crack, %ecx
        movl    $len, %edx
        int    $0x80

close_file:
        movl    $CLOSE,%eax
        pop    %ebx
        #movl  4(%esp), %ebx
        int    $0x80

      leave
        movl    $EXIT, %eax
        int    $0x80

enter is the shortcut for:
pushl %ebp
movl %esp, %ebp
subl STACK_SZ, %esp
and leave is:
movl %ebp, %esp
popl %ebp

also notice i added the sys_exit function. when u enter a program via _start, there is not stack frame setup by libc init code, so u can't just "return" from main. if u ran your code rfurther, u would find it segfaulting after close_file.

vexer 05-05-2004 07:45 PM

Do you have to use a ret after?

infamous41md 05-05-2004 07:47 PM

you CANNOT ret when u call _start directly, u have to call exit like above. else u need to define main as a global function and use gcc to compile so that libc init code can be inserted, rather than 'ld && as' like i imagine ur using now.

vexer 05-05-2004 07:50 PM

Ok, I understand that now.

One last thing, why "enter $STACK_FRAME_SZ, $0"?

I understand that enter is equivalent to building the stack, but why the operands? what do those serve?

infamous41md 05-05-2004 09:44 PM

the first arg is the size of the frame, which is the amount to subtract from esp to create it, usually it looks like:
movl %esp, %ebp
pushl %ebp
subl $STACK_FRAME_SZ, %esp
that is just a shortcut for those 3 ins. and the 2nd arg to enter is some crap about nested frames that i dont recall, its not important for simple use.


All times are GMT -5. The time now is 11:21 PM.