flat assembler
Message board for the users of flat assembler.

Index > Linux > A problem (SegFault)

Author
Thread Post new topic Reply to topic
Patrick_



Joined: 11 Mar 2006
Posts: 53
Location: 127.0.0.1
Patrick_ 12 Mar 2006, 23:08
Hey all. With the following code, I continually get Segmentation Faults. It's supposed to open up a file, write the records, and close the file. I believe the segmentation faults come from the "section '...'" parts, because if I change some to different values (like "data", "code", "text", etc), it doesn't fault. If I set the last section to ".text", I get a dump of all of the strings declared at the beginning of the code. Nothing is written to file, however, and no segmentation fault.

Thanks.

Code:
include 'linux.asm'
include 'record-def.asm'

        format ELF

        section '.data' 

        ;; Constant data of the records we want to write.
        ;; Each text data item is padded to the proper length
        ;; with NULL bytes

record1:

        ;; First name
        db 'Fredrick\0'

        ;; pad it for 40 bytes total
        repeat 32
                db 0
        end repeat

        ;; Last name
        db 'Bartlett\0'
        repeat 31
                db 0
        end repeat

        ;; Address
        db '4242 S Prairie\nTulsa, OK 55555\0'
        repeat 209
                db 0
        end repeat

        dd 45

record2:
        db 'Marilyn\0'
        repeat 32
                db 0
        end repeat

        db 'Taylor\0'
        repeat 33
                db 0
        end repeat

        db '2224 S Johannan St\nChicago, IL, 12345\0'
        repeat 203
                db 0
        end repeat

        dd 29

record3:
        db 'Derrick\0'
        repeat 32
                db 0
        end repeat

        db 'McIntire\0'
        repeat 32
                db 0
        end repeat

        db '500 W Oakland\nSan Diego, CA 54321'
        repeat 206
                db 0
        end repeat

        dd 36

        ;; Name of the file we will write to
file_name:
        db 'test.dat\0'

                
        ST_FD equ -4
        section '.code' executable
_start:
        
        
        ;; Copy stack pointer to ebp
        mov ebp, esp
        sub esp, 4              ;allocate some space to hold fd

        ;; open the file
        mov eax, SYS_OPEN
        mov ebx, file_name
        mov ecx, 0101           ;create if doesn't exist, open for writing
        mov edx, 0666
        int LINUX_SYSCALL

        ;; Store fd
        mov eax, [ebp+ST_FD]

        ;; How would I push a "record" based on loop counter?
        ;; edi is our increment counter
        xor edi, edi            ;zero it out

write_record_loop:

        ;; Write the record
        push word [ebp+ST_FD]

        jmp push_proper_record  ;find the record to push
after_push:
        call write_record
        add esp, 8
        inc edi                 ;increment our counter

        cmp edi, 3              ;reached end?
        jae done_loop

        ;; Nope, loop again
        jmp write_record_loop

done_loop:
        ;; close fd
        mov eax, SYS_CLOSE
        mov ebx, [ebp+ST_FD]
        int LINUX_SYSCALL

        ;; Exit program
        mov eax, SYS_EXIT
        mov ebx, 0
        int LINUX_SYSCALL
        
        ;; ok, kind of useless, but oh well
push_proper_record:
        cmp edi, 0
        je push_record_one

        cmp edi, 1
        je push_record_two

        cmp edi, 2
        je push_record_three

        jmp after_push          ;this won't happen, unless eic > 2

push_record_one:
        push record1
        jmp after_push
push_record_two:
        push record2
        jmp after_push
push_record_three:
        push record3
        jmp after_push


        ;; This function writes a record to the given file descriptor
        ;; INPUT: File descriptor, buffer
        ;; OUTPUT: Produces a status code (when're we gonna start printing?)

        section '.data'
        restore ST_WRITE_BUF
        restore ST_FD
        
        ST_WRITE_BUF equ -8
        ST_FD equ -12

        section '.code' 
        ;; @function write_record
write_record:
        push ebp
        mov ebp, esp

        push ebx                ;save ebx
        mov eax, SYS_WRITE
        mov ebx, [ebp+ST_FD]
        mov ecx, [ebp+ST_WRITE_BUF]
        mov edx, RECORD_SIZE
        int LINUX_SYSCALL

        ;; Note: eax has return value, which we will give back
        ;; to our calling program
        pop ebx

        mov esp, ebp
        pop ebp
        ret
    

[/code]
Post 12 Mar 2006, 23:08
View user's profile Send private message Reply with quote
Patrick_



Joined: 11 Mar 2006
Posts: 53
Location: 127.0.0.1
Patrick_ 13 Mar 2006, 23:51
Anyone? I keep getting Segmentation Faults if I use "format ELF", but don't if I use "format ELF executable"... however, with the "executable", I can't use sections like '.bss'... how would I do this?
Post 13 Mar 2006, 23:51
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi 14 Mar 2006, 00:38
Patrick_ wrote:
Anyone? I keep getting Segmentation Faults if I use "format ELF", but don't if I use "format ELF executable"... however, with the "executable", I can't use sections like '.bss'... how would I do this?



im just guessing here, you cant name the section with 'format ELF executable', but without you can and maybe section names are for linkers...


just guessing...


anyway, why do you have(want) to assign name for the section?

_________________
When We Ride On Our Enemies
support reverse smileys |:
Post 14 Mar 2006, 00:38
View user's profile Send private message MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8349
Location: Kraków, Poland
Tomasz Grysztar 14 Mar 2006, 08:10
The SECTION directive is kept in "format ELF executable" only for backward compatiblity - i the latest manual and examples you can notice it's a SEGMENT directive used istead it. It was done to reduce the confusion between ELF's sections (for the linker purpose) and the run-time segments.
Post 14 Mar 2006, 08:10
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.