flat assembler
Message board for the users of flat assembler.

Index > Linux > Problems with Hello world

Author
Thread Post new topic Reply to topic
marek



Joined: 21 Mar 2008
Posts: 5
marek 21 Mar 2008, 14:08
Hi all, I was trying to run my first FASM program, and I got only "Segmentation fault". Here is the code:

format ELF executable
string:
db 'Hello world!',0
start:
mov eax, 4
mov ebx, 1
mov ecx, string
mov edx, start-string
int 0x80

Compilation:

myhost~/fasm# ./fasm hello.asm
flat assembler version 1.67.26 (16384 kilobytes memory)
1 passes, 119 bytes.

Execution:
myhost~/fasm# ./hello
Segmentation fault


The similar program written in AT&T or NASM assembly workes fine, but in those two there were text and data sections declared explicitly. While trying to define these sections in FASM, I get compilation error.
Could you please help me correct my program?

Thanks in advance,
Marek.
Post 21 Mar 2008, 14:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
revolution 21 Mar 2008, 14:11
Check out the examples that come with the download. This is from there:
Code:
; fasm demonstration of writing simple ELF executable

format ELF executable
entry start

section readable executable

start:

 mov     eax,4
       mov     ebx,1
       mov     ecx,msg
     mov     edx,msg_size
        int     0x80

    mov     eax,1
       xor     ebx,ebx
     int     0x80

section readable writeable

msg db 'Hello world!',0xA
msg_size = $-msg    
Post 21 Mar 2008, 14:11
View user's profile Send private message Visit poster's website Reply with quote
marek



Joined: 21 Mar 2008
Posts: 5
marek 21 Mar 2008, 15:24
Thank you for your help (-:

"entry start" was the reason I think, now it works.

Surprisingly, the code you posted does not compile.

Here is my fasm output:
myhost:~/fasm# ./fasm hello.asm
flat assembler version 1.67.26 (16384 kilobytes memory)
hello1.asm [6]:
section readable executable
error: illegal instruction.
Post 21 Mar 2008, 15:24
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
revolution 21 Mar 2008, 15:32
Oh yeah, that is an old version of the example. Just change 'section' to 'segment'.
Post 21 Mar 2008, 15:32
View user's profile Send private message Visit poster's website Reply with quote
egy



Joined: 01 Aug 2017
Posts: 13
egy 04 Aug 2017, 09:28
Hey,

I am confused about whether it's 'segment' or 'section'. In the example shipped with fasm on linux, after replacing 'section' with 'segment', the code:
Code:
; fasm demonstration of writing simple ELF executable

format ELF executable 3

segment readable executable

entry start    ; in the original file this was located before the segment. You should put it here instead

start:

        mov     eax,4
        mov     ebx,1
        mov     ecx,msg
        mov     edx,msg_size
        int     0x80

        mov     eax,1
        xor     ebx,ebx
        int     0x80

segment readable writeable

msg db 'Hello world!',0xA
msg_size = $-msg
    

compiles successfully. while the code for the 'elfobj' example:
Code:
format ELF

section '.text' executable

 public _start
 _start:

 extrn writemsg

        mov     esi,msg
        call    writemsg

        mov     eax,1
        xor     ebx,ebx
        int     0x80

section '.data' writeable

 msg db "Elves are coming!",0xA,0
    

also compiles. So is it section + section_name or segment ?
Post 04 Aug 2017, 09:28
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2493
Furs 04 Aug 2017, 11:10
See Tomasz's post here: https://board.flatassembler.net/topic.php?p=193485#193485

Sections are for object files/static libraries, they get linked into segments by the linker. I hate the term "segment" though since it conflicts with x86's segments in terms (one of my pet peeves is how Intel didn't manage to utilize segments properly, pisses me off, so much lost potential).

If you are familiar with linker scripts for ld, they look at sections by name usually (with pattern matching) and merge them into output in a specific order. In this case, they merge them into segments.

In your first example, you compile directly to elf executable, not to "obj" (not object file). That's why you need segments, since that's what executable needs.

Object files cannot be run, they contain sections which need to be linked and merged (it is used in larger projects obviously, where you compile many files to objects and then link them together, instead of having to recompile everything from scratch all the time, you only recompile the object files that changed).

Disclaimer: I'm not too versed into this area, as most of my hacky and linker scripts are based on Windows (or Linux w/ wine); when I code tools for Linux I usually only use direct system calls. But you should understand the difference between object files and executable I hope?


For example you have 'foo' and 'bar' functions in two different object files. They are in .text.foo and .text.bar sections, respectively. Linker then merges them both to executable segment and you get one executable with one segment containing both functions. This is your second case with "object files".

Your first one would be: you place both foo and bar into executable segment and compile directly to executable -- no linking needed.
Post 04 Aug 2017, 11:10
View user's profile Send private message Reply with quote
egy



Joined: 01 Aug 2017
Posts: 13
egy 04 Aug 2017, 13:06
Thanks Furs. Your answer was complementary to Tomasz's. His answer was very clear too.

Quote:
Disclaimer: I'm not too versed into this area, as most of my hacky and linker scripts are based on Windows (or Linux w/ wine); when I code tools for Linux I usually only use direct system calls. But you should understand the difference between object files and executable I hope?

I do have a background on the subject and I understand most of what you've said. What I find strange though is that fasm is used as a linker and assembler ?!

From what I understood, the second code sample should be given to a linker (gcc perhaps?) after being assembled with fasm (fasm should produce a file with .o extension). What am I missing here ?

EDIT: ah okay, executing the file produced by fasm gives
Code:
cannot execute binary file    
. As described within the file, I have to link it with ld (or link.exe for windows If I recall).
Thanks so far for the explanations.

EDIT2: So, when I write stand-alone asm files. I should
    include exectutable in the format header
    not use sections
    not include external references or libraries
right ?
Post 04 Aug 2017, 13:06
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2493
Furs 04 Aug 2017, 14:18
Yeah that's right, I'm not entirely sure on the last point though.

BTW, theoretically, you could implement an ELF executable directly with just FASM macros and using "binary" format (i.e. raw bytes), and it wouldn't need to be hardcoded at all (as the macros would calculate all the stuff themselves).

People have done that on this board for Windows PE, not sure if any for ELF.

So it's not very surprising FASM can create executables directly (without a linker)
Post 04 Aug 2017, 14:18
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 04 Aug 2017, 14:32
Furs wrote:
People have done that on this board for Windows PE, not sure if any for ELF.
I made a simple template for manually constructed ELF executable a long time ago, in the early years of fasm development. In fact, even the Linux version of fasm was initially constructed this way, up to version 1.43 when the "format ELF" directive was implemented (the Linux version was first introduced with version 1.37).
Post 04 Aug 2017, 14:32
View user's profile Send private message Visit poster's website Reply with quote
egy



Joined: 01 Aug 2017
Posts: 13
egy 04 Aug 2017, 15:32
Tomasz Grysztar wrote:
Furs wrote:
People have done that on this board for Windows PE, not sure if any for ELF.
I made a simple template for manually constructed ELF executable a long time ago, in the early years of fasm development.


Oh! I just finished reading about the ELF header on Wikipedia and it matches exactly the sample you provided! I hope I learn as much as I can from all of you.
Post 04 Aug 2017, 15:32
View user's profile Send private message 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.