From ec9aecea070c534ccc37e880a204ce7460162723 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Wed, 11 Dec 2019 08:21:51 +0000 Subject: [PATCH] freebsd: Initial x86 scaffold. * lib/freebsd/x86-mes-gcc/crt1.c: New file. * lib/freebsd/x86-mes-gcc/exit-42.S: New file. * lib/freebsd/x86-mes-gcc/hello-mes.S: New file. * lib/freebsd/x86-mes-gcc/mini.c: New file. * lib/freebsd/x86-mes-mescc/crt1.c: New file. * lib/freebsd/x86-mes-mescc/mini.c: New file. * lib/freebsd/x86-mes/elf32-0exit-42.hex2: New file. * lib/freebsd/x86-mes/elf32-0header.hex2: New file. * lib/freebsd/x86-mes/elf32-0hello-mes.hex2: New file. * lib/freebsd/x86-mes/elf32-body-exit-42.hex2: New file. * lib/freebsd/x86-mes/elf32-body-hello-mes.hex2: New file. * lib/freebsd/x86-mes/elf32-footer-single-main.hex2: New file. * lib/freebsd/x86-mes/elf32-header.hex2: New file. --- lib/freebsd/x86-mes-gcc/crt1.c | 76 ++++++ lib/freebsd/x86-mes-gcc/exit-42.S | 47 ++++ lib/freebsd/x86-mes-gcc/hello-mes.S | 60 +++++ lib/freebsd/x86-mes-gcc/mini.c | 64 +++++ lib/freebsd/x86-mes-mescc/crt1.c | 65 +++++ lib/freebsd/x86-mes-mescc/mini.c | 43 ++++ lib/freebsd/x86-mes/elf32-0exit-42.hex2 | 39 +++ lib/freebsd/x86-mes/elf32-0header.hex2 | 85 +++++++ lib/freebsd/x86-mes/elf32-0hello-mes.hex2 | 58 +++++ lib/freebsd/x86-mes/elf32-body-exit-42.hex2 | 54 +++++ lib/freebsd/x86-mes/elf32-body-hello-mes.hex2 | 69 ++++++ .../x86-mes/elf32-footer-single-main.hex2 | 64 +++++ lib/freebsd/x86-mes/elf32-header.hex2 | 225 ++++++++++++++++++ 13 files changed, 949 insertions(+) create mode 100644 lib/freebsd/x86-mes-gcc/crt1.c create mode 100644 lib/freebsd/x86-mes-gcc/exit-42.S create mode 100644 lib/freebsd/x86-mes-gcc/hello-mes.S create mode 100644 lib/freebsd/x86-mes-gcc/mini.c create mode 100644 lib/freebsd/x86-mes-mescc/crt1.c create mode 100644 lib/freebsd/x86-mes-mescc/mini.c create mode 100644 lib/freebsd/x86-mes/elf32-0exit-42.hex2 create mode 100644 lib/freebsd/x86-mes/elf32-0header.hex2 create mode 100644 lib/freebsd/x86-mes/elf32-0hello-mes.hex2 create mode 100644 lib/freebsd/x86-mes/elf32-body-exit-42.hex2 create mode 100644 lib/freebsd/x86-mes/elf32-body-hello-mes.hex2 create mode 100644 lib/freebsd/x86-mes/elf32-footer-single-main.hex2 create mode 100644 lib/freebsd/x86-mes/elf32-header.hex2 diff --git a/lib/freebsd/x86-mes-gcc/crt1.c b/lib/freebsd/x86-mes-gcc/crt1.c new file mode 100644 index 00000000..a5998043 --- /dev/null +++ b/lib/freebsd/x86-mes-gcc/crt1.c @@ -0,0 +1,76 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include +//int main (int argc, char *argv[], char *envp[]); + +// *INDENT-OFF* +void +_start () +{ + asm ( + "mov $0,%%eax\n\t" + "mov %%eax,%0\n" + : "=r" (__stdin) + : //no inputs "" + ); + + asm ( + "mov $1,%%eax\n\t" + "mov %%eax,%0\n" + : "=r" (__stdout) + : //no inputs "" + ); + + asm ( + "mov $2,%%eax\n\t" + "mov %%eax,%0\n" + : "=r" (__stderr) + : //no inputs "" + ); + asm ( + "mov %%ebp,%%eax\n\t" + "add $4,%%eax\n\t" + "movzbl (%%eax),%%eax\n\t" + "add $3,%%eax\n\t" + "shl $2,%%eax\n\t" + "add %%ebp,%%eax\n\t" + "mov %%eax,%0\n\t" + "push %%eax\n\t" + : "=r" (environ) + : //no inputs "" + ); + asm ( + "mov %ebp,%eax\n\t" + "add $8,%eax\n\t" + "push %eax\n\t" + + "mov %ebp,%eax\n\t" + "add $4,%eax\n\t" + "movzbl (%eax),%eax\n\t" + "push %eax\n\t" + + "call main\n\t" + + "push %eax\n\t" + "call _exit\n\t" + "hlt \n\t" + ); +} diff --git a/lib/freebsd/x86-mes-gcc/exit-42.S b/lib/freebsd/x86-mes-gcc/exit-42.S new file mode 100644 index 00000000..503186db --- /dev/null +++ b/lib/freebsd/x86-mes-gcc/exit-42.S @@ -0,0 +1,47 @@ +/* + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +/** Commentary: */ + +/* Using gdb, esp. GUD in GNU Emacs is recommended +M-x gdb-display-disassembly-buffer +M-x gdb-display-io-buffer +set disassemble-next-line on +break _start +run +info registers +si +p/x $eax +RET +... +*/ + +/** Code: */ + +SYS_exit = 1 /* FreeBSD syscall: exit. */ + .globl _start /* Magic linker symbol: --entry-address. */ + + .text /* Program text. */ +_start: + mov $SYS_exit,%eax /* System call function: exit, in %eax. */ + push $42 /* Push first parameter: exit status. */ + push $00 /* Push ??. */ + int $0x80 /* Call system. */ + hlt /* Should not be reached. */ diff --git a/lib/freebsd/x86-mes-gcc/hello-mes.S b/lib/freebsd/x86-mes-gcc/hello-mes.S new file mode 100644 index 00000000..154babec --- /dev/null +++ b/lib/freebsd/x86-mes-gcc/hello-mes.S @@ -0,0 +1,60 @@ +/* + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +/** Commentary: */ + +/* Using gdb, esp. GUD in GNU Emacs is recommended +M-x gdb-display-disassembly-buffer +M-x gdb-display-io-buffer +set disassemble-next-line on +break _start +run +info registers +si +p/x $eax +RET +... +*/ + +/** Code: */ + +SYS_exit = 1 /* Linux syscalls. */ +SYS_write = 4 +stdout = 1 /* File discriptor */ + .globl _start /* Magic linker symbol: --entry-address. */ + + .text /* Program text. */ +_start: + mov $SYS_write,%eax /* System call function: write, in %eax. */ + push $(bye-hello) /* 3rd parameter: byte count. */ + push $hello /* 2nd parameter: address. */ + push $stdout /* 1st parameter: file descriptor. */ + push $0 /* ?? */ + int $0x80 /* Call system. */ + + mov $SYS_exit,%eax /* System call function: exit, in %eax. */ + push $0 /* 1st parameter: exit status. */ + push $0 /* ?? */ + int $0x80 /* Call system. */ + hlt /* Should not be reached. */ + + .data +hello: .ascii "Hello, GNU Mes!\n" +bye: .byte 0 diff --git a/lib/freebsd/x86-mes-gcc/mini.c b/lib/freebsd/x86-mes-gcc/mini.c new file mode 100644 index 00000000..7978ee25 --- /dev/null +++ b/lib/freebsd/x86-mes-gcc/mini.c @@ -0,0 +1,64 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +#define SYS_exit "0x01" +#define SYS_write "0x04" + +// *INDENT-OFF* +void +_exit (int code) +{ + asm ( + "mov $"SYS_exit",%%eax\n\t" + "mov %0,%%ebx\n\t" + "push %%ebx\n\t" + "push %%ebx\n\t" + "int $0x80\n\t" + : // no outputs "=" (r) + : "rm" (code) + : "eax", "ebx" + ); + // not reached + _exit (0); +} + +ssize_t +_write (int filedes, void const *buffer, size_t size) +{ + long r; + asm ( + "mov $"SYS_write",%%eax\n\t" + "mov %3,%%edx\n\t" + "push %%edx\n\t" + "mov %2,%%ecx\n\t" + "push %%ecx\n\t" + "mov %1,%%ebx\n\t" + "push %%ebx\n\t" + "push %%ebx\n\t" + "int $0x80\n\t" + "mov %%eax,%0\n\t" + : "=r" (r) + : "rm" (filedes), "rm" (buffer), "rm" (size) + : "eax", "ebx", "ecx", "edx" + ); + return r; +} diff --git a/lib/freebsd/x86-mes-mescc/crt1.c b/lib/freebsd/x86-mes-mescc/crt1.c new file mode 100644 index 00000000..0e92ad34 --- /dev/null +++ b/lib/freebsd/x86-mes-mescc/crt1.c @@ -0,0 +1,65 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +int main (int argc, char *argv[], char *envp[]); + +int +_start () +{ + asm ("mov____$i8,%eax !0"); + asm ("mov____%eax,0x32 &__stdin"); + + asm ("mov____$i8,%eax !1"); + asm ("mov____%eax,0x32 &__stdout"); + + asm ("mov____$i8,%eax !2"); + asm ("mov____%eax,0x32 &__stderr"); + + asm ("mov____%ebp,%eax"); + asm ("add____$i8,%eax !4"); + + asm ("movzbl_(%eax),%eax"); + asm ("add____$i8,%eax !3"); + + asm ("shl____$i8,%eax !0x02"); + asm ("add____%ebp,%eax"); + asm ("mov____%eax,0x32 &environ"); + asm ("push___%eax"); + + asm ("mov____%ebp,%eax"); + asm ("add____$i8,%eax !8"); + asm ("push___%eax"); + + asm ("mov____%ebp,%eax"); + asm ("add____$i8,%eax !4"); + asm ("movzbl_(%eax),%eax"); + asm ("push___%eax"); + + main (); + + asm ("mov____%eax,%ebx"); + asm ("mov____$i32,%eax %1"); + asm ("push___%ebx"); + asm ("push___%ebx"); + asm ("int____$0x80"); + asm ("hlt"); +} diff --git a/lib/freebsd/x86-mes-mescc/mini.c b/lib/freebsd/x86-mes-mescc/mini.c new file mode 100644 index 00000000..7756a7ae --- /dev/null +++ b/lib/freebsd/x86-mes-mescc/mini.c @@ -0,0 +1,43 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +void +_exit () +{ + asm ("mov____$i32,%eax SYS_exit"); + asm ("mov____0x8(%ebp),%ebx !8"); + asm ("push___%ebx"); + asm ("push___%ebx"); + asm ("int____$0x80"); +} + +void +_write () +{ + asm ("mov____$i32,%eax SYS_write"); + asm ("mov____0x8(%ebp),%edx !16"); + asm ("push___%edx"); + asm ("mov____0x8(%ebp),%ecx !12"); + asm ("push___%ecx"); + asm ("mov____0x8(%ebp),%ebx !8"); + asm ("push___%ebx"); + asm ("push___%ebx"); + asm ("int____$0x80"); +} diff --git a/lib/freebsd/x86-mes/elf32-0exit-42.hex2 b/lib/freebsd/x86-mes/elf32-0exit-42.hex2 new file mode 100644 index 00000000..b00bc38d --- /dev/null +++ b/lib/freebsd/x86-mes/elf32-0exit-42.hex2 @@ -0,0 +1,39 @@ +### GNU Mes --- Maxwell Equations of Software +### Copyright © 2017,2019 Jan (janneke) Nieuwenhuizen +### +### This file is part of GNU Mes. +### +### GNU Mes is free software; you can redistribute it and/or modify it +### under the terms of the GNU General Public License as published by +### the Free Software Foundation; either version 3 of the License, or (at +### your option) any later version. +### +### GNU Mes is distributed in the hope that it will be useful, but +### WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with GNU Mes. If not, see . + +### Commentary: + +# elf32-exit-42.hex2: `exit 42' for i686-freebsd written in hex2 assembly, +# for usage with elf32-0header.hex2. This produces a 112-byte binary +# (70 hex). Inspiration was taken from GNU Gcc output of exit-42.S. + +### Code: + +# DEFINE SYS_exit 1 + +# @60 +:ELF_text +b8 01 00 00 00 # mov SYS_exit,%eax +68 2a 00 00 00 # push $42 +68 00 00 00 00 # push $00 +cd 80 # int $0x80 +f4 # hlt + + 00 00 00 00 00 00 # align to @80 +00 00 00 00 00 00 00 00 +:ELF_end diff --git a/lib/freebsd/x86-mes/elf32-0header.hex2 b/lib/freebsd/x86-mes/elf32-0header.hex2 new file mode 100644 index 00000000..542fa55d --- /dev/null +++ b/lib/freebsd/x86-mes/elf32-0header.hex2 @@ -0,0 +1,85 @@ +### Copyright (C) 2016 Jeremiah Orians +### Copyright (C) 2017,2019 Jan (janneke) Nieuwenhuizen +### +### This file is part of GNU Mes. +### +### GNU Mes is free software; you can redistribute it and/or modify it +### under the terms of the GNU General Public License as published by +### the Free Software Foundation; either version 3 of the License, or (at +### your option) any later version. +### +### GNU Mes is distributed in the hope that it will be useful, but +### WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with GNU Mes. If not, see . + +### Commentary: + +# elf32-0header.hex2: Simplest 32 bit elf header in hex2. Only a text +# segment, no data segment, no symbol tables. + +# stage0's hex2 format for x86 +# !