jart / cosmopolitan

build-once run-anywhere c library
ISC License
18.32k stars 628 forks source link

Unable to have Ape binaries be zip's using amalgamated build files(using external build system) #214

Open mundusnine opened 3 years ago

mundusnine commented 3 years ago

I am trying to build ape files as archives to store application data using the amalgamated files supplied for the build.

I tried doing what was outlined here: #166 but no cigar.

I am able to build the ape files using the commands shown here but they don't have the PK begin and end signatures.

Building on Debian 10 with default gcc.

ahgamut commented 3 years ago

Can you provide a minimal example? The below example works:

hello.c:

#include <stdio.h>
#include <time.h>

#ifdef COSMOPOLITAN_H_
STATIC_YOINK("usr/share/zoneinfo/Beijing");
// yoinking __zip_start or zip_uri_support doesn't work
#endif

int main(int argc, char **argv) {
#ifdef COSMOPOLITAN_H_
    showcrashreports();
    FILE *f = fopen("zip:sample.txt", "r");
    char buf[128];
    if(feof(f)) return 1;
    printf("viewing file:\n");
    while(fgets(buf, sizeof(buf), f) != NULL)
        printf("%s\n", buf);
    fclose(f);
#endif
    return 0;
}

Compilation:

gcc hello.c -std=gnu99 -g3 -Og \
    -static -nostdlib -nostdinc -fno-pie \
    -mno-red-zone -fno-omit-frame-pointer \
    -pg -no-pie \ 
    -I../libcosmo/header_stubs/ \
    -o hello.com.dbg \
    -fuse-ld=bfd \
    -Wl,-T,../libcosmo/ape.lds \
    -include ../libcosmo/cosmopolitan.h ../libcosmo/crt.o ../libcosmo/ape.o ../libcosmo/cosmopolitan.a
objcopy --strip-all --output-target binary hello.com.dbg hello.com
echo "cosmopolitan libc" > sample.txt
zip hello.com sample.txt
unzip -l hello.com
./hello.com
mundusnine commented 3 years ago

Ok so by using the commands you used I was able to make the .com file a zip but only when the .com file was stripped of all debug information. i.e. : image

@jart is it desired that when the .com file is a .com.dbg that we can't use it as a zip ? If so how would one debug code that uses fopen("zip:sample.txt","r") ? In the image I supplied the first time I do zip hello.com sample.txt the hello.com file is actually a .com.dbg file.

ahgamut commented 3 years ago

Call showcrashreports() at the start of the main function? Here's what I did:

  1. Edit above code snippet to add showcrashreports() at the start of the main function
  2. compile to obtain hello.com.dbg and hello.com (keep both in the same directory)
  3. skip adding the sample.txt to hello.com (now it will segfault at the feof() call)
  4. run hello.com. if hello.com.dbg is in the same directory, gdb starts if hello.com crashes.
  5. if hello.com.dbg is not available, gdb doesn't start, I just get a list of registers.

2021-07-31_22-23-56_1366x768