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

@ -1,33 +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/elf/elf.h"
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
void CheckElfAddress(const Elf64_Ehdr *elf, size_t mapsize, intptr_t addr,
size_t addrsize) {
#if !(TRUSTWORTHY + ELF_TRUSTWORTHY + 0) || ELF_UNTRUSTWORTHY + 0
if (addr < (intptr_t)elf || addr + addrsize > (intptr_t)elf + mapsize) {
kprintf("%p-%p falls outside interval %p-%p", //
addr, addr + addrsize, //
elf, (char *)elf + mapsize); //
abort();
}
#endif
}

View file

@ -187,11 +187,13 @@
#define R_PPC_ADDR32 1
#define R_PPC_ADDR24 2
#define R_PPC_ADDR16 3
#define R_PPC_REL32 26
#define R_PPC64_NONE R_PPC_NONE
#define R_PPC64_ADDR32 R_PPC_ADDR32
#define R_PPC64_ADDR24 R_PPC_ADDR24
#define R_PPC64_ADDR16 R_PPC_ADDR16
#define R_PPC64_REL32 R_PPC_REL32
#define R_RISCV_NONE 0
#define R_RISCV_32 1

View file

@ -17,9 +17,8 @@ char *GetElfStringTable(const Elf64_Ehdr *, size_t);
char *GetElfStrs(const Elf64_Ehdr *, size_t, size_t *);
Elf64_Sym *GetElfSymbolTable(const Elf64_Ehdr *, size_t, Elf64_Xword *);
bool IsElf64Binary(const Elf64_Ehdr *, size_t);
void CheckElfAddress(const Elf64_Ehdr *, size_t, intptr_t, size_t);
bool IsElfSymbolContent(const Elf64_Sym *);
Elf64_Phdr *GetElfSegmentHeaderAddress(const Elf64_Ehdr *, size_t, unsigned);
Elf64_Phdr *GetElfSegmentHeaderAddress(const Elf64_Ehdr *, size_t, Elf64_Half);
Elf64_Shdr *GetElfSectionHeaderAddress(const Elf64_Ehdr *, size_t, Elf64_Half);
void *GetElfSectionAddress(const Elf64_Ehdr *, size_t, const Elf64_Shdr *);
char *GetElfSectionNameStringTable(const Elf64_Ehdr *, size_t);

View file

