Browse Source

Add support for OpenBSD

It just rejects the generic System V ABI and has other syscall numbers.
master
Přemysl Janouch 3 years ago
parent
commit
b846a7a0ea
Signed by: Přemysl Janouch <p.janouch@gmail.com> GPG Key ID: B715679E3A361BE6
3 changed files with 21 additions and 8 deletions
  1. +5
    -3
      Makefile
  2. +3
    -3
      README.adoc
  3. +13
    -2
      bfc-amd64.c

+ 5
- 3
Makefile View File

@@ -1,11 +1,13 @@
# All we need is C99 and POSIX, which this should make available
CFLAGS = -std=gnu99
NAMES = bfc-amd64-linux
NAMES = bfc-amd64-linux bfc-amd64-openbsd

all: $(NAMES)

%: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@
%-linux: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ -DTARGET_LINUX
%-openbsd: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ -DTARGET_OPENBSD
clean:
rm -f $(NAMES)


+ 3
- 3
README.adoc View File

@@ -1,8 +1,8 @@
bfc
===

'bfc' is a small, fast, self-contained, optimizing Brainfuck compiler for Linux
on Intel x86-64.
'bfc' is a small, fast, self-contained, optimizing Brainfuck compiler for *nix
on Intel x86-64. Currently supported targets are Linux and OpenBSD.

Also included are several interpreters in various states of sophistication that
document my progress as I was writing this, from the simplest approach to an
@@ -14,7 +14,7 @@ The compiler itself is platform agnostic.
Building
--------
Build dependencies: a C99 compiler +
Runtime dependencies: Linux
Runtime dependencies: Linux or OpenBSD

$ git clone https://github.com/pjanouch/bfc.git
$ cd bfc

bfc-amd64-linux.c → bfc-amd64.c View File

@@ -580,7 +580,13 @@ main (int argc, char *argv[])
// args -> rdi, rsi, rdx, r10, r8, r9
// trashed <- rcx, r11

#ifdef TARGET_OPENBSD
enum { SYS_READ = 3, SYS_WRITE = 4, SYS_EXIT = 1 };
#elif defined TARGET_LINUX
enum { SYS_READ = 0, SYS_WRITE = 1, SYS_EXIT = 60 };
#else
#error Target not supported
#endif

CODE ("\xB8") DD (SYS_EXIT) // mov eax, 0x3c
CODE ("\x48\x31\xFF") // xor rdi, rdi
@@ -605,7 +611,7 @@ main (int argc, char *argv[])
size_t read_offset = buffer.len;
CODE ("\x50") // push rax -- save tape position
CODE ("\xB8") DD (SYS_READ) // mov eax, "SYS_READ"
CODE ("\x48\x89\xC7") // mov rdi, rax -- STDIN_FILENO
CODE ("\xBF") DD (0) // mov edi, "STDIN_FILENO"
CODE ("\x66\x6A\x00") // push word 0 -- the default value for EOF
CODE ("\x48\x89\xE6") // mov rsi, rsp -- the char starts at rsp
CODE ("\xBA") DD (1) // mov edx, 1 -- count
@@ -623,7 +629,7 @@ main (int argc, char *argv[])
size_t write_offset = buffer.len;
CODE ("\x50") // push rax -- save tape position
CODE ("\xB8") DD (SYS_WRITE) // mov eax, "SYS_WRITE"
CODE ("\x48\x89\xC7") // mov rdi, rax -- STDOUT_FILENO
CODE ("\xBF") DD (1) // mov edi, "STDOUT_FILENO"
CODE ("\x66\x53") // push bx
CODE ("\x48\x89\xE6") // mov rsi, rsp -- the char starts at rsp
CODE ("\xBA") DD (1) // mov edx, 1 -- count
@@ -683,7 +689,12 @@ main (int argc, char *argv[])

// ELF header
CODE ("\x7F" "ELF\x02\x01\x01") // ELF, 64-bit, little endian, v1
#ifdef TARGET_OPENBSD
// OpenBSD either requires its ABI or a PT_NOTE with "OpenBSD" in it
CODE ("\x0C\x00" "\0\0\0\0\0\0\0") // OpenBSD ABI, v0, padding
#else
CODE ("\x00\x00" "\0\0\0\0\0\0\0") // Unix System V ABI, v0, padding
#endif
DW (2) DW (62) DD (1) // executable, x86-64, v1
DQ (ELF_LOAD_CODE + ELF_META_SIZE) // entry point address
DQ (ELF_HEADER_SIZE) DQ (0) // program, section header offset

Loading…
Cancel
Save