flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Unix > Hello World on FreeBSD

Author
Thread Post new topic Reply to topic
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4641
Location: Argentina
Hello World on FreeBSD
What's wrong here?


Code:

$ uname -vmp
FreeBSD 6.1-RELEASE #0Sun May  7 04:32:43 UTC 2006     root@opus.cse.buffalo.e
du:/usr/obj/usr/src/sys/GENERIC  i386 i386
$ cat hello-exe.asm
format ELF executable
entry main

main:
push    mensaje.size
push    mensaje
push    1               ; stdout
mov     eax4          ; sys_write
push    0               ; dummy argument
int     $80

xor     eaxeax        ; sys_exit
int     $80

mensaje         db      "Hello World Very Happy"10
mensaje.size    =       $-mensaje
$ ./fasm hello-exe.asm
flat assembler  version 1.67.14  (16384 kilobytes memory)
3 passes121 bytes.
$ chmod +x hello-exe
$ ./hello-exe
ELF binary type "0" not known.
./hello-exe1Syntax error"(" unexpected
$ file hello-exe
hello-exeELF 32-bit LSB executableIntel 80386version 1 (SYSV), statically
linkedstripped
$




Note that producing an ELF object first and then using gcc to produce the executable works fine

Code:

$ uname -vmp
FreeBSD 6.1-RELEASE #0Sun May  7 04:32:43 UTC 2006     root@opus.cse.buffalo.e
du:/usr/obj/usr/src/sys/GENERIC  i386 i386
$ cat hello.asm
format ELF
public main

main:
push    mensaje.size
push    mensaje
push    1               ; stdout
mov     eax4          ; sys_write
push    0               ; dummy argument
int     $80

xor     eaxeax        ; sys_exit
int     $80

mensaje         db      "Hello World Very Happy"10
mensaje.size    =       $-mensaje
$ ./fasm hello.asm
flat assembler  version 1.67.14  (16384 kilobytes memory)
3 passes377 bytes.
$ gcc hello.o -o hello
$ ./hello
Hello World :D
$ file hello
helloELF 32-bit LSB executableIntel 80386version 1 (FreeBSD), dynamically
linked (uses shared libs), not stripped
$ file hello.o
hello.oELF 32-bit LSB relocatableIntel 80386version 1 (SYSV), not stripped
$




Is the last one method the unique available to produce executables for FreeBSD?
Post 19 Nov 2006, 14:10
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6303
Location: Kraków, Poland
Perhaps you need to use the "brandelf -t FreeBSD" command, or something like?
Post 19 Nov 2006, 15:25
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4641
Location: Argentina

Code:
$ brandelf -t FreeBSD hello-exe                                                
$ file hello-exe                                                               
hello-exeELF 32-bit LSB executableIntel 80386version 1 (FreeBSD), statica
ly linkedstripped                                                            
$ ./hello-exe                                                                  
Hello World :D                                                                 
$                                                                              




Thanks!! Very Happy

BTW, this utility just sets "FreeBSD" and doesn't do anything else? If it is the case then it could be great to add some feature in fasm to override the SYSV built-in default (but doing it inside the source, not a comand line parameter, something like "abi FreeBSD" or "abi number").

[edit] Or maybe better, something like "format ELF {FreeBSD|SYSV|number|etc} executable " / "format ELF {FreeBSD|SYSV|number|etc}"[/edit]
Post 19 Nov 2006, 15:49
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6303
Location: Kraków, Poland
Well, it was already requested: http://board.flatassembler.net/topic.php?t=6163
I certainly should consider this.
Post 19 Nov 2006, 16:30
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4641
Location: Argentina
Thanks for consider this Smile
Post 19 Nov 2006, 18:09
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4641
Location: Argentina

Code:

$ brandelf -t Linux hello-exe
$ file hello-exe                                                               
hello-exeELF 32-bit LSB executableIntel 80386version 1 (GNU/Linux), statically linkedstripped
$ ./hello-exe
Segmentation fault (core dumped)




Now I see why the Linux examples doesn't work in FreeBSD, because fasm produces SYSV executables instead. Is there some difference between Unix SysV ABI and Linux ABI or just FreeBSD is too strict?

PS: Note that the Segmentation fault is OK since the way I'm calling the kernel is for FreeBSD (I'm using the same source from the first post).
PS2: And I have Linux ABI compatibility enabled of course
Post 30 Nov 2006, 21:23
View user's profile Send private message Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
The Linux ABI is different. The BSDs (at least on 32-bit) pass arguments to syscalls via the stack, where Linux uses registers. I believe that at least some of the function numbers differ as well.

FreeBSD does support the Linux ABI, but if you brand a FreeBSD executable as "Linux", it will try to run it with the Linux ABI and have problems quickly.

As a side note, not all BSD's make use of branding. NetBSD for example checks for a ".note.netbsd.ident" section for branding information, rather than use a "brandelf" type tool. Given this, it is possible to make a program that works on both FreeBSD (after branding) and NetBSD (no branding needed if this section exists with the proper values).
Post 02 Dec 2006, 04:58
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4641
Location: Argentina
crc, yes, I know that Linux ABI is different to FreeBSD ABI. My question was

Quote:

