From 801224df67ef236954633ccd1e989d70696b5fab Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sat, 29 Jul 2023 23:50:15 -0700 Subject: [PATCH] Support symbol tables with arch specific name --- libc/runtime/getsymboltable.c | 27 +++++++++++++++++---------- libc/zip.internal.h | 6 ++++-- test/libc/runtime/fork_test.c | 6 ------ tool/build/compile.c | 8 ++++++++ tool/build/lib/elfwriter_zip.c | 2 +- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/libc/runtime/getsymboltable.c b/libc/runtime/getsymboltable.c index 464a2ac3d..09cd4d9e3 100644 --- a/libc/runtime/getsymboltable.c +++ b/libc/runtime/getsymboltable.c @@ -34,21 +34,27 @@ __static_yoink("__get_symbol"); +#ifdef __x86_64__ +#define SYMTAB_ARCH ".symtab.x86" +#elif defined(__aarch64__) +#define SYMTAB_ARCH ".symtab.aarch64" +#elif defined(__powerpc64__) +#define SYMTAB_ARCH ".symtab.powerpc64" +#else +#error "unsupported architecture" +#endif + static pthread_spinlock_t g_lock; struct SymbolTable *__symtab; // for kprintf -/** - * Looks for `.symtab` in zip central directory. - */ -static ssize_t FindSymtabInZip(struct Zipos *zipos) { - size_t i, n, c; +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) == 7 && - READ32LE(ZIP_CFILE_NAME(zipos->map + c + 0)) == READ32LE(".sym") && - READ16LE(ZIP_CFILE_NAME(zipos->map + c + 4)) == READ16LE("ta") && - *ZIP_CFILE_NAME(zipos->map + c + 6) == 'b') { + if (ZIP_CFILE_NAMESIZE(zipos->map + c) == z && + !memcmp(ZIP_CFILE_NAME(zipos->map + c), name, z)) { return c; } } @@ -63,7 +69,8 @@ static struct SymbolTable *GetSymbolTableFromZip(struct Zipos *zipos) { size_t size, size2; ssize_t rc, cf, lf; struct SymbolTable *res = 0; - if ((cf = FindSymtabInZip(zipos)) != -1) { + if ((cf = GetZipFile(zipos, SYMTAB_ARCH)) != -1 || + (cf = GetZipFile(zipos, ".symtab")) != -1) { lf = GetZipCfileOffset(zipos->map + cf); size = GetZipLfileUncompressedSize(zipos->map + lf); size2 = ROUNDUP(size, FRAMESIZE); diff --git a/libc/zip.internal.h b/libc/zip.internal.h index b02523000..5dc300daa 100644 --- a/libc/zip.internal.h +++ b/libc/zip.internal.h @@ -96,6 +96,7 @@ #define kZipCfileOffsetCrc32 16 #define kZipCfileOffsetCompressedsize 20 #define kZipCfileOffsetUncompressedsize 24 +#define kZipCfileOffsetNamesize 28 #define kZipCfileOffsetExternalattributes 38 #define kZipCfileOffsetOffset 42 @@ -106,6 +107,7 @@ #define kZipLfileOffsetLastmodifiedtime 10 #define kZipLfileOffsetLastmodifieddate 12 #define kZipLfileOffsetCrc32 14 +#define kZipLfileOffsetNamesize 26 #define kZipLfileOffsetCompressedsize 18 #define kZipLfileOffsetUncompressedsize 22 @@ -170,7 +172,7 @@ #define ZIP_CFILE_COMPRESSEDSIZE(P) READ32LE(P + kZipCfileOffsetCompressedsize) #define ZIP_CFILE_UNCOMPRESSEDSIZE(P) \ READ32LE((P) + kZipCfileOffsetUncompressedsize) -#define ZIP_CFILE_NAMESIZE(P) READ16LE((P) + 28) +#define ZIP_CFILE_NAMESIZE(P) READ16LE((P) + kZipCfileOffsetNamesize) #define ZIP_CFILE_EXTRASIZE(P) READ16LE((P) + 30) #define ZIP_CFILE_COMMENTSIZE(P) READ16LE((P) + 32) #define ZIP_CFILE_DISK(P) READ16LE((P) + 34) @@ -203,7 +205,7 @@ READ32LE((P) + kZipLfileOffsetCompressedsize) #define ZIP_LFILE_UNCOMPRESSEDSIZE(P) \ READ32LE((P) + kZipLfileOffsetUncompressedsize) -#define ZIP_LFILE_NAMESIZE(P) READ16LE((P) + 26) +#define ZIP_LFILE_NAMESIZE(P) READ16LE((P) + kZipLfileOffsetNamesize) #define ZIP_LFILE_EXTRASIZE(P) READ16LE((P) + 28) #define ZIP_LFILE_NAME(P) ((const char *)((P) + 30)) #define ZIP_LFILE_EXTRA(P) ((P) + 30 + ZIP_LFILE_NAMESIZE(P)) diff --git a/test/libc/runtime/fork_test.c b/test/libc/runtime/fork_test.c index 1b1899907..df20517c5 100644 --- a/test/libc/runtime/fork_test.c +++ b/test/libc/runtime/fork_test.c @@ -42,7 +42,6 @@ TEST(fork, testPipes) { int a, b; int ws, pid; int pipefds[2]; - alarm(5); ASSERT_NE(-1, pipe(pipefds)); ASSERT_NE(-1, (pid = fork())); if (!pid) { @@ -57,11 +56,9 @@ TEST(fork, testPipes) { EXPECT_NE(-1, close(pipefds[0])); EXPECT_NE(-1, waitpid(pid, &ws, 0)); EXPECT_EQ(31337, b); - alarm(0); } TEST(fork, testSharedMemory) { - alarm(5); int ws, pid; int stackvar; int *sharedvar; @@ -93,7 +90,6 @@ TEST(fork, testSharedMemory) { EXPECT_EQ(1, *privatevar); EXPECT_NE(-1, munmap(sharedvar, FRAMESIZE)); EXPECT_NE(-1, munmap(privatevar, FRAMESIZE)); - alarm(0); } static volatile bool gotsigusr1; @@ -140,13 +136,11 @@ TEST(fork, childToChild) { } TEST(fork, preservesTlsMemory) { - alarm(5); int pid; __get_tls()->tib_errno = 31337; SPAWN(fork); ASSERT_EQ(31337, __get_tls()->tib_errno); EXITS(0); - alarm(0); } void ForkInSerial(void) { diff --git a/tool/build/compile.c b/tool/build/compile.c index 169a62848..3db3dd52e 100644 --- a/tool/build/compile.c +++ b/tool/build/compile.c @@ -66,6 +66,10 @@ #include "libc/x/x.h" #include "third_party/getopt/getopt.internal.h" +#ifndef NDEBUG +__static_yoink("zipos"); +#endif + #define MANUAL \ "\ SYNOPSIS\n\ @@ -843,6 +847,10 @@ int main(int argc, char *argv[]) { int ws, opt, exitcode; char *s, *p, *q, **envp; +#ifndef NDEBUG + ShowCrashReports(); +#endif + mode = firstnonnull(getenv("MODE"), MODE); /* diff --git a/tool/build/lib/elfwriter_zip.c b/tool/build/lib/elfwriter_zip.c index 8470f8205..a1692b0c9 100644 --- a/tool/build/lib/elfwriter_zip.c +++ b/tool/build/lib/elfwriter_zip.c @@ -47,7 +47,7 @@ static bool ShouldCompress(const char *name, size_t namesize, static void GetDosLocalTime(int64_t utcunixts, uint16_t *out_time, uint16_t *out_date) { struct tm tm; - CHECK_NOTNULL(localtime_r(&utcunixts, &tm)); + CHECK_NOTNULL(gmtime_r(&utcunixts, &tm)); *out_time = DOS_TIME(tm.tm_hour, tm.tm_min, tm.tm_sec); *out_date = DOS_DATE(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday + 1); }