flat assembler
Message board for the users of flat assembler.
Index
> Linux > Problems with Hello world |
Author |
|
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 |
|||
21 Mar 2008, 14:11 |
|
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. |
|||
21 Mar 2008, 15:24 |
|
revolution 21 Mar 2008, 15:32
Oh yeah, that is an old version of the example. Just change 'section' to 'segment'.
|
|||
21 Mar 2008, 15:32 |
|
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 ? |
|||
04 Aug 2017, 09:28 |
|
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. |
|||
04 Aug 2017, 11:10 |
|
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 Thanks so far for the explanations. EDIT2: So, when I write stand-alone asm files. I should
|
|||
04 Aug 2017, 13:06 |
|
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) |
|||
04 Aug 2017, 14:18 |
|
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. |
|||
04 Aug 2017, 14:32 |
|
egy 04 Aug 2017, 15:32
Tomasz Grysztar wrote:
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. |
|||
04 Aug 2017, 15:32 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.