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:
Justine Tunney 2023-06-10 09:15:19 -07:00
parent f6407d5f7c
commit 8ff48201ca
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
125 changed files with 1056 additions and 928 deletions

View file

@ -36,6 +36,7 @@
#include "libc/nt/runtime.h"
#include "libc/nt/struct/byhandlefileinformation.h"
#include "libc/nt/struct/overlapped.h"
#include "libc/stdckdint.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/f.h"
#include "libc/sysv/consts/fd.h"
@ -149,7 +150,7 @@ static textwindows int sys_fcntl_nt_lock(struct Fd *f, int fd, int cmd,
len = INT64_MAX - off;
}
if (off < 0 || len < 0 || __builtin_add_overflow(off, len, &end)) {
if (off < 0 || len < 0 || ckd_add(&end, off, len)) {
return einval();
}

View file

@ -22,6 +22,7 @@
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/stdckdint.h"
#include "libc/sysv/errfuns.h"
/**
@ -34,8 +35,8 @@
int getgroups(int size, uint32_t list[]) {
int rc;
size_t n;
if (IsAsan() && (__builtin_mul_overflow(size, sizeof(list[0]), &n) ||
!__asan_is_valid(list, n))) {
if (IsAsan() &&
(ckd_mul(&n, size, sizeof(list[0])) || !__asan_is_valid(list, n))) {
rc = efault();
} else if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) {
rc = sys_getgroups(size, list);

View file

@ -64,6 +64,7 @@ textstartup noasan void InitializeMetalFile(void) {
* user code can STATIC_YOINK this symbol.
*/
size_t size = ROUNDUP(_ezip - __executable_start, 4096);
// TODO(jart): Restore support for ZIPOS on metal.
void *copied_base;
struct DirectMap dm;
dm = sys_mmap_metal(NULL, size, PROT_READ | PROT_WRITE,

View file

@ -54,7 +54,7 @@
*
* If your main() source file has this statement:
*
* STATIC_YOINK("zip_uri_support");
* STATIC_YOINK("zipos");
*
* Then you can read zip assets by adding a `"/zip/..."` prefix to `file`, e.g.
*

View file

@ -22,6 +22,7 @@
#include "libc/intrin/strace.internal.h"
#include "libc/sock/struct/pollfd.h"
#include "libc/sock/struct/pollfd.internal.h"
#include "libc/stdckdint.h"
#include "libc/sysv/errfuns.h"
/**
@ -69,8 +70,8 @@ int poll(struct pollfd *fds, size_t nfds, int timeout_ms) {
uint64_t millis;
BEGIN_CANCELLATION_POINT;
if (IsAsan() && (__builtin_mul_overflow(nfds, sizeof(struct pollfd), &n) ||
!__asan_is_valid(fds, n))) {
if (IsAsan() &&
(ckd_mul(&n, nfds, sizeof(struct pollfd)) || !__asan_is_valid(fds, n))) {
rc = efault();
} else if (!IsWindows()) {
if (!IsMetal()) {

View file

@ -27,6 +27,7 @@
#include "libc/intrin/strace.internal.h"
#include "libc/sock/struct/pollfd.h"
#include "libc/sock/struct/pollfd.internal.h"
#include "libc/stdckdint.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
@ -67,10 +68,10 @@ int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
struct timespec ts, *tsp;
BEGIN_CANCELLATION_POINT;
if (IsAsan() && (__builtin_mul_overflow(nfds, sizeof(struct pollfd), &n) ||
!__asan_is_valid(fds, n) ||
(timeout && !__asan_is_valid(timeout, sizeof(timeout))) ||
(sigmask && !__asan_is_valid(sigmask, sizeof(sigmask))))) {
if (IsAsan() &&
(ckd_mul(&n, nfds, sizeof(struct pollfd)) || !__asan_is_valid(fds, n) ||
(timeout && !__asan_is_valid(timeout, sizeof(timeout))) ||
(sigmask && !__asan_is_valid(sigmask, sizeof(sigmask))))) {
rc = efault();
} else if (!IsWindows()) {
e = errno;
@ -84,8 +85,7 @@ int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
if (rc == -1 && errno == ENOSYS) {
errno = e;
if (!timeout ||
__builtin_add_overflow(timeout->tv_sec, timeout->tv_nsec / 1000000,
&millis)) {
ckd_add(&millis, timeout->tv_sec, timeout->tv_nsec / 1000000)) {
millis = -1;
}
if (sigmask) sys_sigprocmask(SIG_SETMASK, sigmask, &oldmask);
@ -93,8 +93,8 @@ int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
if (sigmask) sys_sigprocmask(SIG_SETMASK, &oldmask, 0);
}
} else {
if (!timeout || __builtin_add_overflow(
timeout->tv_sec, timeout->tv_nsec / 1000000, &millis)) {
if (!timeout ||
ckd_add(&millis, timeout->tv_sec, timeout->tv_nsec / 1000000)) {
millis = -1;
}
rc = sys_poll_nt(fds, nfds, &millis, sigmask);

View file

@ -22,6 +22,7 @@
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/stdckdint.h"
#include "libc/sysv/errfuns.h"
/**
@ -40,8 +41,8 @@
int setgroups(size_t size, const uint32_t list[]) {
int rc;
size_t n;
if (IsAsan() && (__builtin_mul_overflow(size, sizeof(list[0]), &n) ||
!__asan_is_valid(list, n))) {
if (IsAsan() &&
(ckd_mul(&n, size, sizeof(list[0])) || !__asan_is_valid(list, n))) {
rc = efault();
} else if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) {
rc = sys_setgroups(size, list);

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/struct/timespec.h"
#include "libc/limits.h"
#include "libc/stdckdint.h"
/**
* Reduces `ts` from 1e-9 to 1e-6 granularity w/ ceil rounding.
@ -45,8 +46,7 @@ int64_t timespec_tomicros(struct timespec ts) {
}
}
// convert to scalar result
if (!__builtin_mul_overflow(ts.tv_sec, 1000000ul, &us) &&
!__builtin_add_overflow(us, ts.tv_nsec, &us)) {
if (!ckd_mul(&us, ts.tv_sec, 1000000ul) && !ckd_add(&us, us, ts.tv_nsec)) {
return us;
} else if (ts.tv_sec < 0) {
return INT64_MIN;

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/struct/timespec.h"
#include "libc/limits.h"
#include "libc/stdckdint.h"
/**
* Reduces `ts` from 1e-9 to 1e-3 granularity w/ ceil rounding.
@ -44,8 +45,7 @@ int64_t timespec_tomillis(struct timespec ts) {
}
}
// convert to scalar result
if (!__builtin_mul_overflow(ts.tv_sec, 1000ul, &ms) &&
!__builtin_add_overflow(ms, ts.tv_nsec, &ms)) {
if (!ckd_mul(&ms, ts.tv_sec, 1000ul) && !ckd_add(&ms, ms, ts.tv_nsec)) {
return ms;
} else if (ts.tv_sec < 0) {
return INT64_MIN;

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/struct/timespec.h"
#include "libc/limits.h"
#include "libc/stdckdint.h"
/**
* Converts timespec to scalar.
@ -30,8 +31,7 @@
*/
int64_t timespec_tonanos(struct timespec x) {
int64_t ns;
if (!__builtin_mul_overflow(x.tv_sec, 1000000000ul, &ns) &&
!__builtin_add_overflow(ns, x.tv_nsec, &ns)) {
if (!ckd_mul(&ns, x.tv_sec, 1000000000ul) && !ckd_add(&ns, ns, x.tv_nsec)) {
return ns;
} else if (x.tv_sec < 0) {
return INT64_MIN;

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/struct/timeval.h"
#include "libc/limits.h"
#include "libc/stdckdint.h"
/**
* Converts timeval to scalar.
@ -30,8 +31,7 @@
*/
int64_t timeval_tomicros(struct timeval x) {
int64_t ns;
if (!__builtin_mul_overflow(x.tv_sec, 1000000ul, &ns) &&
!__builtin_add_overflow(ns, x.tv_usec, &ns)) {
if (!ckd_mul(&ns, x.tv_sec, 1000000ul) && !ckd_add(&ns, ns, x.tv_usec)) {
return ns;
} else if (x.tv_sec < 0) {
return INT64_MIN;

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/struct/timeval.h"
#include "libc/limits.h"
#include "libc/stdckdint.h"
/**
* Reduces `ts` from 1e-6 to 1e-3 granularity w/ ceil rounding.
@ -45,8 +46,7 @@ int64_t timeval_tomillis(struct timeval ts) {
}
}
// convert to scalar result
if (!__builtin_mul_overflow(ts.tv_sec, 1000ul, &ms) &&
!__builtin_add_overflow(ms, ts.tv_usec, &ms)) {
if (!ckd_mul(&ms, ts.tv_sec, 1000ul) && !ckd_add(&ms, ms, ts.tv_usec)) {
return ms;
} else if (ts.tv_sec < 0) {
return INT64_MIN;

View file

@ -1,36 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
// TODO(jart): DELETE
/**
* Deletes file.
*
* The caller's variable is made NULL. Note that we define unlink(NULL)
* as a no-op.
*
* @return 0 on success, or -1 w/ errno
* @asyncsignalsafe
*/
int unlink_s(const char **namep) {
const char *name = *namep;
*namep = 0;
return unlink(name);
}

View file

@ -27,14 +27,9 @@
#include "libc/intrin/bits.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/auxv.h"
static inline int CompareStrings(const char *l, const char *r) {
size_t i = 0;
while (l[i] == r[i] && r[i]) ++i;
return (l[i] & 255) - (r[i] & 255);
}
static inline int CheckDsoSymbolVersion(Elf64_Verdef *vd, int sym,
const char *name, char *strtab) {
Elf64_Verdaux *aux;
@ -42,7 +37,7 @@ static inline int CheckDsoSymbolVersion(Elf64_Verdef *vd, int sym,
if (!(vd->vd_flags & VER_FLG_BASE) &&
(vd->vd_ndx & 0x7fff) == (sym & 0x7fff)) {
aux = (Elf64_Verdaux *)((char *)vd + vd->vd_aux);
return !CompareStrings(name, strtab + aux->vda_name);
return !strcmp(name, strtab + aux->vda_name);
}
if (!vd->vd_next) {
return 0;
@ -138,7 +133,7 @@ void *__vdsosym(const char *version, const char *name) {
if (!symtab[i].st_shndx) {
continue;
}
if (CompareStrings(name, strtab + symtab[i].st_name)) {
if (strcmp(name, strtab + symtab[i].st_name)) {
continue;
}
if (versym && !CheckDsoSymbolVersion(verdef, versym[i], version, strtab)) {