mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-12 14:09:12 +00:00
Rewrite .zip.o file linker
This change takes an entirely new approach to the incremental linking of pkzip executables. The assets created by zipobj.com are now treated like debug data. After a .com.dbg is compiled, fixupobj.com should be run, so it can apply fixups to the offsets and move the zip directory to the end of the file. Since debug data doesn't get objcopy'd, a new tool has been introduced called zipcopy.com which should be run after objcopy whenever a .com file is created. This is all automated by the `cosmocc` toolchain which is rapidly becoming the new recommended approach. This change also introduces the new C23 checked arithmetic macros.
This commit is contained in:
parent
f6407d5f7c
commit
8ff48201ca
125 changed files with 1056 additions and 928 deletions
21
third_party/chibicc/test/builtin_test.c
vendored
21
third_party/chibicc/test/builtin_test.c
vendored
|
@ -1,4 +1,5 @@
|
|||
#include "libc/math.h"
|
||||
#include "libc/stdckdint.h"
|
||||
#include "third_party/chibicc/test/test.h"
|
||||
|
||||
#define FPNAN 0
|
||||
|
@ -194,28 +195,28 @@ void test_memcpy(void) {
|
|||
void test_add_overflow(void) {
|
||||
{
|
||||
int z;
|
||||
ASSERT(0, __builtin_add_overflow(2, 3, &z));
|
||||
ASSERT(0, ckd_add(&z, 2, 3));
|
||||
ASSERT(5, z);
|
||||
}
|
||||
{
|
||||
int x, y, z;
|
||||
x = 2;
|
||||
y = 3;
|
||||
ASSERT(0, __builtin_add_overflow(x, y, &z));
|
||||
ASSERT(0, ckd_add(&z, x, y));
|
||||
ASSERT(5, z);
|
||||
}
|
||||
{
|
||||
int x, y, z;
|
||||
x = 0x7fffffff;
|
||||
y = 1;
|
||||
ASSERT(1, __builtin_add_overflow(x, y, &z));
|
||||
ASSERT(1, ckd_add(&z, x, y));
|
||||
ASSERT(-2147483648, z);
|
||||
}
|
||||
{
|
||||
long x, y, z;
|
||||
x = 0x7fffffff;
|
||||
y = 1;
|
||||
ASSERT(0, __builtin_add_overflow(x, y, &z));
|
||||
ASSERT(0, ckd_add(&z, x, y));
|
||||
ASSERT(2147483648, z);
|
||||
}
|
||||
}
|
||||
|
@ -225,21 +226,21 @@ void test_sub_overflow(void) {
|
|||
int x, y, z;
|
||||
x = 2;
|
||||
y = 3;
|
||||
ASSERT(0, __builtin_sub_overflow(x, y, &z));
|
||||
ASSERT(0, ckd_sub(&z, x, y));
|
||||
ASSERT(-1, z);
|
||||
}
|
||||
{
|
||||
int x, y, z;
|
||||
x = -2147483648;
|
||||
y = 1;
|
||||
ASSERT(1, __builtin_sub_overflow(x, y, &z));
|
||||
ASSERT(1, ckd_sub(&z, x, y));
|
||||
ASSERT(2147483647, z);
|
||||
}
|
||||
{
|
||||
long x, y, z;
|
||||
x = -2147483648;
|
||||
y = 1;
|
||||
ASSERT(0, __builtin_sub_overflow(x, y, &z));
|
||||
ASSERT(0, ckd_sub(&z, x, y));
|
||||
ASSERT(-2147483649, z);
|
||||
}
|
||||
}
|
||||
|
@ -249,21 +250,21 @@ void test_mul_overflow(void) {
|
|||
int x, y, z;
|
||||
x = 2;
|
||||
y = 3;
|
||||
ASSERT(0, __builtin_mul_overflow(x, y, &z));
|
||||
ASSERT(0, ckd_mul(&z, x, y));
|
||||
ASSERT(6, z);
|
||||
}
|
||||
{
|
||||
int x, y, z;
|
||||
x = 2147483647;
|
||||
y = 2;
|
||||
ASSERT(1, __builtin_mul_overflow(x, y, &z));
|
||||
ASSERT(1, ckd_mul(&z, x, y));
|
||||
ASSERT(-2, z);
|
||||
}
|
||||
{
|
||||
long x, y, z;
|
||||
x = 2147483647;
|
||||
y = 2;
|
||||
ASSERT(0, __builtin_mul_overflow(x, y, &z));
|
||||
ASSERT(0, ckd_mul(&z, x, y));
|
||||
ASSERT(4294967294, z);
|
||||
}
|
||||
}
|
||||
|
|
3
third_party/dlmalloc/dlmalloc.c
vendored
3
third_party/dlmalloc/dlmalloc.c
vendored
|
@ -11,6 +11,7 @@
|
|||
#include "libc/nexgen32e/rdtsc.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/sysconf.h"
|
||||
#include "libc/stdckdint.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -829,7 +830,7 @@ void dlfree(void* mem) {
|
|||
void* dlcalloc(size_t n_elements, size_t elem_size) {
|
||||
void* mem;
|
||||
size_t req = 0;
|
||||
if (__builtin_mul_overflow(n_elements, elem_size, &req)) req = -1;
|
||||
if (ckd_mul(&req, n_elements, elem_size)) req = -1;
|
||||
mem = dlmalloc(req);
|
||||
if (mem != 0 && calloc_must_clear(mem2chunk(mem)))
|
||||
bzero(mem, req);
|
||||
|
|
4
third_party/linenoise/linenoise.c
vendored
4
third_party/linenoise/linenoise.c
vendored
|
@ -155,6 +155,7 @@
|
|||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sock/struct/pollfd.h"
|
||||
#include "libc/stdckdint.h"
|
||||
#include "libc/stdio/append.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
|
@ -2011,8 +2012,7 @@ ssize_t linenoiseEdit(struct linenoiseState *l, const char *prompt, char **obuf,
|
|||
itemlen = linenoiseMaxCompletionWidth(&l->lc) + 4;
|
||||
xn = MAX(1, (l->ws.ws_col - 1) / itemlen);
|
||||
yn = (l->lc.len + (xn - 1)) / xn;
|
||||
if (!__builtin_mul_overflow(xn, yn, &xy) &&
|
||||
(p = calloc(xy, sizeof(char *)))) {
|
||||
if (!ckd_mul(&xy, xn, yn) && (p = calloc(xy, sizeof(char *)))) {
|
||||
// arrange in column major order
|
||||
for (i = x = 0; x < xn; ++x) {
|
||||
for (y = 0; y < yn; ++y) {
|
||||
|
|
2
third_party/mbedtls/test/lib.c
vendored
2
third_party/mbedtls/test/lib.c
vendored
|
@ -50,7 +50,7 @@ Copyright ARM Limited\\n\
|
|||
Copyright Mbed TLS Contributors\"");
|
||||
asm(".include \"libc/disclaimer.inc\"");
|
||||
|
||||
STATIC_YOINK("zip_uri_support");
|
||||
STATIC_YOINK("zipos");
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
static mbedtls_platform_context platform_ctx;
|
||||
|
|
2
third_party/mbedtls/test/test.inc
vendored
2
third_party/mbedtls/test/test.inc
vendored
|
@ -13,4 +13,4 @@ Copyright ARM Limited\\n\
|
|||
Copyright Mbed TLS Contributors\"");
|
||||
asm(".include \"libc/disclaimer.inc\"");
|
||||
|
||||
STATIC_YOINK("zip_uri_support");
|
||||
STATIC_YOINK("zipos");
|
||||
|
|
2
third_party/python/launch.c
vendored
2
third_party/python/launch.c
vendored
|
@ -41,7 +41,7 @@
|
|||
|
||||
#define USE_COSMO_CRASH MODE_DBG + 0
|
||||
|
||||
STATIC_YOINK("zip_uri_support");
|
||||
STATIC_YOINK("zipos");
|
||||
PYTHON_YOINK("_bootlocale");
|
||||
PYTHON_YOINK("_locale");
|
||||
PYTHON_YOINK("encodings.aliases");
|
||||
|
|
17
third_party/python/pyobj.c
vendored
17
third_party/python/pyobj.c
vendored
|
@ -234,7 +234,6 @@ static bool nocompress;
|
|||
static bool isunittest;
|
||||
static bool insertrunner;
|
||||
static bool insertlauncher;
|
||||
static uint64_t image_base;
|
||||
static int strip_components;
|
||||
static struct ElfWriter *elf;
|
||||
static const char *path_prefix;
|
||||
|
@ -246,7 +245,6 @@ static void
|
|||
GetOpts(int argc, char *argv[])
|
||||
{
|
||||
int opt;
|
||||
image_base = IMAGE_BASE_VIRTUAL;
|
||||
path_prefix = ".python";
|
||||
while ((opt = getopt(argc, argv, "hnmtr0Bb:O:o:C:P:Y:")) != -1) {
|
||||
switch (opt) {
|
||||
|
@ -279,9 +277,6 @@ GetOpts(int argc, char *argv[])
|
|||
case 'C':
|
||||
strip_components = atoi(optarg);
|
||||
break;
|
||||
case 'b':
|
||||
image_base = strtoul(optarg, NULL, 0);
|
||||
break;
|
||||
case 'Y':
|
||||
yoinks.p = realloc(yoinks.p, ++yoinks.n * sizeof(*yoinks.p));
|
||||
yoinks.p[yoinks.n - 1] = xstrdup(optarg);
|
||||
|
@ -656,22 +651,18 @@ Objectify(void)
|
|||
if (ispkg) {
|
||||
elfwriter_zip(elf, zipdir, zipdir, strlen(zipdir),
|
||||
pydata, 0, 040755, timestamp, timestamp,
|
||||
timestamp, nocompress, image_base,
|
||||
kZipCdirHdrLinkableSize);
|
||||
timestamp, nocompress);
|
||||
}
|
||||
if (!binonly) {
|
||||
elfwriter_zip(elf, gc(xstrcat("py:", modname)), zipfile,
|
||||
strlen(zipfile), pydata, pysize, st.st_mode, timestamp,
|
||||
timestamp, timestamp, nocompress, image_base,
|
||||
kZipCdirHdrLinkableSize);
|
||||
timestamp, timestamp, nocompress);
|
||||
}
|
||||
elfwriter_zip(elf, gc(xstrcat("pyc:", modname)), gc(xstrcat(zipfile, 'c')),
|
||||
strlen(zipfile) + 1, pycdata, pycsize, st.st_mode, timestamp,
|
||||
timestamp, timestamp, nocompress, image_base,
|
||||
kZipCdirHdrLinkableSize);
|
||||
timestamp, timestamp, nocompress);
|
||||
elfwriter_align(elf, 1, 0);
|
||||
elfwriter_startsection(elf, ".yoink", SHT_PROGBITS,
|
||||
SHF_ALLOC | SHF_EXECINSTR);
|
||||
elfwriter_startsection(elf, ".yoink", SHT_PROGBITS, 0);
|
||||
if (!(rc = AnalyzeModule(modname))) {
|
||||
if (*path_prefix && !IsDot()) {
|
||||
elfwriter_yoink(elf, gc(xstrcat(path_prefix, "/")), STB_GLOBAL);
|
||||
|
|
2
third_party/python/python.mk
vendored
2
third_party/python/python.mk
vendored
|
@ -4194,7 +4194,7 @@ $(THIRD_PARTY_PYTHON_HELLO_OBJS): private PYFLAGS += -C2 -m
|
|||
# this directory entry is at the tip of the tree
|
||||
# therefore building it requires special care
|
||||
o/$(MODE)/third_party/python/Lib/.zip.o: third_party/python/.python
|
||||
@$(COMPILE) -wAZIPOBJ $(ZIPOBJ) -b$(IMAGE_BASE_VIRTUAL) -C2 $(OUTPUT_OPTION) third_party/python/.python
|
||||
@$(COMPILE) -wAZIPOBJ $(ZIPOBJ) -C2 $(OUTPUT_OPTION) third_party/python/.python
|
||||
|
||||
# these need to be explictly defined because landlock make won't sandbox
|
||||
# prerequisites with a trailing slash.
|
||||
|
|
2
third_party/python/runpythonmodule.c
vendored
2
third_party/python/runpythonmodule.c
vendored
|
@ -55,7 +55,7 @@
|
|||
STATIC_STACK_SIZE(0x100000);
|
||||
|
||||
STATIC_YOINK("__die");
|
||||
STATIC_YOINK("zip_uri_support");
|
||||
STATIC_YOINK("zipos");
|
||||
|
||||
PYTHON_YOINK("cosmo");
|
||||
PYTHON_YOINK("_locale");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue