Make minor improvements

- Work towards simplifying ape.S startup process
- Rewrote ar because it took minutes to build cosmopolitan.a
This commit is contained in:
Justine Tunney 2020-11-09 15:41:11 -08:00
parent 95bc650be8
commit aea89fe832
70 changed files with 1037 additions and 456 deletions

146
tool/decode/ar.c Normal file
View file

@ -0,0 +1,146 @@
/*-*- 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
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/conv/conv.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/x/x.h"
#define AR_MAGIC1 "!<arch>\n"
#define AR_MAGIC2 "`\n"
/**
* ar rU doge.a NOTICE # create archive and use non-deterministic stuff
* o//tool/decode/ar.com doge.a
*/
static int fd;
static uint8_t *data;
static long size;
static const char *path;
static struct stat st;
static void PrintString(uint8_t *p, long n, const char *name) {
char *s;
s = xmalloc(n + 1);
s[n] = '\0';
memcpy(s, p, n);
printf("\t.ascii\t%`'.*s\t\t\t# %s\n", n, s, name);
free(s);
}
static void Open(void) {
if ((fd = open(path, O_RDONLY)) == -1) {
fprintf(stderr, "error: open() failed: %s\n", path);
exit(1);
}
CHECK_NE(-1, fstat(fd, &st));
if (!(size = st.st_size)) exit(0);
CHECK_NE(MAP_FAILED,
(data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0)));
LOGIFNEG1(close(fd));
}
static void Close(void) {
LOGIFNEG1(munmap(data, size));
}
static void Check(void) {
if (size < 8 + 60 + 4 ||
(memcmp(data, AR_MAGIC1, strlen(AR_MAGIC1)) != 0 ||
memcmp(data + 66, AR_MAGIC2, strlen(AR_MAGIC2)) != 0)) {
fprintf(stderr, "error: not a unix archive: %s\n", path);
exit(1);
}
}
static void PrintTable(void) {
}
static void PrintHeader(uint8_t *p) {
PrintString(p + 0, 16, "file identifier [ascii]");
PrintString(p + 16, 12, "file modification timestamp [decimal]");
PrintString(p + 28, 6, "owner id [decimal]");
PrintString(p + 34, 6, "group id [decimal]");
PrintString(p + 40, 8, "file mode [octal] (type and permission)");
PrintString(p + 48, 10, "file size in bytes [decimal]");
PrintString(p + 58, 2, "ending characters");
}
static void Print(void) {
int arsize;
uint8_t *b, *p;
uint64_t offset;
uint32_t i, n, o, table, entries, symbols, symbolslen;
arsize = atoi((char *)(data + 8 + 48));
CHECK_LE(4, arsize);
CHECK_LE(8 + 60 + arsize, size);
entries = read32be(data + 8 + 60);
CHECK_LE(4 + entries * 4 + 1, arsize);
printf("\t# %'s\n", path);
PrintString(data, 8, "file signature");
PrintHeader(data + 8);
printf("\n");
printf("\t.long\t%u\t\t\t# %s\n", entries, "symbol table entries");
table = 8 + 60 + 4;
for (i = 0; i < entries; ++i) {
printf("\t.long\t%#x\t\t\t\t# %u\n", read32be(data + table + i * 4), i);
}
symbols = table + entries * 4;
symbolslen = arsize - (4 + entries * 4);
for (i = o = 0; o < symbolslen; ++i, o += n + 1) {
b = data + symbols + o;
CHECK_NOTNULL((p = memchr(b, '\0', symbolslen - (symbols + o))));
n = p - b;
printf("\t.asciz\t%#`'.*s\t\t\t# %u\n", n, b, i);
}
offset = 8 + 60 + arsize;
while (offset < size) {
offset += offset & 1;
CHECK_LE(offset + 60, size);
CHECK_EQ(0, memcmp(data + offset + 58, AR_MAGIC2, strlen(AR_MAGIC2)));
arsize = atoi((char *)(data + offset + 48));
CHECK_LE(offset + 60 + arsize, size);
printf("\n");
PrintHeader(data + offset);
offset += 60 + arsize;
}
}
int main(int argc, char *argv[]) {
if (argc < 2) return 1;
path = argv[1];
Open();
Check();
Print();
Close();
return 0;
}

View file

@ -134,8 +134,8 @@ static void printelfsectionheader(int i, char *shstrtab) {
printf(".Lsh%d:", i);
show(".long", format(b1, "%d", shdr->sh_name),
format(b2,
"%`'s == getelfstring(elf, st->st_size, shstrtab, shdr->sh_name)",
getelfstring(elf, st->st_size, shstrtab, shdr->sh_name)));
"%`'s == GetElfString(elf, st->st_size, shstrtab, shdr->sh_name)",
GetElfString(elf, st->st_size, shstrtab, shdr->sh_name)));
show(".long",
firstnonnull(findnamebyid(kElfSectionTypeNames, shdr->sh_type),
format(b1, "%d", shdr->sh_type)),
@ -167,7 +167,7 @@ static void printelfsectionheaders(void) {
->sh_offset);
for (i = 0; i < elf->e_shnum; ++i) {
Elf64_Shdr *shdr = getelfsectionheaderaddress(elf, st->st_size, i);
const char *str = getelfstring(elf, st->st_size, shstrtab, shdr->sh_name);
const char *str = GetElfString(elf, st->st_size, shstrtab, shdr->sh_name);
show(".asciz", format(b1, "%`'s", str), NULL);
}
}
@ -199,14 +199,14 @@ static void printelfsymbolother(Elf64_Sym *sym) {
static void printelfsymbol(Elf64_Sym *sym, char *strtab, char *shstrtab) {
show(".long", format(b1, "%d", sym->st_name),
format(b2, "%`'s (sym->st_name)",
getelfstring(elf, st->st_size, strtab, sym->st_name)));
GetElfString(elf, st->st_size, strtab, sym->st_name)));
printelfsymbolinfo(sym);
printelfsymbolother(sym);
show(".short", format(b1, "%d", sym->st_shndx),
format(b2, "%s sym->st_shndx",
sym->st_shndx < 0xff00
? format(b1, "%`'s",
getelfstring(elf, st->st_size, shstrtab,
GetElfString(elf, st->st_size, shstrtab,
getelfsectionheaderaddress(
elf, st->st_size, sym->st_shndx)
->sh_name))
@ -217,8 +217,8 @@ static void printelfsymbol(Elf64_Sym *sym, char *strtab, char *shstrtab) {
static void printelfsymboltable(void) {
size_t i, symcount = 0;
Elf64_Sym *symtab = getelfsymboltable(elf, st->st_size, &symcount);
char *strtab = getelfstringtable(elf, st->st_size);
Elf64_Sym *symtab = GetElfSymbolTable(elf, st->st_size, &symcount);
char *strtab = GetElfStringTable(elf, st->st_size);
char *shstrtab = getelfsectionnamestringtable(elf, st->st_size);
if (symtab && strtab) {
printf("\n\n");
@ -239,8 +239,8 @@ static char *getelfsymbolname(const Elf64_Ehdr *elf, size_t mapsize,
((shstrtab && !sym->st_name &&
ELF64_ST_TYPE(sym->st_info) == STT_SECTION &&
(shdr = getelfsectionheaderaddress(elf, mapsize, sym->st_shndx)) &&
(res = getelfstring(elf, mapsize, shstrtab, shdr->sh_name))) ||
(strtab && (res = getelfstring(elf, mapsize, strtab, sym->st_name))))) {
(res = GetElfString(elf, mapsize, shstrtab, shdr->sh_name))) ||
(strtab && (res = GetElfString(elf, mapsize, strtab, sym->st_name))))) {
return res;
} else {
return NULL;
@ -254,13 +254,13 @@ static void printelfrelocations(void) {
const Elf64_Rela *rela;
const Elf64_Shdr *shdr, *boop;
char *strtab, *shstrtab, *symbolname;
strtab = getelfstringtable(elf, st->st_size);
strtab = GetElfStringTable(elf, st->st_size);
shstrtab = getelfsectionnamestringtable(elf, st->st_size);
for (i = 0; i < elf->e_shnum; ++i) {
if ((shdr = getelfsectionheaderaddress(elf, st->st_size, i)) &&
shdr->sh_type == SHT_RELA &&
(rela = getelfsectionaddress(elf, st->st_size, shdr))) {
printf("\n/\t%s\n", getelfsectionname(elf, st->st_size, shdr));
printf("\n/\t%s\n", GetElfSectionName(elf, st->st_size, shdr));
printf("\t.org\t%#x\n", (intptr_t)rela - (intptr_t)elf);
for (j = 0; ((uintptr_t)rela + sizeof(Elf64_Rela) <=
min((uintptr_t)elf + st->st_size,
@ -272,7 +272,7 @@ static void printelfrelocations(void) {
symbolname =
getelfsymbolname(elf, st->st_size, strtab, shstrtab, &syms[sym]);
printf("/\t%s+%#lx → %s%c%#lx\n",
getelfstring(
GetElfString(
elf, st->st_size, shstrtab,
getelfsectionheaderaddress(elf, st->st_size, shdr->sh_info)
->sh_name),

View file

@ -68,6 +68,6 @@ kPollNames:
lodsl
add %rbx,%rax # %rbx is image base (cosmo abi)
stosq
loop 0b
.loop 0b
add $16,%rdi
.init.end 301,_init_kPollNames