LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2004, 05:31 PM   #1
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Rep: Reputation: 30
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
 
Old 05-05-2004, 05:32 PM   #2
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
PS: if you download the pdf of Programming from the ground up, check page 89.
 
Old 05-05-2004, 06:59 PM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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
 
Old 05-05-2004, 07:00 PM   #4
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
just look in the header files from man open to see the other constant values.
 
Old 05-05-2004, 07:32 PM   #5
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
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

Last edited by vexer; 05-05-2004 at 07:41 PM.
 
Old 05-05-2004, 07:54 PM   #6
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
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 +++
 
Old 05-05-2004, 08:24 PM   #7
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
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
 
Old 05-05-2004, 08:38 PM   #8
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.

Last edited by infamous41md; 05-05-2004 at 08:41 PM.
 
Old 05-05-2004, 08:45 PM   #9
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
Do you have to use a ret after?
 
Old 05-05-2004, 08:47 PM   #10
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 05-05-2004, 08:50 PM   #11
vexer
Member
 
Registered: Jan 2003
Location: Sudbury Ontario, Canada
Distribution: Slackware
Posts: 388

Original Poster
Rep: Reputation: 30
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?
 
Old 05-05-2004, 10:44 PM   #12
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ethical Issues in Open Source firefly2442 General 26 05-18-2005 02:50 PM
Jumping in asm LongName Programming 3 08-31-2004 08:27 PM
Open source release - disability issues Nimoy Linux - General 1 01-14-2004 03:21 PM
Open GL Refresh issues Cabs Linux - Software 10 08-24-2003 09:35 AM
Open GL issues ClayOgre Linux - Software 0 04-01-2003 04:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration