commit 60c196243360163e4cb21c7d1d46cb8883410363
parent 692b08eb78c7300496f236a429d85126975ac9ca
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Thu, 19 Dec 2024 11:37:01 +0000
add mappable memory
Diffstat:
| M | Tupfile | | | 4 | ++-- |
| A | paste.c | | | 59 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | tac.c | | | 59 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | x86_64.ld | | | 14 | +++++++++++--- |
4 files changed, 131 insertions(+), 5 deletions(-)
diff --git a/Tupfile b/Tupfile
@@ -1,8 +1,8 @@
-CFLAGS = -Os -fno-asynchronous-unwind-tables -no-pie -g
+CFLAGS = -Os -fno-asynchronous-unwind-tables -no-pie -g -fno-stack-protector
LDFLAGS = -static -nostartfiles -nostdlib -no-pie -Xlinker --gc-sections
!cc = |> cc $(CFLAGS) -c -o %o %f |> obj/%B.o
-!link = | x86_64.ld obj/start.o obj/syscall.o |> cc $(LDFLAGS) -o %o -T x86_64.ld %f obj/start.o obj/syscall.o |>
+!link = | x86_64.ld obj/start.o obj/syscall.o |> cc $(LDFLAGS) -o %o -T x86_64.ld obj/start.o obj/syscall.o %f |>
: foreach *.s |> !cc |>
: foreach *.c |> !cc |> {objs}
diff --git a/paste.c b/paste.c
@@ -0,0 +1,59 @@
+#include "syscall.h"
+#include <linux/mman.h>
+
+struct input
+{
+ int fd;
+ unsigned short start, end;
+ char buf[0x1000 - 8];
+};
+
+extern struct input mappable[];
+static int n_inputs;
+
+static char getc(struct input* in)
+{
+ if (in->start == in->end)
+ {
+ in->start = 0;
+ in->end = 0;
+ int n = read(in->fd, in->buf, 0x1000-8);
+ if (n <= 0)
+ return 0;
+ in->end = n;
+ }
+ return in->buf[in->start++];
+}
+
+int main(int argc, char* argv[])
+{
+ for (int i = 1; i < argc; ++i)
+ {
+ int fd = open(argv[i], 0, 0);
+ if (fd < 0)
+ return 1;
+ struct input* m = mmap(mappable + n_inputs++, sizeof(struct input), PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+ if ((long long)m < 0)
+ return 2;
+ m->fd = fd;
+ }
+
+ for (;;)
+ {
+ for (int i = 0; i < n_inputs; ++i)
+ {
+ struct input* in = mappable + i;
+
+ for (;;)
+ {
+ char c = getc(in);
+ if (!c)
+ return 0;
+ if (c == '\n')
+ break;
+ write(1, &c, 1);
+ }
+ write(1, i + 1 == n_inputs ? "\n" : "\t", 1);
+ }
+ }
+}
diff --git a/tac.c b/tac.c
@@ -0,0 +1,59 @@
+#include "syscall.h"
+#include <linux/mman.h>
+
+extern char mappable[];
+
+static int rd(int const fd, char* const start, char const* const end)
+{
+ char* pos = start;
+ while (pos != end)
+ {
+ int n = read(fd, pos, end - pos);
+ if (n < 0)
+ exit(2);
+ if (n == 0)
+ break;
+ pos += n;
+ }
+ return pos - start;
+}
+
+static void wr(int const fd, char const* start, char const* const end)
+{
+ while (start != end)
+ {
+ int n = write(fd, start, end - start);
+ if (n < 0)
+ exit(3);
+ start += n;
+ }
+}
+
+int main()
+{
+ char* buf = mappable;
+ size_t cap = 0;
+ size_t len = 0;
+
+ while (len == cap)
+ {
+ void* m = mmap(buf + cap, cap ?: 0x1000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+ if ((long long)m < 0)
+ return 1;
+ cap += cap ?: 0x1000;
+
+ len += rd(0, buf + len, buf + cap);
+ }
+
+reverse:
+ char const* end = buf + len;
+ char const* str = end;
+
+ while (--str > buf)
+ {
+ while (str > buf && str[-1] != '\n')
+ --str;
+ wr(1, str, end);
+ end = str;
+ }
+}
diff --git a/x86_64.ld b/x86_64.ld
@@ -13,14 +13,22 @@ SECTIONS
*(.rodata)
}
- .bss :
+ .data :
+ ALIGN(0x1000)
+ {
+ *(.data)
+ }
+
+ .bss (NOLOAD) :
ALIGN(0x1000)
{
*(.bss)
}
- .data :
+
+ .mappable (OVERLAY) :
+ ALIGN(0x1000)
{
- *(.data)
+ mappable = .;
}
}