Decentralize Python native module linkage

We can now link even smaller Python binaries. For example, the hello.com
program in the Python build directory is a compiled linked executable of
hello.py which just prints hello world. Using decentralized sections, we
can make that binary 1.9mb in size (noting that python.com is 6.3 megs!)

This works for nontrivial programs too. For example, say we want an APE
binary that's equivalent to python.com -m http.server. Our makefile now
builds such a binary using the new launcher and it's only 3.2mb in size
since Python sources get turned into ELF objects, which tell our linker
that we need things like native hashing algorithm code.
This commit is contained in:
Justine Tunney 2021-09-07 11:40:11 -07:00
parent dfa0359b50
commit 559b024e1d
129 changed files with 2798 additions and 13514 deletions

View file

@ -8,6 +8,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/errno.h"
#include "libc/log/log.h"
#include "libc/mem/alloca.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
@ -125,12 +126,12 @@ wchar_t *Py_GetProgramName(void);
#define LANDMARK L"os.py"
#endif
static const wchar_t limited_search_path[] = L"/zip/.python";
#define LIMITED_SEARCH_PATH L"/zip/.python"
static wchar_t *progpath;
static wchar_t *prefix = limited_search_path;
static wchar_t *exec_prefix = limited_search_path;
static wchar_t *module_search_path = limited_search_path;
static wchar_t *prefix = LIMITED_SEARCH_PATH;
static wchar_t *exec_prefix = LIMITED_SEARCH_PATH;
static wchar_t *module_search_path = LIMITED_SEARCH_PATH;
/* Get file status. Encode the path to the locale encoding. */
@ -667,3 +668,11 @@ Py_GetProgramFullPath(void)
}
return progpath;
}
void
Py_LimitedPath(void)
{
prefix = wcscpy(PyMem_RawMalloc((wcslen(LIMITED_SEARCH_PATH) + 1) * 4), LIMITED_SEARCH_PATH);
exec_prefix = wcscpy(PyMem_RawMalloc((wcslen(LIMITED_SEARCH_PATH) + 1) * 4), LIMITED_SEARCH_PATH);
module_search_path = wcscpy(PyMem_RawMalloc((wcslen(LIMITED_SEARCH_PATH) + 1) * 4), LIMITED_SEARCH_PATH);
}