@ -21,20 +21,16 @@
#include "libc/str/str.h"
char *GetElfDynStringTable(const Elf64_Ehdr *elf, size_t mapsize) {
int i;
char *name;
Elf64_Half i;
Elf64_Shdr *shdr;
if (elf->e_shentsize) {
for (i = 0; i < elf->e_shnum; ++i) {
shdr = GetElfSectionHeaderAddress(elf, mapsize, i);
if (shdr->sh_type == SHT_STRTAB) {
name = GetElfSectionName(elf, mapsize,
GetElfSectionHeaderAddress(elf, mapsize, i));
if (name && !strcmp(name, ".dynstr")) {
return GetElfSectionAddress(elf, mapsize, shdr);
}
}
for (i = 0; i < elf->e_shnum; ++i) {
if ((shdr = GetElfSectionHeaderAddress(elf, mapsize, i)) &&
shdr->sh_type == SHT_STRTAB &&
(name = GetElfSectionName(elf, mapsize, shdr)) &&
!strcmp(name, ".dynstr")) {
return GetElfSectionAddress(elf, mapsize, shdr);
}
}
return NULL;
return 0;
}

View file

@ -21,17 +21,15 @@
Elf64_Sym *GetElfDynSymbolTable(const Elf64_Ehdr *elf, size_t mapsize,
Elf64_Xword *out_count) {
Elf64_Half i;
int i;
Elf64_Shdr *shdr;
if (elf->e_shentsize) {
for (i = elf->e_shnum; i > 0; --i) {
shdr = GetElfSectionHeaderAddress(elf, mapsize, i - 1);
if (shdr->sh_type == SHT_DYNSYM) {
if (shdr->sh_entsize != sizeof(Elf64_Sym)) continue;
if (out_count) *out_count = shdr->sh_size / shdr->sh_entsize;
return GetElfSectionAddress(elf, mapsize, shdr);
}
for (i = elf->e_shnum; i-- > 0;) {
if ((shdr = GetElfSectionHeaderAddress(elf, mapsize, i)) && //
shdr->sh_type == SHT_DYNSYM && //
shdr->sh_entsize == sizeof(Elf64_Sym)) {
if (out_count) *out_count = shdr->sh_size / sizeof(Elf64_Sym);
return GetElfSectionAddress(elf, mapsize, shdr);
}
}
return NULL;
return 0;
}

View file

@ -17,12 +17,16 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/elf/elf.h"
#include "libc/stdckdint.h"
void *GetElfSectionAddress(const Elf64_Ehdr *elf, size_t mapsize,
const Elf64_Shdr *shdr) {
intptr_t addr, size;
addr = (intptr_t)elf + (intptr_t)shdr->sh_offset;
size = (intptr_t)shdr->sh_size;
CheckElfAddress(elf, mapsize, addr, size);
// note: should not be used on bss section
void *GetElfSectionAddress(const Elf64_Ehdr *elf, // validated
size_t mapsize, // validated
const Elf64_Shdr *shdr) { // foreign
uint64_t addr, last;
if (!shdr) return 0;
if (ckd_add(&addr, (uintptr_t)elf, shdr->sh_offset)) return 0;
if (ckd_add(&last, addr, shdr->sh_size)) return 0;
if (last > (uintptr_t)elf + mapsize) return 0;
return (void *)addr;
}

View file

@ -17,11 +17,16 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/elf/elf.h"
#include "libc/stdckdint.h"
Elf64_Shdr *GetElfSectionHeaderAddress(const Elf64_Ehdr *elf, size_t mapsize,
Elf64_Half i) {
intptr_t addr =
((intptr_t)elf + (intptr_t)elf->e_shoff + (intptr_t)elf->e_shentsize * i);
CheckElfAddress(elf, mapsize, addr, elf->e_shentsize);
Elf64_Shdr *GetElfSectionHeaderAddress(const Elf64_Ehdr *elf, //
size_t mapsize, //
Elf64_Half i) { //
uint64_t addr, last;
if (i >= elf->e_shnum) return 0;
if (ckd_add(&addr, (uintptr_t)elf, elf->e_shoff)) return 0;
if (ckd_add(&addr, addr, (unsigned)i * elf->e_shentsize)) return 0;
if (ckd_add(&last, addr, elf->e_shentsize)) return 0;
if (last > (uintptr_t)elf + mapsize) return 0;
return (Elf64_Shdr *)addr;
}

View file

@ -20,7 +20,7 @@
const char *GetElfSectionName(const Elf64_Ehdr *elf, size_t mapsize,
Elf64_Shdr *shdr) {
if (!elf || !shdr) return NULL;
if (!shdr) return 0;
return GetElfString(elf, mapsize, GetElfSectionNameStringTable(elf, mapsize),
shdr->sh_name);
}

View file

@ -19,7 +19,7 @@
#include "libc/elf/elf.h"
char *GetElfSectionNameStringTable(const Elf64_Ehdr *elf, size_t mapsize) {
if (!elf->e_shoff || !elf->e_shentsize) return NULL;
if (!elf->e_shoff || !elf->e_shentsize) return 0;
return GetElfSectionAddress(
elf, mapsize, GetElfSectionHeaderAddress(elf, mapsize, elf->e_shstrndx));
}

View file

@ -17,11 +17,16 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/elf/elf.h"
#include "libc/stdckdint.h"
Elf64_Phdr *GetElfSegmentHeaderAddress(const Elf64_Ehdr *elf, size_t mapsize,
unsigned i) {
intptr_t addr =
((intptr_t)elf + (intptr_t)elf->e_phoff + (intptr_t)elf->e_phentsize * i);
CheckElfAddress(elf, mapsize, addr, elf->e_phentsize);
Elf64_Phdr *GetElfSegmentHeaderAddress(const Elf64_Ehdr *elf, //
size_t mapsize, //
Elf64_Half i) { //
uint64_t last, addr;
if (i >= elf->e_phnum) return 0;
if (ckd_add(&addr, (uintptr_t)elf, elf->e_phoff)) return 0;
if (ckd_add(&addr, addr, (unsigned)i * elf->e_phentsize)) return 0;
if (ckd_add(&last, addr, elf->e_phentsize)) return 0;
if (last > (uintptr_t)elf + mapsize) return 0;
return (Elf64_Phdr *)addr;
}

View file

@ -17,15 +17,17 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/elf/elf.h"
#include "libc/stdckdint.h"
#include "libc/str/str.h"
char *GetElfString(const Elf64_Ehdr *elf, size_t mapsize, const char *strtab,
Elf64_Word rva) {
intptr_t addr = (intptr_t)strtab + rva;
#if !(TRUSTWORTHY + ELF_TRUSTWORTHY + 0)
CheckElfAddress(elf, mapsize, addr, 0);
CheckElfAddress(elf, mapsize, addr,
strnlen((char *)addr, (intptr_t)elf + mapsize - addr) + 1);
#endif
char *GetElfString(const Elf64_Ehdr *elf, // validated
size_t mapsize, // validated
const char *strtab, // validated
Elf64_Word rva) { // foreign
uintptr_t addr;
if (!strtab) return 0;
if (ckd_add(&addr, (uintptr_t)strtab, rva)) return 0;
if (addr >= (uintptr_t)elf + mapsize) return 0;
if (!memchr((char *)addr, 0, (uintptr_t)elf + mapsize - addr)) return 0;
return (char *)addr;
}

View file

@ -21,19 +21,15 @@
#include "libc/str/str.h"
char *GetElfStringTable(const Elf64_Ehdr *elf, size_t mapsize) {
int i;
char *name;
Elf64_Half i;
Elf64_Shdr *shdr;
if (elf->e_shentsize) {
for (i = 0; i < elf->e_shnum; ++i) {
shdr = GetElfSectionHeaderAddress(elf, mapsize, i);
if (shdr->sh_type == SHT_STRTAB) {
name = GetElfSectionName(elf, mapsize,
GetElfSectionHeaderAddress(elf, mapsize, i));
if (name && !strcmp(name, ".strtab")) {
return GetElfSectionAddress(elf, mapsize, shdr);
}
}
for (i = 0; i < elf->e_shnum; ++i) {
if ((shdr = GetElfSectionHeaderAddress(elf, mapsize, i)) &&
shdr->sh_type == SHT_STRTAB &&
(name = GetElfSectionName(elf, mapsize, shdr)) &&
!strcmp(name, ".strtab")) {
return GetElfSectionAddress(elf, mapsize, shdr);
}
}
return NULL;

View file

@ -21,18 +21,16 @@
#include "libc/str/str.h"
char *GetElfStrs(const Elf64_Ehdr *elf, size_t mapsize, size_t *out_size) {
int i;
char *name;
Elf64_Half i;
Elf64_Shdr *shdr;
for (i = 0; i < elf->e_shnum; ++i) {
shdr = GetElfSectionHeaderAddress(elf, mapsize, i);
if (shdr->sh_type == SHT_STRTAB) {
name = GetElfSectionName(elf, mapsize,
GetElfSectionHeaderAddress(elf, mapsize, i));
if (name && !strcmp(name, ".strtab")) {
if (out_size) *out_size = shdr->sh_size;
return GetElfSectionAddress(elf, mapsize, shdr);
}
if ((shdr = GetElfSectionHeaderAddress(elf, mapsize, i)) &&
shdr->sh_type == SHT_STRTAB &&
(name = GetElfSectionName(elf, mapsize, shdr)) &&
!strcmp(name, ".strtab")) {
if (out_size) *out_size = shdr->sh_size;
return GetElfSectionAddress(elf, mapsize, shdr);
}
}
return 0;

View file

@ -21,14 +21,14 @@
Elf64_Sym *GetElfSymbolTable(const Elf64_Ehdr *elf, size_t mapsize,
Elf64_Xword *out_count) {
Elf64_Half i;
int i;
Elf64_Shdr *shdr;
if (elf->e_shentsize) {
for (i = elf->e_shnum; i > 0; --i) {
shdr = GetElfSectionHeaderAddress(elf, mapsize, i - 1);
if (shdr->sh_type == SHT_SYMTAB) {
if (shdr->sh_entsize != sizeof(Elf64_Sym)) continue;
if (out_count) *out_count = shdr->sh_size / shdr->sh_entsize;
if (shdr->sh_entsize != sizeof(Elf64_Sym)) __builtin_trap();
if (out_count) *out_count = shdr->sh_size / sizeof(Elf64_Sym);
return GetElfSectionAddress(elf, mapsize, shdr);
}
}

View file

@ -2,10 +2,11 @@
#define COSMOPOLITAN_LIBC_ELF_STRUCT_EHDR_H_
#include "libc/elf/def.h"
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#define EI_NIDENT 16
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Ehdr {
unsigned char e_ident[EI_NIDENT];
Elf64_Half e_type;

View file

@ -16,14 +16,14 @@ COSMOPOLITAN_C_START_
#define GetSectionName(e, s) GetStr(GetShstrtab(e), (s)->sh_name)
#define GetPhdr(e, i) \
((Elf64_Phdr *)((intptr_t)(e) + (e)->e_phoff + \
(size_t)(e)->e_phentsize * (i)))
(unsigned)(e)->e_phentsize * (i)))
#define GetShdr(e, i) \
((Elf64_Shdr *)((intptr_t)(e) + (e)->e_shoff + \
(size_t)(e)->e_shentsize * (i)))
(unsigned)(e)->e_shentsize * (i)))
static inline char *GetStrtab(Elf64_Ehdr *e, size_t *n) {
int i;
char *name;
Elf64_Half i;
Elf64_Shdr *shdr;
for (i = 0; i < e->e_shnum; ++i) {
shdr = GetShdr(e, i);
@ -39,13 +39,12 @@ static inline char *GetStrtab(Elf64_Ehdr *e, size_t *n) {
}
static inline Elf64_Sym *GetSymtab(Elf64_Ehdr *e, Elf64_Xword *n) {
Elf64_Half i;
int i;
Elf64_Shdr *shdr;
for (i = e->e_shnum; i > 0; --i) {
shdr = GetShdr(e, i - 1);
for (i = e->e_shnum; i-- > 0;) {
shdr = GetShdr(e, i);
if (shdr->sh_type == SHT_SYMTAB) {
if (shdr->sh_entsize != sizeof(Elf64_Sym)) continue;
if (n) *n = shdr->sh_size / shdr->sh_entsize;
if (n) *n = shdr->sh_size / sizeof(Elf64_Sym);
return GetSection(e, shdr);
}
}