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.
This commit is contained in:
Jan Nieuwenhuizen 2019-12-11 08:21:51 +00:00
parent e4f293afdd
commit ec9aecea07
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
13 changed files with 949 additions and 0 deletions

View file

@ -0,0 +1,76 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <mes/lib-mini.h>
//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"
);
}

View file

@ -0,0 +1,47 @@
/*
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/** 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. */

View file

@ -0,0 +1,60 @@
/*
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/** 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

View file

@ -0,0 +1,64 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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;
}

View file

@ -0,0 +1,65 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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");
}

View file

@ -0,0 +1,43 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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");
}

View file

@ -0,0 +1,39 @@
### GNU Mes --- Maxwell Equations of Software
### Copyright © 2017,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
###
### 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 <http://www.gnu.org/licenses/>.
### 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

View file

@ -0,0 +1,85 @@
### Copyright (C) 2016 Jeremiah Orians
### Copyright (C) 2017,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
###
### 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 <http://www.gnu.org/licenses/>.
### 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
# !<label> 1 byte relative
# $<label> 2 byte address
# @<label> 2 byte relative
# &<label> 4 byte address
# %<label> 4 byte relative
# local_<label> function-local
# string_<index> string #<index>
### Code:
:ELF_base
7F 45 4C 46 # e_ident[EI_MAG0-3] ELF's magic number
01 # e_ident[EI_CLASS] Indicating 32 bit
01 # e_ident[EI_DATA] Indicating little endianness
01 # e_ident[EI_VERSION] Indicating original elf
09 # e_ident[EI_OSABI] FreeBSD
00 # e_ident[EI_ABIVERSION] Not specified because nobody cares
00 00 00 00 00 00 00 # e_ident[EI_PAD]
02 00 # e_type Indicating Executable
03 00 # e_machine Indicating 32bit x86
01 00 00 00 # e_version Indicating original elf
&ELF_text # e_entry Address of the entry point
%ELF_program_headers>ELF_base # e_phoff Address of program header table
00 00 00 00 # e_shoff Address of section header table
00 00 00 00 # e_flags
34 00 # e_ehsize Indicating our 52 Byte header
20 00 # e_phentsize size of a program header table
01 00 # e_phnum number of entries in program table
00 00 # e_shentsize size of a section header table
00 00 # e_shnum number of entries in section table
00 00 # e_shstrndx index of the section names
# @34
00 00 00 00
00 00 00 00
00 00 00 00
# @40
:ELF_program_headers
:ELF_program_header__text
01 00 00 00 # ph_type: PT-LOAD = 1
00 00 00 00 # ph_offset
&ELF_base # ph_vaddr
&ELF_base # ph_physaddr
%ELF_end>ELF_base # ph_filesz
%ELF_end>ELF_base # ph_memsz
07 00 00 00 # ph_flags: PF-X|PF-W|PF-R = 7
01 00 00 00 # ph_align
# @60
:ELF_text

View file

@ -0,0 +1,58 @@
### GNU Mes --- Maxwell Equations of Software
### Copyright © 2017,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
###
### 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 <http://www.gnu.org/licenses/>.
### Commentary:
# elf32-0hello-mes.hex2: `Hello, GNU Mes!' for i686-linux written in
# hex2 assembly, for usage with elf32-0header.hex2. This produces a
# 160-byte binary (a0 hex). Inspiration was taken from GNU Gcc output
# of hello-mes.S.
### Code:
# DEFINE SYS_exit 1
# DEFINE SYS_write 4
# DEFINE stdout 1
# @60
:ELF_text
# print <hello>
b8 04 00 00 00 # mov $SYS_write,%eax
68 %bye>hello # push $(bye-hello)
68 &hello # push $hello
68 01 00 00 00 # push $stdout
68 00 00 00 00 # push ??
cd 80 # int $0x80
# exit 0
b8 01 00 00 00 # mov $SYS_exit,%eax
68 00 00 00 00 # mov $0,%ebx
68 00 00 00 00 # push ??
cd 80 # int $0x80
f4 # hlt
00 00 00 # align to a0
# @a0
:ELF_data
:hello
48 65 6c 6c 6f 2c 20 47 # Hello, G
4e 55 20 4d 65 73 21 0a # NU Mes!\n
:bye
:ELF_end

View file

@ -0,0 +1,54 @@
### GNU Mes --- Maxwell Equations of Software
### Copyright © 2017,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
###
### 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 <http://www.gnu.org/licenses/>.
### Commentary:
# elf32-body-exit-42.hex2: `exit 42' for i686-freebsd written in hex2
# assembly, for usage with generic elf32-header.hex2 and
# elf32-footer-single-main.hex.
# This ELF binary contains a symbol table, which means that objdump and
# gdb can be used to inspect and debug.
### Code:
# DEFINE SYS_exit 1
# @200
:ELF_text
:_start
e8 %main # call +11 <main>
00 00 00
00 00 00 00
00 00 00 00
# @210
:main
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
00 00 00 00 00 00 00 00
# @230
:ELF_data
65 78 69 74 34 32 20 64 61 74 61 20 68 65 72 65 # exit42 data here"
00

View file

@ -0,0 +1,69 @@
### GNU Mes --- Maxwell Equations of Software
### Copyright © 2017,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
###
### 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 <http://www.gnu.org/licenses/>.
### Commentary:
# elf32-body-hello-mes.hex2: `Hello, GNU Mes!' for i686-linux written in
# hex2 assembly, for usage with generic elf32-header.hex2 and
# elf32-footer-single-main.hex.
# This ELF binary contains a symbol table which means that objdump and
# gdb can be used to inspect and debug.
### Code:
# DEFINE SYS_exit 1
# DEFINE SYS_write 4
# DEFINE stdout 1
# @200
:ELF_text
:_start
e8 %main # call +11 <main>
00 00 00
00 00 00 00
00 00 00 00
# @210
:main
# print <hello>
b8 04 00 00 00 # mov $SYS_write,%eax
68 %bye>hello # push $(bye-hello)
68 &hello # push $hello
68 01 00 00 00 # push $stdout
68 00 00 00 00 # push ??
cd 80 # int $0x80
# exit 0
b8 01 00 00 00 # mov $SYS_exit,%eax
68 00 00 00 00 # push 0
68 00 00 00 00 # push ??
cd 80 # int $0x80
f4 # hlt
00 00 00 00 00 00 00 00 00 00 # align to @240
00 00 00
# @240
:ELF_data
:hello
48 65 6c 6c 6f 2c 20 47 # Hello, G
4e 55 20 4d 65 73 21 0a # NU Mes!\n
:bye
00

View file

@ -0,0 +1,64 @@
### Copyright (C) 2017 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
### This file is part of stage0.
###
### stage0 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.
###
### stage0 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 stage0. If not, see <http://www.gnu.org/licenses/>.
### stage0's hex2 format for x86
### !<label> 1 byte relative
### $<label> 2 byte address
### @<label> 2 byte relative
### &<label> 4 byte address
### %<label> 4 byte relative
### local_<label> function-local
### string_<index> string #<index>
### elf32-footer-single-main.hex2: 32 bit elf footer in hex2 for single main
# @230
:ELF_str
00 # 0
:ELF_str__start
5f 73 74 61 72 74 00 # _start
:ELF_str__main
6d 61 69 6e 00 # main
00 00 00
# @240
:ELF_sym
00 00 00 00 # st-name
00 00 00 00 # st-offset: &_start - BaseAddress
00 00 00 00 # st-len : &main - _start
00 # st-info = stt-func= 2
00 # st-other
01 00 # st-shndx: 1
# _start
%ELF_str__start>ELF_str # st-name
&_start
10 00 00 00 # st-len : &main - _start
02 # st-info = stt-func= 2
00 # st-other
01 00 # st-shndx: 1
# main
%ELF_str__main>ELF_str # st-name
&main
10 00 00 00 # st-len : &ELF_data - main
02 # st-info = stt-func= 2
00 # st-other
01 00 # st-shndx: 1
:ELF_end

View file

@ -0,0 +1,225 @@
### Copyright (C) 2016 Jeremiah Orians
### Copyright (C) 2017,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
###
### 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 <http://www.gnu.org/licenses/>.
### Commentary:
# elf32-header.hex2: 32 bit elf header in hex2, with text segment, data
# segment and symbol tables.
# stage0's hex2 format for x86
# !<label> 1 byte relative
# $<label> 2 byte address
# @<label> 2 byte relative
# &<label> 4 byte address
# %<label> 4 byte relative
# local_<label> function-local
# string_<index> string #<index>
### Code:
:ELF_base
7F 45 4C 46 # e_ident[EI_MAG0-3] ELF's magic number
01 # e_ident[EI_CLASS] Indicating 32 bit
01 # e_ident[EI_DATA] Indicating little endianness
01 # e_ident[EI_VERSION] Indicating original elf
09 # e_ident[EI_OSABI] FreeBSD
00 # e_ident[EI_ABIVERSION] Not specified because nobody cares
00 00 00 00 00 00 00 # e_ident[EI_PAD]
02 00 # e_type Indicating Executable
03 00 # e_machine Indicating 32bit x86
01 00 00 00 # e_version Indicating original elf
&ELF_text # e_entry Address of the entry point
%ELF_program_headers>ELF_base # e_phoff Address of program header table
%ELF_section_headers>ELF_base # e_shoff Address of section header table
00 00 00 00 # e_flags
34 00 # e_ehsize Indicating our 52 Byte header
20 00 # e_phentsize size of a program header table
01 00 # e_phnum number of entries in program table
28 00 # e_shentsize size of a section header table
07 00 # e_shnum number of entries in section table
04 00 # e_shstrndx index of the section names
# @34
00 00 00 00
00 00 00 00
00 00 00 00
# @40
:ELF_program_headers
:ELF_program_header__text
01 00 00 00 # ph_type: PT-LOAD = 1
00 00 00 00 # ph_offset
&ELF_base # ph_vaddr
&ELF_base # ph_physaddr
%ELF_end>ELF_base # ph_filesz
%ELF_end>ELF_base # ph_memsz
07 00 00 00 # ph_flags: PF-X|PF-W|PF-R = 7
01 00 00 00 # ph_align
# @60
#:ELF_program_header__data # NOT USED
# FIXME: linux 4.17 does not allow this overlap
# Uhuuh, elf segment at 0000000001000000
# requested but the memory is mapped already
01 00 00 00 # ph_type: PT-LOAD = 1
00 00 00 00 # ph_offset
&ELF_base # ph_vaddr
&ELF_base # ph_physaddr
%ELF_end>ELF_base # ph_filesz
%ELF_end>ELF_base # ph_memsz
07 00 00 00 # ph_flags: PF-X|PF-W|PF-R = 7
01 00 00 00 # ph_align
# @80
:ELF_comment
4d 45 53 00 # MES
00 00 00 00 # align
00 00 00 00
00 00 00 00
4d 45 53 00 # MES
00 00 00 00 # align
00 00 00 00
00 00 00 00
# @a0
:ELF_shstr
00
:ELF_shstr__text
2e 74 65 78 74 00 # .text
:ELF_shstr__data
2e 64 61 74 61 00 # .data
:ELF_shstr__comment
2e 63 6f 6d 6d 65 6e 74 00 # .comment
:ELF_shstr__shstr
2e 73 68 73 74 72 74 61 62 00 # .shstrtab
:ELF_shstr__sym
2e 73 79 6d 74 61 62 00 # .symtab
:ELF_shstr__str
2e 73 74 72 74 61 62 00 # .strtab
# @d0
:ELF_section_headers
00 00 00 00 # sh_name
00 00 00 00 # sh_type
00 00 00 00 # sh_flags
00 00 00 00 # sh_addr
00 00 00 00 # sh_offset
00 00 00 00 # sh_length
00 00 00 00 # sh_link
00 00 00 00 # sh_info
01 00 00 00 # sh_1?
00 00 00 00 # sh_entsize
## FIXME: M0 for calculations?
:ELF_section_header_text
%ELF_shstr__text>ELF_shstr # sh_name
01 00 00 00 # sh_type = SHT_PROGBITS = 1
06 00 00 00 # sh_flags = SHF-ALLOC|SHF-EXEC =2 | 4 = 6
&ELF_text # sh_addr
%ELF_text>ELF_base # sh_offset
%ELF_data>ELF_text # sh_length
00 00 00 00 # sh_link
00 00 00 00 # sh_info
01 00 00 00 # sh_1?
00 00 00 00 # sh_entsize
:ELF_section_header_data
%ELF_shstr__data>ELF_shstr # sh_name
01 00 00 00 # sh_type = SHT_PROGBITS = 1
03 00 00 00 # sh_flags = SHF-WRITE|SHF-ALLOC = 1 | 2 = 3
&ELF_data # sh_addr
%ELF_data>ELF_base # sh_offset
%ELF_sym>ELF_data # sh_length
00 00 00 00 # sh_link
00 00 00 00 # sh_info
01 00 00 00 # sh_1?
00 00 00 00 # sh_entsize
:ELF_section_header_comment
%ELF_shstr__comment>ELF_shstr # sh_name
01 00 00 00 # sh_type = SHT_PROGBITS = 1
00 00 00 00 # sh_flags
&ELF_comment # sh_addr
%ELF_comment>ELF_base # sh_offset
%ELF_shstr>ELF_comment # sh_length
00 00 00 00 # sh_link
00 00 00 00 # sh_info
01 00 00 00 # sh_1?
00 00 00 00 # sh_entsize
:ELF_section_header_shstr
%ELF_shstr__shstr>ELF_shstr # sh_name
03 00 00 00 # sh_type: str-sht-strtab
00 00 00 00 # sh_flags
&ELF_shstr # sh_addr
%ELF_shstr>ELF_base # sh_offset
%ELF_section_headers>ELF_shstr # sh_length
00 00 00 00 # sh_link
00 00 00 00 # sh_info
01 00 00 00 # sh_1?
00 00 00 00 # sh_entsize
:ELF_section_header_sym
%ELF_shstr__sym>ELF_shstr # sh_name
02 00 00 00 # sh_type: str-sht-symtab
00 00 00 00 # sh_flags
&ELF_sym # sh_addr
%ELF_sym>ELF_base # sh_offset
%ELF_end>ELF_sym # sh_length
06 00 00 00 # sh_link:6
00 00 00 00 # sh_info
01 00 00 00 # sh_1?
10 00 00 00 # sh_entsize
:ELF_section_header_str
%ELF_shstr__str>ELF_shstr # sh_name
03 00 00 00 # sh_type: str-sht-strtab
00 00 00 00 # sh_flags
&ELF_str # sh_addr
%ELF_str>ELF_base # sh_offset
%ELF_sym>ELF_str # sh_length
00 00 00 00 # sh_link
00 00 00 00 # sh_info
01 00 00 00 # sh_1?
00 00 00 00 # sh_entsize
# @1e8
00 00 00 00 # align
00 00 00 00
# @1f0
00 00 00 00 # align
00 00 00 00
00 00 00 00
00 00 00 00
# @200
:ELF_text