Browse Source

Add support for OpenBSD

It just rejects the generic System V ABI and has other syscall numbers.
Přemysl Janouch 2 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 @@
1 1
 # All we need is C99 and POSIX, which this should make available
2 2
 CFLAGS = -std=gnu99
3
-NAMES = bfc-amd64-linux
3
+NAMES = bfc-amd64-linux bfc-amd64-openbsd
4 4
 
5 5
 all: $(NAMES)
6 6
 
7
-%: %.c
8
-	$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@
7
+%-linux: %.c
8
+	$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ -DTARGET_LINUX
9
+%-openbsd: %.c
10
+	$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ -DTARGET_OPENBSD
9 11
 clean:
10 12
 	rm -f $(NAMES)
11 13
 

+ 3
- 3
README.adoc View File

@@ -1,8 +1,8 @@
1 1
 bfc
2 2
 ===
3 3
 
4
-'bfc' is a small, fast, self-contained, optimizing Brainfuck compiler for Linux
5
-on Intel x86-64.
4
+'bfc' is a small, fast, self-contained, optimizing Brainfuck compiler for *nix
5
+on Intel x86-64.  Currently supported targets are Linux and OpenBSD.
6 6
 
7 7
 Also included are several interpreters in various states of sophistication that
8 8
 document my progress as I was writing this, from the simplest approach to an
@@ -14,7 +14,7 @@ The compiler itself is platform agnostic.
14 14
 Building
15 15
 --------
16 16
 Build dependencies: a C99 compiler +
17
-Runtime dependencies: Linux
17
+Runtime dependencies: Linux or OpenBSD
18 18
 
19 19
  $ git clone https://github.com/pjanouch/bfc.git
20 20
  $ cd bfc

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

@@ -580,7 +580,13 @@ main (int argc, char *argv[])
580 580
 	//   args -> rdi, rsi, rdx, r10, r8, r9
581 581
 	//   trashed <- rcx, r11
582 582
 
583
+#ifdef TARGET_OPENBSD
584
+	enum { SYS_READ = 3, SYS_WRITE = 4, SYS_EXIT = 1 };
585
+#elif defined TARGET_LINUX
583 586
 	enum { SYS_READ = 0, SYS_WRITE = 1, SYS_EXIT = 60 };
587
+#else
588
+#error Target not supported
589
+#endif
584 590
 
585 591
 	CODE ("\xB8") DD (SYS_EXIT)  // mov eax, 0x3c
586 592
 	CODE ("\x48\x31\xFF")        // xor rdi, rdi
@@ -605,7 +611,7 @@ main (int argc, char *argv[])
605 611
 	size_t read_offset = buffer.len;
606 612
 	CODE ("\x50")                // push rax -- save tape position
607 613
 	CODE ("\xB8") DD (SYS_READ)  // mov eax, "SYS_READ"
608
-	CODE ("\x48\x89\xC7")        // mov rdi, rax -- STDIN_FILENO
614
+	CODE ("\xBF") DD (0)         // mov edi, "STDIN_FILENO"
609 615
 	CODE ("\x66\x6A\x00")        // push word 0 -- the default value for EOF
610 616
 	CODE ("\x48\x89\xE6")        // mov rsi, rsp -- the char starts at rsp
611 617
 	CODE ("\xBA") DD (1)         // mov edx, 1 -- count
@@ -623,7 +629,7 @@ main (int argc, char *argv[])
623 629
 	size_t write_offset = buffer.len;
624 630
 	CODE ("\x50")                // push rax -- save tape position
625 631
 	CODE ("\xB8") DD (SYS_WRITE) // mov eax, "SYS_WRITE"
626
-	CODE ("\x48\x89\xC7")        // mov rdi, rax -- STDOUT_FILENO
632
+	CODE ("\xBF") DD (1)         // mov edi, "STDOUT_FILENO"
627 633
 	CODE ("\x66\x53")            // push bx
628 634
 	CODE ("\x48\x89\xE6")        // mov rsi, rsp -- the char starts at rsp
629 635
 	CODE ("\xBA") DD (1)         // mov edx, 1 -- count
@@ -683,7 +689,12 @@ main (int argc, char *argv[])
683 689
 
684 690
 	// ELF header
685 691
 	CODE ("\x7F" "ELF\x02\x01\x01")     // ELF, 64-bit, little endian, v1
692
+#ifdef TARGET_OPENBSD
693
+	// OpenBSD either requires its ABI or a PT_NOTE with "OpenBSD" in it
694
+	CODE ("\x0C\x00" "\0\0\0\0\0\0\0")  // OpenBSD ABI, v0, padding
695
+#else
686 696
 	CODE ("\x00\x00" "\0\0\0\0\0\0\0")  // Unix System V ABI, v0, padding
697
+#endif
687 698
 	DW (2) DW (62) DD (1)               // executable, x86-64, v1
688 699
 	DQ (ELF_LOAD_CODE + ELF_META_SIZE)  // entry point address
689 700
 	DQ (ELF_HEADER_SIZE) DQ (0)         // program, section header offset

Loading…
Cancel
Save