Is there some difference between Unix SysV ABI and Linux ABI or it is just FreeBSD that is too much strict? bold=corrections



Thanks for the note about NetBSD, now we know that http://board.flatassembler.net/topic.php?t=6163 is not enough (if fasm just brands the executable instead of adding a note when found NetBSD)

PD: Not that by strict I mean that FreeBSD refuses to execute SYSV executables when the same executables works on Linux with "SYSV" brand and "Linux" brand.
Post 02 Dec 2006, 14:16
View user's profile Send private message Reply with quote
hamoz



Joined: 14 Dec 2006
Posts: 29
LocoDelAssembly, that is your hello world example

I didnt use gcc to link the hello coz the current fasm gave me an

executable file no hello.o < no object file Confused > it works well after I have

been allowed it as a root



Quote:
# ./fasm hello.asm
flat assembler version 1.67.21 (16384 kilobytes memory)
3 passes, 129 bytes.
# brandelf -t freebsd hello
# ./hello
./hello: Permission denied.
#chmod 777 hello
#./hello
hello world very happy
# file hello
hello: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), statically linked, stripped



tell me why I didnt need to gcc is it an advantage in the current fasm

because the fasm is linked by gcc first time and we dont need it again

I did it easily than yours


thanks alot Smile
Post 20 Feb 2007, 18:26
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4641
Location: Argentina
Not sure if you are asking or not Razz You don't need gcc for your files assembled with fasm because since you have specified "format ELF executable" it produces an executable and for that reason no linking is needed but a branding is required since fasm brands the executable as SysV and that doesn't work.
Post 20 Feb 2007, 23:09
View user's profile Send private message Reply with quote
hamoz



Joined: 14 Dec 2006
Posts: 29
exactly I have been specified " format ELF executable "

but one question what does the brandelf command brand to if it is not

SYSV.... Very Happy
Post 20 Feb 2007, 23:35
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4641
Location: Argentina
The branding specifies which type of ABI (Application Binary Interfase) the executable has, for example FreeBSD supports Linux executables so if you brand it as Linux, FreeBSD will execute it but since the kernel on FreeBSD is called different than Linux the executable will produce errors at runtime (The parameters on FreeBSD system call are passed in stack but on Linux are passed with registers).
Post 21 Feb 2007, 00:17
View user's profile Send private message Reply with quote
hamoz



Joined: 14 Dec 2006
Posts: 29
LocoDelAssembly, I really thank you so much Smile

cheers

newbie
Post 21 Feb 2007, 00:24
View user's profile Send private message Reply with quote
jb



Joined: 26 Mar 2007
Posts: 5
Location: Holland
I have a small patch for fasm to create an elf note for NetBSD 3.1/i386 executables. It allows a new flag (`note') in the segment directive. Here's a small example of how to use it:

$ cat test.asm
format elf executable
entry start
start:
push 47
push eax
xor eax,eax
inc eax
int 0x80
include 'note.inc'
$ cat note.inc
segment note
dd 0x00000007
dd 0x00000004
dd 0x00000001
dd 0x4274654e
dd 0x00004453
dd 0x11f0e540
$ fasm test.asm
flat assembler version 1.67.21 (16384 kilobytes memory)
3 passes, 148 bytes.
$ file test
test: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for NetBSD 3.1, statically linked, stripped
$ ./test; echo $?
47
$ readelf -l test

Elf file type is EXEC (Executable file)
Entry point 0x8048074
There are 2 program headers, starting at offset 52

Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000074 0x08048074 0x08048074 0x00008 0x00008 RWE 0x1000
NOTE 0x00007c 0x0804907c 0x0804907c 0x00018 0x00018 0x4

To keep `file' accurate the alignment was changed from 4K to 4. RWE is a don't care.

The note itself was created by picking a random executable (i.e. /bin/ls), looking at the segment header for the note to get offset and size (0x00018=24 bytes), and then doing a hexdump -e '6/4 "dd 0x%08x\n"' -n 24 -s 0x108 on /bin/ls.

Here's the patch:

$ rcsdiff -r1.1 -rnote formats.inc
===================================================================
RCS file: formats.inc,v
retrieving revision 1.1
retrieving revision 1.2
diff -r1.1 -r1.2
3630a3631,3632
> cmp ah,4
> je elf_note
3643a3646,3649
> elf_note:
> mov byte [ebx],4
> mov word [ebx+1Ch],4
> jmp elf_segment_flags
$ rcsdiff -r1.1 -rnote tables.inc
===================================================================
RCS file: tables.inc,v
retrieving revision 1.1
retrieving revision 1.2
diff -r1.1 -r1.2
273a274
> db 'note',19h,32

Crude, but effective.
Post 26 Mar 2007, 23:23
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4641
Location: Argentina
Well, I can't remember how I managed to copy the text from the VirtualPC (or maybe changed the copy behavior in some version?), so I'll have to post a screenshot instead...

It is essentially the same code I posted at the beginning of this thread but now the linker nor brandelf are needed anymore (due to the latest changes in the ELF formatter)

PS: Notice the "9" in the first line of the code, that is the value for FreeBSD ABI.


Description:
Filesize: 13.02 KB
Viewed: 780 Time(s)

freebsd.png


Post 13 Jul 2010, 23:41
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.