mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 16:52:28 +00:00
Rewrite ZipOS
This reduces the virtual memory usage of Emacs for me by 30%. We now have a simpler implementation that uses read(), rather mmap()ing the whole executable.
This commit is contained in:
parent
ff77f2a6af
commit
b01282e23e
21 changed files with 408 additions and 421 deletions
|
@ -16,127 +16,89 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/promises.internal.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/runtime/zipos.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "libc/zip.internal.h"
|
||||
#include "third_party/puff/puff.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
|
||||
__static_yoink("__get_symbol");
|
||||
|
||||
static pthread_spinlock_t g_lock;
|
||||
struct SymbolTable *__symtab; // for kprintf
|
||||
struct SymbolTableLoader __symtab;
|
||||
|
||||
static ssize_t GetZipFile(struct Zipos *zipos, const char *name) {
|
||||
size_t i, n, c, z;
|
||||
z = strlen(name);
|
||||
c = GetZipCdirOffset(zipos->cdir);
|
||||
n = GetZipCdirRecords(zipos->cdir);
|
||||
for (i = 0; i < n; ++i, c += ZIP_CFILE_HDRSIZE(zipos->map + c)) {
|
||||
if (ZIP_CFILE_NAMESIZE(zipos->map + c) == z &&
|
||||
!memcmp(ZIP_CFILE_NAME(zipos->map + c), name, z)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads symbol table from zip directory.
|
||||
* @note This code can't depend on dlmalloc()
|
||||
*/
|
||||
static struct SymbolTable *GetSymbolTableFromZip(struct Zipos *zipos) {
|
||||
ssize_t cf, lf;
|
||||
size_t size, size2;
|
||||
static struct SymbolTable *GetSymbolTableFromZip(void) {
|
||||
int fd;
|
||||
struct SymbolTable *res = 0;
|
||||
if ((cf = GetZipFile(zipos, ".symtab." _ARCH_NAME)) != -1 ||
|
||||
(cf = GetZipFile(zipos, ".symtab")) != -1) {
|
||||
lf = GetZipCfileOffset(zipos->map + cf);
|
||||
size = GetZipLfileUncompressedSize(zipos->map + lf);
|
||||
size2 = ROUNDUP(size, FRAMESIZE);
|
||||
if ((res = _mapanon(size2))) {
|
||||
switch (ZIP_LFILE_COMPRESSIONMETHOD(zipos->map + lf)) {
|
||||
case kZipCompressionNone:
|
||||
memcpy(res, (void *)ZIP_LFILE_CONTENT(zipos->map + lf), size);
|
||||
break;
|
||||
case kZipCompressionDeflate:
|
||||
if (__inflate((void *)res, size,
|
||||
(void *)ZIP_LFILE_CONTENT(zipos->map + lf),
|
||||
GetZipLfileCompressedSize(zipos->map + lf))) {
|
||||
munmap(res, size2);
|
||||
res = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
munmap(res, size2);
|
||||
res = 0;
|
||||
break;
|
||||
}
|
||||
if ((fd = open("/zip/.symtab." _ARCH_NAME, O_RDONLY)) != -1 ||
|
||||
(fd = open("/zip/.symtab", O_RDONLY)) != -1) {
|
||||
void *map;
|
||||
ssize_t size;
|
||||
if ((size = lseek(fd, 0, SEEK_END)) != -1 &&
|
||||
(map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) !=
|
||||
MAP_FAILED) {
|
||||
res = map;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
STRACE("GetSymbolTableFromZip() → %p", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads symbol table from .com.dbg file.
|
||||
* @note This code can't depend on dlmalloc()
|
||||
*/
|
||||
static struct SymbolTable *GetSymbolTableFromElf(void) {
|
||||
const char *s;
|
||||
if (PLEDGED(RPATH) && (s = FindDebugBinary())) {
|
||||
return OpenSymbolTable(s);
|
||||
const char *path;
|
||||
if ((path = FindDebugBinary())) {
|
||||
return OpenSymbolTable(path);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void GetSymbolTableInit(void) {
|
||||
if (!PLEDGED(RPATH)) return;
|
||||
int e = errno;
|
||||
if ((__symtab.st = GetSymbolTableFromZip())) {
|
||||
__symtab.st->names =
|
||||
(uint32_t *)((char *)__symtab.st + __symtab.st->names_offset);
|
||||
__symtab.st->name_base =
|
||||
(char *)((char *)__symtab.st + __symtab.st->name_base_offset);
|
||||
}
|
||||
if (!__symtab.st) {
|
||||
__symtab.st = GetSymbolTableFromElf();
|
||||
}
|
||||
errno = e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns symbol table singleton.
|
||||
*
|
||||
* This uses multiple strategies to find the symbol table. The first
|
||||
* strategy, depends on whether or not the following is linked:
|
||||
*
|
||||
* __static_yoink("__zipos_get");
|
||||
* __static_yoink("zipos");
|
||||
*
|
||||
* In that case, the symbol table may be read from `/zip/.symtab` which
|
||||
* is generated by `o//tool/build/symtab.com`. The second strategy is to
|
||||
* look for the concomitant `.com.dbg` executable, which may very well
|
||||
* be the one currently executing, or it could be placed in the same
|
||||
* folder as your `.com` binary, or lastly, it could be explicitly
|
||||
* specified via the `COMDBG` environment variable.
|
||||
* In that case, the symbol table may be read from `/zip/.symtab.ARCH`
|
||||
* or `/zip/.symtab` which are generated by `o//tool/build/symtab.com`
|
||||
* or `o//tool/build/apelink.com`.
|
||||
*
|
||||
* Function tracing is disabled throughout the duration of this call.
|
||||
* Backtraces and other core runtime functionality depend on this.
|
||||
* The second strategy is to look for the ELF executable for the current
|
||||
* program. If you're running a .com binary, it'll look for the .com.dbg
|
||||
* file. If it's running the .com.dbg or .elf file, then it'll just read
|
||||
* the symbols from itself. In other cases, you can explicitly specify a
|
||||
* debug symbol binary via the `COMDBG` environment variable.
|
||||
*
|
||||
* When using pledge() security, it's recommended that this function get
|
||||
* called *before* calling pledge() if it lacks the rpath promise, so it
|
||||
* can load the symbols into memory before the filesystem goes away.
|
||||
*
|
||||
* @return symbol table, or NULL if not found
|
||||
*/
|
||||
struct SymbolTable *GetSymbolTable(void) {
|
||||
struct Zipos *z;
|
||||
if (pthread_spin_trylock(&g_lock)) return 0;
|
||||
if (!__symtab && !__isworker) {
|
||||
if (_weaken(__zipos_get) && (z = _weaken(__zipos_get)())) {
|
||||
if ((__symtab = GetSymbolTableFromZip(z))) {
|
||||
__symtab->names =
|
||||
(uint32_t *)((char *)__symtab + __symtab->names_offset);
|
||||
__symtab->name_base =
|
||||
(char *)((char *)__symtab + __symtab->name_base_offset);
|
||||
}
|
||||
}
|
||||
if (!__symtab) {
|
||||
__symtab = GetSymbolTableFromElf();
|
||||
}
|
||||
}
|
||||
pthread_spin_unlock(&g_lock);
|
||||
return __symtab;
|
||||
cosmo_once(&__symtab.once, GetSymbolTableInit);
|
||||
return __symtab.st;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue