Add support for symbol table in .com files

This change fixes minor bugs and adds a feature, which lets us store the
ELF symbol table, inside the ZIP directory. We use the path /zip/.symtab
which can be safely removed using a zip editing tool, to make the binary
smaller after compilation. This supplements the existing method of using
a separate .com.dbg file, which is still supported. The intent is people
don't always know that it's a good idea to download the debug file. It's
not great having someone's first experience be a crash report, that only
has numbers rather than symbols. This will help fix that!
This commit is contained in:
Justine Tunney 2022-03-23 06:31:55 -07:00
parent 393ca4be40
commit 23b72eb617
61 changed files with 963 additions and 510 deletions

View file

@ -3105,7 +3105,9 @@ static void OnlyRunOnFirstCpu(void) {
}
int main(int argc, char *argv[]) {
if (!NoDebug()) ShowCrashReports();
if (!NoDebug()) {
ShowCrashReports();
}
pty = NewPty();
pty->conf |= kPtyNocanon;
m = NewMachine();

View file

@ -50,6 +50,7 @@ TOOL_BUILD_DIRECTDEPS = \
LIBC_TINYMATH \
LIBC_UNICODE \
LIBC_X \
LIBC_ZIPOS \
NET_HTTPS \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_GDTOA \
@ -78,7 +79,7 @@ o/$(MODE)/tool/build/%.com.dbg: \
o/$(MODE)/tool/build/%.o \
$(CRT) \
$(APE)
-@$(APELINK)
@$(APELINK)
o/$(MODE)/tool/build/blinkenlights.com.dbg: \
$(TOOL_BUILD_DEPS) \
@ -88,13 +89,23 @@ o/$(MODE)/tool/build/blinkenlights.com.dbg: \
$(APE_NO_MODIFY_SELF)
@$(APELINK)
o/$(MODE)/tool/build/blinkenlights.com: \
o/$(MODE)/tool/build/blinkenlights.com.dbg \
o/$(MODE)/third_party/infozip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/build/.blinkenlights/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -9qj $@ \
o/$(MODE)/tool/build/.blinkenlights/.symtab
o/$(MODE)/tool/build/ar.com.dbg: \
$(TOOL_BUILD_DEPS) \
o/$(MODE)/tool/build/build.pkg \
o/$(MODE)/tool/build/ar.o \
$(CRT) \
$(APE)
-@$(APELINK)
@$(APELINK)
o/$(MODE)/tool/build/package.com.dbg: \
$(TOOL_BUILD_DEPS) \
@ -102,7 +113,7 @@ o/$(MODE)/tool/build/package.com.dbg: \
o/$(MODE)/tool/build/package.o \
$(CRT) \
$(APE)
-@$(APELINK)
@$(APELINK)
o/$(MODE)/tool/build/mkdeps.com.dbg: \
$(TOOL_BUILD_DEPS) \
@ -118,7 +129,7 @@ o/$(MODE)/tool/build/compile.com.dbg: \
o/$(MODE)/tool/build/compile.o \
$(CRT) \
$(APE)
-@$(APELINK)
@$(APELINK)
o/$(MODE)/tool/build/zipobj.com.dbg: \
$(TOOL_BUILD_DEPS) \
@ -126,7 +137,7 @@ o/$(MODE)/tool/build/zipobj.com.dbg: \
o/$(MODE)/tool/build/zipobj.o \
$(CRT) \
$(APE)
-@$(APELINK)
@$(APELINK)
o/$(MODE)/tool/build/emulator.o: \
OVERRIDE_COPTS += \

99
tool/build/symtab.c Normal file
View file

@ -0,0 +1,99 @@
/*-*- 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 2022 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"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/ex.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/o.h"
#include "third_party/getopt/getopt.h"
/**
* @fileoverview elf to symbol table file dump tool
*/
void PrintUsage(FILE *f) {
fprintf(f, "%s%s%s\n", "usage: ", program_invocation_name,
" [-?h] -o PATH COMDBG");
}
int main(int argc, char *argv[]) {
int fd, opt;
const char *outpath;
struct SymbolTable *tab;
outpath = 0;
while ((opt = getopt(argc, argv, "?ho:")) != -1) {
switch (opt) {
case 'o':
outpath = optarg;
break;
case '?':
case 'h':
PrintUsage(stdout);
return 0;
default:
PrintUsage(stderr);
return EX_USAGE;
}
}
if (!outpath) {
fprintf(stderr, "error: need output path\n");
PrintUsage(stderr);
return 1;
}
if (optind + 1 != argc) {
fprintf(stderr, "error: need exactly one input path\n");
PrintUsage(stderr);
return 2;
}
if (!(tab = OpenSymbolTable(argv[optind]))) {
fprintf(stderr, "error: %s(%`'s) failed %m\n", "OpenSymbolTable",
argv[optind]);
return 3;
}
tab->names = 0;
tab->name_base = 0;
if ((fd = open(outpath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
fprintf(stderr, "error: %s(%`'s) failed %m\n", "open", outpath);
return 4;
}
if (write(fd, (const char *)tab, tab->size) != tab->size) {
fprintf(stderr, "error: %s(%`'s) failed %m\n", "write", outpath);
return 5;
}
if (close(fd) == -1) {
fprintf(stderr, "error: %s(%`'s) failed %m\n", "close", outpath);
return 6;
}
CloseSymbolTable(&tab);
return 0;
}

View file

@ -52,7 +52,7 @@ struct timespec timestamp;
wontreturn void PrintUsage(int rc, FILE *f) {
fprintf(f, "%s%s%s\n", "Usage: ", program_invocation_name,
" [-n] [-B] [-C INT] [-P PREFIX] [-o FILE] [-s SYMBOL] [-y YOINK] "
"[FILE...]\n");
"[FILE...]");
exit(rc);
}

View file

@ -58,6 +58,8 @@ FLAGS
-P PATH pid file location
-U INT daemon set user id
-G INT daemon set group id
--strace enables system call tracing
--ftrace enables function call tracing
FEATURES

View file

@ -98,14 +98,23 @@ o/$(MODE)/tool/net/redbean.com.dbg: \
o/$(MODE)/tool/net/redbean.com: \
o/$(MODE)/tool/net/redbean.com.dbg \
o/$(MODE)/third_party/infozip/zip.com \
o/$(MODE)/tool/build/symtab.com \
tool/net/net.mk \
tool/net/help.txt \
tool/net/.init.lua \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.ape tool/net/help.txt tool/net/.init.lua tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com -o o/$(MODE)/tool/net/.redbean/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean/.ape \
o/$(MODE)/tool/net/.redbean/.symtab \
tool/net/help.txt \
tool/net/.init.lua \
tool/net/favicon.ico \
tool/net/redbean.png
# REDBEAN-DEMO.COM
#
@ -184,11 +193,16 @@ o/$(MODE)/tool/net/redbean-demo.com.dbg: \
o/$(MODE)/tool/net/redbean-demo.com: \
o/$(MODE)/tool/net/redbean-demo.com.dbg \
o/$(MODE)/tool/build/symtab.com \
o/$(MODE)/third_party/infozip/zip.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-demo
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-demo/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-demo/.ape
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/net/.redbean-demo/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean-demo/.ape \
o/$(MODE)/tool/net/.redbean-demo/.symtab
# REDBEAN-STATIC.COM
#
@ -197,14 +211,22 @@ o/$(MODE)/tool/net/redbean-demo.com: \
o/$(MODE)/tool/net/redbean-static.com: \
o/$(MODE)/tool/net/redbean-static.com.dbg \
o/$(MODE)/third_party/infozip/zip.com \
o/$(MODE)/third_party/infozip/zip.com \
o/$(MODE)/tool/build/symtab.com \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-static
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-static/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-static/.ape tool/net/help.txt tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/net/.redbean-static/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean-static/.ape \
o/$(MODE)/tool/net/.redbean-static/.symtab \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
o/$(MODE)/tool/net/redbean-static.com.dbg: \
$(TOOL_NET_DEPS) \
@ -225,14 +247,22 @@ o/$(MODE)/tool/net/redbean-static.o: tool/net/redbean.c o/$(MODE)/tool/net/redbe
o/$(MODE)/tool/net/redbean-unsecure.com: \
o/$(MODE)/tool/net/redbean-unsecure.com.dbg \
o/$(MODE)/third_party/infozip/zip.com \
o/$(MODE)/third_party/infozip/zip.com \
o/$(MODE)/tool/build/symtab.com \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-unsecure
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-unsecure/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-unsecure/.ape tool/net/help.txt tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/net/.redbean-unsecure/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean-unsecure/.ape \
o/$(MODE)/tool/net/.redbean-unsecure/.symtab \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
o/$(MODE)/tool/net/redbean-unsecure.com.dbg: \
$(TOOL_NET_DEPS) \
@ -256,14 +286,22 @@ o/$(MODE)/tool/net/redbean-unsecure.o: tool/net/redbean.c o/$(MODE)/tool/net/red
o/$(MODE)/tool/net/redbean-original.com: \
o/$(MODE)/tool/net/redbean-original.com.dbg \
o/$(MODE)/third_party/infozip/zip.com \
o/$(MODE)/third_party/infozip/zip.com \
o/$(MODE)/tool/build/symtab.com \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -AMKDIR -T$@ mkdir -p o/$(MODE)/tool/net/.redbean-original
@$(COMPILE) -ADD -T$@ dd if=$@ of=o/$(MODE)/tool/net/.redbean-original/.ape bs=64 count=11 conv=notrunc 2>/dev/null
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -qj $@ o/$(MODE)/tool/net/.redbean-original/.ape tool/net/help.txt tool/net/favicon.ico tool/net/redbean.png
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/net/.redbean-original/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -9qj $@ \
o/$(MODE)/tool/net/.redbean-original/.ape \
o/$(MODE)/tool/net/.redbean-original/.symtab \
tool/net/help.txt \
tool/net/favicon.ico \
tool/net/redbean.png
o/$(MODE)/tool/net/redbean-original.com.dbg: \
$(TOOL_NET_DEPS) \

View file

@ -1232,7 +1232,7 @@ static void ReapZombies(void) {
} while (!terminated);
}
static ssize_t ReadAll(int fd, const char *p, size_t n) {
static ssize_t ReadAll(int fd, char *p, size_t n) {
ssize_t rc;
size_t i, got;
for (i = 0; i < n;) {

View file

@ -71,6 +71,16 @@ o/$(MODE)/tool/viz/%.com.dbg: \
$(APE)
@$(APELINK)
o/$(MODE)/tool/viz/printvideo.com: \
o/$(MODE)/tool/viz/printvideo.com.dbg \
o/$(MODE)/third_party/infozip/zip.com \
o/$(MODE)/tool/build/symtab.com
@$(COMPILE) -AOBJCOPY -T$@ $(OBJCOPY) -S -O binary $< $@
@$(COMPILE) -ASYMTAB o/$(MODE)/tool/build/symtab.com \
-o o/$(MODE)/tool/viz/.printvideo/.symtab $<
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/infozip/zip.com -9qj $@ \
o/$(MODE)/tool/viz/.printvideo/.symtab
o/$(MODE)/tool/viz/derasterize.o: \
OVERRIDE_CFLAGS += \
-DSTACK_FRAME_UNLIMITED \