Support malloc() on bare metal

Your Actually Portable Executables now contains a simple virtual memory
that works similarly to the Linux Kernel in the sense that it maps your
physical memory to negative addresses. This is needed to support mmap()
and malloc(). This functionality has zero code size impact. For example
the MODE=tiny LIFE.COM executable is still only 12KB in size.

The APE bootloader code has also been simplified to improve readibility
and further elevate the elegance by which we're able to support so many
platforms thereby enhancing verifiability so that we may engender trust
in this bootloading process.
This commit is contained in:
Justine Tunney 2021-02-23 20:23:19 -08:00
parent ac3b1dfb21
commit edd9297eba
89 changed files with 900 additions and 1417 deletions

View file

@ -18,7 +18,7 @@
*/
#include "libc/calls/internal.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/directmap.h"
#include "libc/runtime/directmap.internal.h"
/**
* Obtains memory mapping directly from system.
@ -28,11 +28,13 @@
* bypassed by calling this function. However the caller is responsible
* for passing the magic memory handle on Windows NT to CloseHandle().
*/
noasan struct DirectMap __mmap(void *addr, size_t size, int prot, int flags,
int fd, int64_t off) {
if (!IsWindows()) {
return (struct DirectMap){sys_mmap(addr, size, prot, flags, fd, off, off),
noasan struct DirectMap sys_mmap(void *addr, size_t size, int prot, int flags,
int fd, int64_t off) {
if (!IsWindows() && !IsMetal()) {
return (struct DirectMap){__sys_mmap(addr, size, prot, flags, fd, off, off),
kNtInvalidHandleValue};
} else if (IsMetal()) {
return sys_mmap_metal(addr, size, prot, flags, fd, off);
} else {
return sys_mmap_nt(addr, size, prot, flags,
fd != -1 ? g_fds.p[fd].handle : kNtInvalidHandleValue,